paintComponent不适用于我的代码

时间:2014-03-02 09:43:45

标签: java swing paintcomponent repaint event-dispatch-thread

public void startGame() {
    Main m = new Main();
    if(m.level == 1)
        delay = 100;
    else if (m.level == 2)
        delay = 50;
    else if (m.level == 3)
        delay = 25;

    //border = m.border;

    board.setSize(new Dimension(630, 455));
    board.setVisible(true);
    //board.setLocation(100, 100);

    //additional.setPreferredSize(new Dimension(630, 64));
    //additional.setVisible(true);

    loadImages();

    xCoor[0] = 315;
    yCoor[0] = 245;

    evilMinion();
    run();
    //timer = new Timer(delay, this);
    //timer.start();

}

public void run() {
    if (isRunning && !isPaused) {
        checkEvil();
        checkCollision();
        nextStep();
    }
    board.repaint();
}

public void paintComponent (Graphics g) {
    super.paintComponents(g);

    if(isRunning) {
        g.drawImage(evil, evilCoor[0], evilCoor[1], null);
        System.out.println("RUN RUN RUN");

        for (int z = 0; z <= dots; z++) {
            if (z == 0)
                g.drawImage(hero, xCoor[z], yCoor[z], board);
            else if (z % 2 == 0)
                g.drawImage(person1, xCoor[z], yCoor[z], board);
            else if (z % 2 == 1)
                g.drawImage(person2, xCoor[z], yCoor[z], board);
        }
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }
}

@Override
public void actionPerformed(ActionEvent e) {
    if (isRunning && !isPaused) {
        checkEvil();
        checkCollision();
        nextStep();
    }
    board.repaint();
}

我试图制作一个蛇游戏(以奴才作为图标,所以我很抱歉为evilMinion的事情)我真的不明白图形是如何在java工作所以我不明白为什么paintComponent部分不工作。当我尝试运行它时,它只是一个空白屏幕。顺便说一句,我打算将电路板组件添加到框架中。我很抱歉请帮助我

0 个答案:

没有答案