简体   繁体   English

为什么Java swing计时器导致动画口吃少于睡眠?

[英]Why does Java swing timer lead to less animation stutter than sleep?

I have two almost identical classes: AnimationFrame1 and AnimationFrame2. 我有两个几乎相同的类:AnimationFrame1和AnimationFrame2。 Both of these classes display a blue ball moving back and forth horizontally across a 500 x 500 window. 这两个类都显示一个蓝色的球在500 x 500的窗口上水平来回移动。 The two classes are identical save for the runAnimation() and createAndShowGUI() methods. 除了runAnimation()和createAndShowGUI()方法之外,这两个类是相同的。 In its runAnimation() method, AnimationFrame1 uses a while loop and sleep method to create the animation loop whereas AnimationFrame2 uses a Swing Timer. 在其runAnimation()方法中,AnimationFrame1使用while循环和sleep方法创建动画循环,而AnimationFrame2使用Swing Timer。 In its createAndShowGUI() method, AnimationFrame1 creates a new thread and calls the runAnimation() method on it whereas AnimationFrame2 simply calls the runAnimation() method with no new thread. 在其createAndShowGUI()方法中,AnimationFrame1创建一个新线程并在其上调用runAnimation()方法,而AnimationFrame2只调用没有新线程的runAnimation()方法。

After compiling both classes, I found that AnimationFrame2, the one that uses the Swing Timer, displays a much smoother animation that doesn't stutter as much as the animation displayed in AnimationFrame1, which uses the while loop and sleep method. 在编译了这两个类之后,我发现使用Swing Timer的AnimationFrame2显示了一个更平滑的动画,它不会像使用while循环和sleep方法的AnimationFrame1中显示的动画那样断断续续。 My question is: why does AnimationFrame1 display more stutter in its animation than AnimationFrame2? 我的问题是:为什么AnimationFrame1在动画中显示出比AnimationFrame2更多的口吃? I've searched around for a reason for this, but have so far found nothing. 我一直在寻找原因,但迄今为止一无所获。

Also, I'm obviously a Java novice, so please let me know if you see anything wrong with my code or if you know of any way I could improve it. 另外,我显然是Java新手,所以如果您发现我的代码有任何问题或者您知道我可以改进它的任何方式,请告诉我。

Here is AnimationFrame1: 这是AnimationFrame1:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

class AnimationFrame1 extends JPanel {

    int ovalX;
    int prevX;
    Timer timer;
    boolean moveRight;
    BufferedImage img;

    public AnimationFrame1() {
        setPreferredSize(new Dimension(500, 500));
    }

    public void runAnimation() {
        moveRight = true;
        img = null;
        ovalX = 0;
        prevX = 0;
        while(true) {
            if (moveRight == true) {
                prevX = ovalX;
                ovalX = ovalX + 4;
            }
            else {
                prevX = ovalX - 4;
                ovalX = ovalX - 4;
            }
            repaint();
            if (ovalX > 430) {
                moveRight = false;
            }
            if (ovalX == 0) {
                moveRight = true;
            }
            try {
                Thread.sleep(25);
            }
            catch(Exception e) {
            }
        }
    }

    public void paintComponent(Graphics g) {
        if (img == null) {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = getGraphicsConfiguration();
            img = gc.createCompatibleImage(78, 70);
            Graphics gImg = img.getGraphics();
            gImg.setColor(getBackground());
            gImg.fillRect(0, 0, getWidth(), getHeight());
            gImg.setColor(Color.BLUE);
            gImg.fillOval(4, 0, 70, 70);
            gImg.dispose();
        }
        g.drawImage(img, ovalX, 250, null);
    }

    public static void createAndShowGUI() {
        JFrame mainFrame = new JFrame();
        final AnimationFrame1 animFrame = new AnimationFrame1();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.add(animFrame);
        mainFrame.pack();
        mainFrame.createBufferStrategy(2);
        mainFrame.setVisible(true);
        new Thread(new Runnable() {
            public void run() {
                animFrame.runAnimation();
            }
        }).start();
    }    

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

}

And here is AnimationFrame2: 这是AnimationFrame2:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

class AnimationFrame2 extends JPanel {

    int ovalX;
    int prevX;
    Timer timer;
    boolean moveRight;
    BufferedImage img;

    public AnimationFrame2() {
        setPreferredSize(new Dimension(500, 500));
    }

    public void runAnimation() {
        moveRight = true;
        img = null;
        ovalX = 0;
        prevX = 0;
        timer = new Timer(25, new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if (moveRight == true) {
                    prevX = ovalX;
                    ovalX = ovalX + 4;
                }
                else {
                    prevX = ovalX - 4;
                    ovalX = ovalX - 4;
                }
                repaint();
                if (ovalX > 430) {
                    moveRight = false;
                }
                if (ovalX == 0) {
                    moveRight = true;
                }
            }
        });
        timer.start();
    }

    public void paintComponent(Graphics g) {
        if (img == null) {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = getGraphicsConfiguration();
            img = gc.createCompatibleImage(78, 70);
            Graphics gImg = img.getGraphics();
            gImg.setColor(getBackground());
            gImg.fillRect(0, 0, getWidth(), getHeight());
            gImg.setColor(Color.BLUE);
            gImg.fillOval(4, 0, 70, 70);
            gImg.dispose();
        }
        g.drawImage(img, ovalX, 250, null);
    }

    public static void createAndShowGUI() {
        JFrame mainFrame = new JFrame();
        final AnimationFrame2 animFrame = new AnimationFrame2();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.add(animFrame);
        mainFrame.pack();
        mainFrame.createBufferStrategy(2);
        mainFrame.setVisible(true);
        animFrame.runAnimation();
    }    

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

}

After putting markers in the code, it appears that the Timer version actually runs every 30 ms whereas the Thread.sleep version runs every 25 ms. 将标记放入代码后,似乎Timer版本实际上每30毫秒运行一次,而Thread.sleep版本每25毫秒运行一次。 There could be several explanations, including: 可能有几种解释,包括:

  • the resolution of Timers, which is not as good as that of Thread.sleep 定时器的分辨率,不如Thread.sleep的分辨率
  • the fact that Timers are single threaded (apart from the wait, everything is run in the EDT) so if a task (like repainting) takes more than 25ms, it will delay the next task 计时器是单线程的事实(除了等待,一切都在EDT中运行)所以如果任务(如重新绘制)需要超过25毫秒,它将延迟下一个任务

If I increase the sleep to 30ms the 2 animations are similar (the actual number may vary depending on your machine). 如果我将睡眠时间增加到30毫秒,则2个动画类似(实际数量可能因机器而异)。

Note: there is a potential thread safety issue in the Thread.sleep version. 注意:Thread.sleep版本中存在潜在的线程安全问题。 You share variables between the worker thread and the UI thread without proper synchronization. 您在工作线程和UI线程之间共享变量而没有正确的同步。 Although it seems that repaint internally introduces a synchronization barrier which ensures the visibility of the changes made by the worker thread from the UI thread, it is an incidental effect and it would be a better practice to explicitly ensure visibility, for example by declaring the variables volatile. 虽然看起来repaint内部引入了同步障碍,该同步障碍确保了工作线程从UI线程所做的更改的可见性,但它是一种偶然的效果,并且明确确保可见性是更好的做法,例如通过声明变量易挥发。

The reason for the problem is most likely due to the "violation" of AWT semantics in the first version. 出现此问题的原因很可能是由于第一版中“违反”AWT语义。 you cannot run gui update code outside of the EDT. 不能在EDT之外运行gui更新代码。

UPDATE: even if the repaint() method is safe to call from another thread, all it is doing is queueing an event which will run on the EDT. 更新:即使repaint()方法可以安全地从另一个线程调用,它所做的只是排队将在EDT上运行的事件。 this means there is a race condition between the thread modifying the ovalx and thread EDT thread which is reading it. 这意味着在修改ovalx的线程和正在读取它的线程EDT线程之间存在竞争条件。 this will cause the movement to be uneven as the drawing code may see different values than the signalling code intends. 这将导致移动不均匀,因为绘图代码可能看到与信令代码意图不同的值。

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

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