运行这个java游戏循环代码来渲染图形会降低性能
我有这个教程中的代码 我不明白他为什么跑得这么慢 有一个线程处理游戏循环并在屏幕上渲染图形。 在实现render()方法之前,它运行得很好 有一个线程处理游戏循环并在屏幕上渲染图形运行这个java游戏循环代码来渲染图形会降低性能,java,swing,graphics,game-loop,Java,Swing,Graphics,Game Loop,我有这个教程中的代码 我不明白他为什么跑得这么慢 有一个线程处理游戏循环并在屏幕上渲染图形。 在实现render()方法之前,它运行得很好 有一个线程处理游戏循环并在屏幕上渲染图形 public class Game extends Canvas implements Runnable { public static final int WIDTH = 420; public static final int HEIGHT = WIDTH / 12 * 9; publi
public class Game extends Canvas implements Runnable {
public static final int WIDTH = 420;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 2;
private boolean running = false;
private Thread thread;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
@Override
public void run() {
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while(running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1) {
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println(updates + " Ticks, FPS "+ frames);
updates = 0;
}
}
stop();
}
private void tick() { }
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
//////////////////////////////////
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
//////////////////////////////////
g.dispose();
bs.show();
}
public static void main(String[] args) {
//jframe creation etc...
game.start();
}
public synchronized void start() {
if(running) return;
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop() {
if(!running) return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(1);
}
}
“寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现该问题所需的最短代码。没有明确问题说明的问题对其他读者没有用。请参阅:。”“寻求调试帮助的问题(”“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题说明的问题对其他读者没有用处。请参阅: