我知道您不使用 Gaphics 函数而是选择操作像素阵列。
尝试更改您的游戏以使用 Graphics 对象,因为没有(简单而有效的)解决方法。如果你仍然选择不这样做,请尝试添加
System.setProperty("sun.java2d.opengl", "true");
在代码的最开头,紧接在
之后
public static void main(String[] args) {
当我第一次制作简单的游戏时,我尝试过和你一样的方式,但后来你意识到内置的图形功能在性能和易用性方面非常优越。
编辑:
关于基本游戏如何使用 Graphics 对象的简短说明:
假设您创建了一个 JFrame。如果您随后添加一个从 Canvas 扩展的类并将其添加到其中。如果您想使用菜单,您甚至可以先创建一个 JPanel,然后将 Canvas 添加到 Jpanel 中,这样您就可以更轻松地隐藏它。
这里有一个我们如何创建可用画布的示例:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
public static final int WIDTH = 1920;
public static final int HEIGHT = WIDTH * 9 / 16;
public static final String TITLE = "YOUR GAMES NAME";
public static final int TICKSPERS = 120;
public static final boolean ISFRAMECAPPED = false;
public static JFrame frame;
private Thread thread;
private boolean running = false;
public int frames;
public int lastFrames;
public int ticks;
public Game(){
Dimension size = new Dimension(WIDTH, HEIGHT);
setPreferredSize(size);
setMaximumSize(size);
setMinimumSize(size);
}
public void render(){
frames++;
BufferStrategy bs = getBufferStrategy();
if (bs == null){
createBufferStrategy(2);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(new Color(79,194,232));
g.fillRect(0, 0, getWidth(), getHeight());
//Call your render funtions from here
g.setColor(Color.BLACK);
g.fillRect(120,70,35,90);
g.dispose();
bs.show();
}
public void tick(){
}
public synchronized void start(){
if(running) return;
running = true;
thread = new Thread(this, "Thread");
thread.start();
}
public synchronized void stop(){
if(!running) return;
running = false;
try {
System.exit(1);
frame.dispose();
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void init(){
}
public void run() {
init();
//Tick counter variable
long lastTime = System.nanoTime();
//Nanoseconds per Tick
double nsPerTick = 1000000000D/TICKSPERS;
frames = 0;
ticks = 0;
long fpsTimer = System.currentTimeMillis();
double delta = 0;
boolean shouldRender;
while(running){
shouldRender = !ISFRAMECAPPED;
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
//if it should tick it does this
while(delta >= 1 ){
ticks++;
tick();
delta -= 1;
shouldRender = true;
}
if (shouldRender){
render();
}
if (fpsTimer < System.currentTimeMillis() - 1000){
System.out.println(ticks +" ticks, "+ frames+ " frames");
ticks = 0;
lastFrames = frames;
frames = 0;
fpsTimer = System.currentTimeMillis();
}
}
}
public static void main(String[] args){
Game game = new Game();
frame = new JFrame(TITLE);
frame.add(game);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
game.start();
}
}
这个类可以作为你的基础。它的 tick 方法每秒调用 120 次,它的 render 方法可以将内容渲染到屏幕上。
如果您不熟悉图形对象的功能,我建议您阅读一下它们。
您无法从外部接触到 Graphics 对象。在处理 Graphics 对象之前,您需要从游戏渲染函数内部调用渲染函数。尝试将您的游戏逻辑与渲染功能分开。