GIF 在游戏鼠标移动时停止动画

GIF stops animating while gaming mouse is moving

当我在 javax.swing.JFrame 内移动我的游戏鼠标时,所有动画 GIF(javax.swing.ImageIconjavax.swing.JLabel 内)停止动画,直到鼠标停止移动。

发生在 macOS 上带有驱动程序的游戏鼠标(使用 Rocket-Kone XTD 和 Razer 游戏测试两台计算机上的鼠标)。当我使用其他鼠标时,一切正常。

游戏鼠标还导致 javax.swing.Timer 停止调用其 actionPerformed() 方法。 解决这个问题,但可以使用 java.util.TimerTask 来解决。 (编辑: 实际上 TimerTask 也没有修复它,因为 JFrame 在鼠标停止移动之前不会重新绘制。)

但我找不到动画 GIF 的替代方法。我更感兴趣的是解决问题而不是使用替代方案,尽管我也会感谢一个可行的替代方案。

代码:

import java.lang.reflect.InvocationTargetException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class Mouse {
    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    new Mouse();
                }

            });
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public Mouse() {
        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ImageIcon(getClass().getResource("waiting.gif")));

        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        frame.add(label);
    }
}

运行申请:

MCVE:

import java.lang.reflect.InvocationTargetException;
import java.net.*;
import javax.swing.*;

public class Mouse {
    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    try {
                        new Mouse();
                    } catch (MalformedURLException ex) {
                        ex.printStackTrace();
                    }
                }

            });
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public Mouse() throws MalformedURLException {
        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ImageIcon(
                new URL("https://i.stack.imgur.com/HXCUV.gif")));

        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        frame.add(label);
    }
}

我解决了这个问题,因为我将鼠标的轮询频率从 1000Hz 降低到 500Hz。现在一切正常。我认为问题在于 UI-线程过度扩展,每秒处理 1000 次轮询,因此忙于制作 GIF 动画。