遊戲循環:Java圖像出現和消失使用遊戲循環時
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();
}
}
}
paint方法:
/**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倍它指的是一種方法,其中的相同的文件夾此方法繪製圖像。一切看起來不錯,順利,當我運行該程序的圖像出現,並在快速消失,有時圖像出現,沒有什麼不好發生,但經過一段隨機時間的事情發生我把fps從低到高,從高到低仍然相同即時通訊使用jpanel在這個項目中
你的'圖像'在哪裏創建/操作? – JimmyB
聽起來像缺乏雙緩衝http://content.gpwiki.org/index.php/Java%3aTutorials%3aDouble_Buffering – weston
@weston也是這麼想的,但是將圖像傳遞給畫布應該像更新一樣快顯示器。 – JimmyB