繁体   English   中英

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

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

我设法创建了一个(基本的)动画JApplet,这是3年以来的第一次,但是当图像移动时,图像闪烁使我感到烦恼。 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项目。

感谢用户“ 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中运行它,您可以看到它正在运行:)。

暂无
暂无

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

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