-1

现在是夏天,所以我想多自学一点,所以我开始创建一个吃豆人游戏,但刚开始我就遇到了问题。

public class PacMan {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {

    GameApp runapp = new GameApp();

    runapp.run();
}

}

类 GameApp

public class GameApp {  
    public void run() throws IOException {

        GameCanvas game = new GameCanvas();
        PacPlay play = new PacPlay();
        JFrame frame = new JFrame(game.getTitle());
        frame.setSize(game.getWidth(), game.getHeight());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.add(play);
    } 
} 

其他类

public class PacPlay extends JPanel implements ActionListener , KeyListener {

    int X , Y = 0 ;
    int KeyCode ;

    BufferedImage PacUP ;
    BufferedImage PacDOWN ;
    BufferedImage PacLEFT ;
    BufferedImage PacRIGHT ;  

    public PacPlay() throws IOException {
        PacRIGHT = ImageIO.read(new File("images\\right.GIF"));
    }

    public void PaintComponent(Graphics2D g) {
        g.drawImage(PacRIGHT , X, Y , null);
    }

    @Override
    public void keyPressed(KeyEvent ke) {
        KeyCode = ke.getKeyCode();
    }
}

我在这里得到的只是一个空框架。我哪里错了?

4

1 回答 1

5

setVisible应该在所有组件都添加到框架后调用。Java 也是区分大小写的,所以它的

@Override 
public void paintComponent(Graphics g) { // note Graphics instead of Graphics2D 
   super.paintComponent(g);
   g.drawImage(pacRightImage, x, y, this);
}

在进行自定义绘画时。记得调用super.paintComponent(g)来绘制背景组件

阅读:执行自定义绘画

于 2014-07-20T15:53:06.407 回答