gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:35:35 25 4
gpt4 key购买 nike

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

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

游戏鼠标还导致 javax.swing.Timer 停止调用它们的 actionPerformed() 方法。 I opened a thread here对于这个问题,但这可以使用 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);
}
}

运行应用:

enter image description here

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("/image/HXCUV.gif")));

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

frame.add(label);
}
}

最佳答案

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

关于java - GIF 在游戏鼠标移动时停止动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40742618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com