GIF stops animation when moving the mouse

While I move my gaming mouse inside javax.swing.JFrame , all animated GIFs ( javax.swing.ImageIcon inside javax.swing.JLabel ) stop the animation until the mouse stops moving.

This only happens with a gaming mouse with a driver on macOS (tested it with Rocket-Kone XTD and a Razer gaming mouse on two computers). When I use other mice, everything works fine.

Gaming mice also force javax.swing.Timer stop calling their actionPerformed() methods. I opened this topic here for this problem, but this can be solved using java.util.TimerTask . ( Edit: Actually, TimerTask also does not fix it, because the JFrame does not redraw until the mouse stops moving.)

But I did not find an alternative for GIF animation. I'm more interested in solving the problem instead of using alternatives, although I would be grateful for a working alternative.

The code:

 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); } } 

Application Launch:

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("https://i.stack.imgur.com/HXCUV.gif"))); frame.setSize(500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.add(label); } } 
+7
java timer swing animated-gif macos
source share
1 answer

I solved the problem because I reduced the polling speed of my mouse from 1000 Hz to 500 Hz. Now everything is working fine. I think the problem was that the UI-Thread was too expanded to process 1000 polls per second, so it was busy to animate GIFs.

+1
source share

All Articles