【发布时间】:2014-12-19 21:59:35
【问题描述】:
我最近刚接触 Java,我的目标是用它制作一个简单的图形游戏,所以请随时指出任何风格错误。
从我的主标题屏幕过渡到主屏幕,我的旧标题屏幕没有刷新,用于单击进入主屏幕的按钮被冻结,基本上,图像被冻结并且主屏幕paintComponent没有调用,程序只是进入无限循环并且不会关闭(必须通过任务管理器关闭)。
有趣的是,如果没有 while 循环,它工作得很好,paintComponent 被调用并且一切正常,当重新引入 while 循环时,同样的问题仍然存在。
public class Game {
private static final int HEIGHT = 650;
private static final int WIDTH = 820;
private static final int FRAMES_PER_SEC = 60;
private JFrame frame = new JFrame("Game");
private boolean inIntroScreen = true;
private boolean game_running = false;
private int x = 1;
private int y = 1;
private int dx = 1;
private int dy = 1;
/* method to set up GUI for the game. */
public void initGUI () {
//Build Frame
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
//End Build Frame
/* Intro screen build */
class drawIntro extends JPanel {
public void paintComponent(Graphics g) {
if (inIntroScreen) {
Graphics2D g2d = (Graphics2D) g;
//Background
g2d.setPaint(Color.BLACK);
g2d.fillRect(0, 0, 820, 650);
//Title
BufferedImage img = null;
try { img = ImageIO.read(new File("game.png")); }
catch (IOException e) { System.out.println("Error image"); }
g2d.drawImage(img, 180, 52, null);
g2d.setPaint(Color.WHITE);
g2d.fillOval(550, 60, 40, 40);
g2d.fillOval(195, 60, 40, 40);
System.out.println("Intro screen painted");
}
} //end paint
} //end draw inner class
final drawIntro introScreen = new drawIntro();
final JPanel introPanel = new JPanel();
final JButton startButton = new JButton("Start");
frame.getContentPane().add(introPanel,BorderLayout.SOUTH);
introPanel.setBackground(Color.BLACK);
frame.getContentPane().add(introScreen, BorderLayout.CENTER);
startButton.setPreferredSize(new Dimension(100,50));
startButton.setBackground(Color.BLACK);
startButton.setForeground(Color.WHITE);
introPanel.add(startButton);
introScreen.repaint();
//End intro screen build
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
introPanel.removeAll();
introPanel.revalidate();
inIntroScreen = false;
game_running = true;
System.out.println("button clicked");
Start();
}
});
} //End initGUI
/* Level building class */
class Level extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//Background
g2d.setPaint(Color.BLACK);
g2d.fillRect(0, 0, 820, 650);
//Anti-aliasing
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setPaint(Color.BLUE);
g2d.fillOval(x, y, 70, 70);
x += dx;
y += dy;
System.out.println("Main screen painted");
} //End paint component
}
/* Game loop */
public void Start () {
Level player = new Level();
frame.add(player);
player.repaint();
int FPS = 1000 / FRAMES_PER_SEC;
while(game_running) { /* PROBLEM HERE, if while loop is removed everything works as intended */
frame.repaint();
try { Thread.sleep(FPS); }
catch (InterruptedException e) {}
}
}
public static void main(String[] args) {
Game game = new Game();
game.initGUI();
System.out.println("Program terminated");
}
} //end game class
【问题讨论】: