0

3 年来我第一次成功地创建了一个(基本)动画 JApplet,但我对它移动时闪烁的图像感到恼火。Timer 对象使图像移动,而我的私有内部类“TimerListener”负责移动图像的动画运动。

这是我的 TimerListener 类的代码,我认为这个问题可能可以解决:

@Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(smileyFace.getImage(), xCoord, yCoord, this);
    }

    private class TimerListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            //Following if-else manipulates Y coordinate
            if (goingUp) {
                if (yCoord > minY) {
                    yCoord -= move;
                } 
                else {
                    goingUp = false;
                }
            } else {
                if (yCoord < (getContentPane().getHeight() - (smileyFace.getIconHeight()+ Y_OFFSET))) {
                    yCoord += move;
                } 
                else {
                    goingUp = true;
                }
            }

            //Following if-else manipulates X coordinate
            if (goingSideways) {
                if (xCoord > 0) {
                    xCoord -= move;
                } 
                else {
                    goingSideways = false;
                }
            } else {
                if (xCoord < (getContentPane().getWidth() - (smileyFace.getIconWidth() + X_OFFSET))) {
                    xCoord += move;
                } 
                else {
                    goingSideways = true;
                }
            }

            repaint();
        }
    }

如果有帮助,这是我的 JApplet 的屏幕截图 - 在这种情况下,巨魔的脸应该在黑色区域中移动,并在撞击它们时从侧面反弹:

在此处输入图像描述

对于那些想要运行和测试 JApplet 的人,可以从https://github.com/rattfieldnz/Java_Projects/tree/master/JAppletAnimation获取 Netbeans 项目。

4

1 回答 1

1

感谢用户'arynaq',我已经解决了我的问题。我把下面的画法:

@Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(smileyFace.getImage(), xCoord, yCoord, this);
    }

...进入扩展 JPanel 的内部类(注意我如何将“paint”更改为“paintComponent”):

class ImagePanel extends JPanel
    {

        public ImagePanel()
        {
            setBackground(Color.BLACK);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(smileyFace.getImage(), xCoord, yCoord, this);
        }

        @Override
        public void setBackground(Color bg) {
            super.setBackground(bg); //To change body of generated methods, choose Tools | Templates.
        }


    }

...然后通过它的 init() 方法将它添加到我的 JApplet 中(我不确定通过调用 JApplet 的构造函数是否正确...):

@Override
public void init() {

    smileyFace = new ImageIcon("images/happyFace.png");
    **add(new ImagePanel());**
    timerDelay = 10;
    timer = new Timer(timerDelay, new TimerListener());
    timer.start();

    //getContentPane().setBounds(0, 0, CONTENTPANE_WIDTH, CONTENTPANE_HEIGHT);
    getContentPane().setBackground(Color.BLACK);

    //maxY = getContentPane().getHeight();
    minY = 0;

    xCoord = 0;
    yCoord = 0;
    move = 2;

}

您可以通过克隆我的 GitHub JApplet 项目并在 NetBeans 中运行它来查看它的运行情况 :)。

于 2013-06-07T13:11:59.110 回答