Repeating image playback without linking gif

I have a non-looping gif that I use as ImageIcon for two JLabels, but not at the same time.

My problem is that when I set the second JLabel icon as a gif, the animation has already been played, so it only displays the last frame.

Do you know a way to get the animation when the gif is set to the second JLabel?

+3
source share
2 answers

On the newly created icon, try using:

icon.getImage().flush();
+3
source

Good,

dropped the old answer. After several searches, I found a way to do this.

ImageIcon icon = ..[the animated gif without looping]..
....
label1.setIcon(icon); //animation plays once
....
// now time to remove icon from label1 and add it to label2
label1.setIcon(null);
icon.getImage().flush(); //reset resource used by the image
label2.setIcon(icon);
....

Java Api: Image # flush ()

. label1.setIcon(null);, 2. label1 .

+1

Source: https://habr.com/ru/post/1722624/


All Articles