Replay a non-looping gif image
I've got a non-looping gif that I use as ImageIcon for two JLabels, but not at the same time.
My pr开发者_如何转开发oblem is that when I set the second JLabel's icon to be the gif, the animation has already been played, so it only show the last frame of it.
Do you know a way to get the animation when the gif is set to the second JLabel?
On the newly created icon try using:
icon.getImage().flush();
Ok,
discarded old answer. After some more searching around I found the way to do it.
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()
btw. if you leave the line label1.setIcon(null);
out it will still repeat the animation of the icon only for label2. label1 stays at the last frame.
精彩评论