Java Graphic2D创建Pacman的问题

-1 java swing pacman graphic

这是夏天,所以我想更多地教自己,所以我开始创建一个pacman游戏,但我刚开始时遇到了问题.

public class PacMan {

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

    GameApp runapp = new GameApp();

    runapp.run();
}

}
Run Code Online (Sandbox Code Playgroud)

类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);
    } 
} 
Run Code Online (Sandbox Code Playgroud)

其他课程

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我到这里只是一个空框架.我哪里错了?

Rei*_*eus 5

setVisible应在将所有组件添加到框架后调用.Java也区分大小写,所以它

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

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

阅读:执行自定义绘画