如何停止JApplet中的图像移动闪烁

时间:2013-06-07 10:23:15

标签: java applet japplet

我设法在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的截图 - 在这种情况下,巨魔脸应该在黑色区域移动,并在它们击中时从侧面反弹:

enter image description here

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

1 个答案:

答案 0 :(得分: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;

}

您可以通过克隆my GitHub JApplet project并在NetBeans中运行它来看到它正在运行。)。

相关问题