私は他の誰かのコードで遊んで始めて、面白い実験に出くわしました。プログラムはifステートメントで正常に動作します。しかし、ifステートメントをwhileループに変更すると、プログラムは実行されますが、Xボタンでプログラムを閉じることができず、代わりにEclipseの終了ボタンを押す必要があることがわかりました。これは無限ループの兆候だと思いますか、それともJavaが同じ画像を何度も繰り返し描画できないという事実ですか?
// if you want to draw graphics on the screen, use the paintComponent method
// it give you a graphic context to draw on
public void paintComponent(Graphics g){
super.paintComponent(g);
// when the player is still in the game
if(inGame){
g.drawImage(apple, apple_x, apple_y, this);
for (int z = 0; z < dots; z++) {
if (z == 0)
g.drawImage(head, collisionX[z], collisionY[z], this);
else g.drawImage(tail, collisionX[z], collisionY[z], this);
}
Toolkit.getDefaultToolkit().sync();
// dispose graphics and redraw new one
g.dispose();
}
else gameOver(g);
}