我有一本名为“Killer Game Programming in Java”的书,我应该安装 J2SE 5.0,但 J2SE 已停产。在与 StackOverflow 和作者讨论后,我从 oracle.com 安装了更新版本,即 JSE 7u5。我下载了包含 JRE 的 JDK。所以现在我将 NetBeans 7.1.2 与 JDK 1.7 一起使用,但终端称它为 1.7.0_05。第一个程序说它不能在没有 main 方法的情况下运行。我安装的更新版本可能无法与本书中的程序一起使用,是否有任何原因?我应该尝试另一个推荐的版本吗?我在下面发布了完整的代码。没有编辑。它来自此链接http://fivedots.coe.psu.ac.th/~ad/jg/ch1/ch1.pdf任何错误都以斜体和粗体显示。
public class GamePanel extends JPanel implements Runnable
{
private static final int PWIDTH = 500; // size of panel
private static final int PHEIGHT = 400;
private Thread animator;
private boolean running = false;
private boolean gameOver = false;
// more variables, explained later
:
public GamePanel()
// for the animation
// stops the animation
// for game termination
{
setBackground(***Color***.white); // white background
setPreferredSize( new ***Dimension***(PWIDTH, PHEIGHT));
// create game components
} // end of GamePanel()
public void addNotify()
/* Wait for the JPanel to be added to the
JFrame/JApplet before starting. */
{
***super.addNotify();*** // creates the peer
startGame(); // start the thread
}
private void startGame()
// initialise and start the thread
{
if (animator == null || !running) {
***animator = new Thread(this);***
animator.start();
}
} // end of startGame()
public void stopGame()
// called by the user to stop execution
{ running = false; }
public void run()
/* Repeatedly update, render, sleep */
{
running = true;
while(running) {
gameUpdate();
***gameRender();***
repaint();
try {
// game state is updated
// render to a buffer
// paint with the buffer
Thread.sleep(20); // sleep a bit
}
catch(InterruptedException ex){}
}
System.exit(0); // so enclosing JFrame/JApplet exits
} // end of run()
private void gameUpdate()
{ if (!gameOver)
// update game state ...
}
// more methods, explained later...
} // end of GamePanel class