繁体   English   中英

创建Pacman时出现Java Graphic2D问题

[英]Java Graphic2D Issue in creating Pacman

现在是夏天,所以我想多学一点点,所以我开始制作吃豆子游戏,但刚开始时遇到了问题。

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();
    }
}

我到这里来的只是一个空框架。 我要去哪里错了?

将所有组件添加到框架后,应调用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)画背景组分

阅读: 执行定制绘画

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM