While I move my gaming mouse inside a javax.swing.JFrame
, all animated GIFs (javax.swing.ImageIcon
inside a javax.swing.JLabel
) stops animating until the mouse stops moving.
This only happens with a gaming mouse with a driver on macOS (tested it with a Rocket-Kone XTD and a Razer gaming mouse on two computers). When I use other mice everything works fine.
The gaming mice also causing javax.swing.Timer
s to stop calling their actionPerformed()
methods. I opened a thread here for this problem, but this can be solved using java.util.TimerTask
instead. (Edit: Actually TimerTask also don't fix it because the JFrame doesn't repaint until the mouse stops moving.)
But I found no alternative for animating GIFs. I'm more interested to solve the problem instead of using alternatives though I would be thankful for an working alternative too.
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);
}
}
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);
}
}
I solved the problem as I reduced the polling-rate of my mouse from 1000Hz to 500Hz. Now everything works perfect. I think the problem was that the UI-Thread was overextended handling the 1000 polls per second so it was to busy to animate the GIF.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With