简体   繁体   English

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

[英]How to stop moving image in a JApplet from flickering

I managed to create a (basic) animated JApplet for the first time in 3 years, but I am annoyed with the image flickering when it moves. 我设法创建了一个(基本的)动画JApplet,这是3年以来的第一次,但是当图像移动时,图像闪烁使我感到烦恼。 The Timer object is what is making the image move, and my private inner class "TimerListener" is responsible for the animated motion of the moving image. Timer对象使图像移动,而我的私有内部类“ TimerListener”负责运动图像的动画运动。

Here is the code of my TimerListener class, where I think this problem may be able to be solved: 这是我的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();
        }
    }

If it helps, here is a screenshot of my JApplet - in this case, the troll face should be moving in the black area, and bouncing off the sides as it hits them: 如果有帮助,这是我的JApplet的屏幕截图-在这种情况下,巨魔的脸应该在黑色区域中移动,并在击中侧面时从侧面弹起:

在此处输入图片说明

For those of you who want to run and test the JApplet, you can get the Netbeans project from https://github.com/rattfieldnz/Java_Projects/tree/master/JAppletAnimation . 对于那些想要运行和测试JApplet的人,可以从https://github.com/rattfieldnz/Java_Projects/tree/master/JAppletAnimation获得Netbeans项目。

Thanks to the user 'arynaq', I have fixed my problem. 感谢用户“ arynaq”,我解决了我的问题。 I put the following paint method: 我把下面的绘画方法:

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

...into an inner class which extends JPanel (notice how I changed 'paint' to 'paintComponent'): ...进入扩展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.
        }


    }

... then added it to my JApplet via it's init() method (I'm not sure if I'd be correct by calling this the JApplet's constructor...): ...然后通过它的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;

}

You can see it running by cloning my GitHub JApplet project and running it in NetBeans :). 通过克隆我的GitHub JApplet项目并在NetBeans中运行它,您可以看到它正在运行:)。

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

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