【发布时间】:2016-03-19 22:17:28
【问题描述】:
游戏循环:
private int FPS = 25;
private int targetTime = 1000 / FPS;
public void run(){
init();
long start;
long elapsed;
long wait;
while (running){
start = System.nanoTime();
init();
repaint();
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed / 1000000;
try {
Thread.sleep(wait);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
绘制方法:
/**draws the game graphics*/
public void paint(Graphics g){
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
gameStateHandler.draw(g2);
}
paint方法也指的方法:
private static Image image = resourceLoader.getImage("/background/menu_background.png");
/**draws the menu state*/
public static void draw(Graphics2D g){
g.drawImage(image, 0, 0, null);
}
我通过这个方法调用图像,它位于图像的同一文件夹中
static resourceLoader rl = new resourceLoader();
public static Image getImage(String image){
return Toolkit.getDefaultToolkit().getImage(rl.getClass().getResource(image));
}
我有一个游戏循环,它将每秒调用repaint(); 60 次,在paint 方法中它指的是该方法绘制图像的方法。一切看起来都很好,很流畅,当我运行程序时,图像会很快出现和消失,有时图像会出现并且没有发生任何不好的事情,但是在随机发生的时间之后,我将 fps 从低到高再从高在这个项目中使用 jpanel 来降低仍然相同的 im
【问题讨论】:
-
您的
image是如何/在哪里创建/操作的? -
听起来好像缺少双缓冲content.gpwiki.org/index.php/…
-
@weston 也是这么想的,但是将图像blitting到画布应该尽可能快地更新显示。
-
对于双缓冲,你也可以看看这里:stackoverflow.com/questions/10508042/…
-
@HannoBinder 我也发现了,但是关于这个问题并没有很好的答案。
标签: java image paint game-loop