
Signature
Steve W. Jackson
Montgomery, Alabama
>>I added an animated GIF to a JLabel's Icon, but the animation doesn't
>>complete a full cycle. It gets through half of the frames and then freezes
[quoted text clipped - 18 lines]
>
> = Steve =
That's not true. If you use java.awt.Toolkit to create an image of an
animated GIF, the image will automatically keep playing (if the GIF
specifies to repeat forever).
Demo:
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class AnimatedGIF {
public static void main(String[] args) throws IOException {
URL animgifURL = new URL(
"http://upload.wikimedia.org/wikipedia/en/b/be/Mashimaro.gif");
Image animgifImg =
Toolkit.getDefaultToolkit().createImage(animgifURL);
Icon animgifIco = new ImageIcon(animgifImg);
JLabel animgifLbl = new JLabel();
animgifLbl.setIcon(animgifIco);
animgifLbl.setHorizontalAlignment(JLabel.CENTER);
JLabel copyrightLbl = new JLabel();
copyrightLbl.setText("An animation of Mashimaro");
copyrightLbl.setHorizontalAlignment(JLabel.CENTER);
JPanel jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(animgifLbl, BorderLayout.CENTER);
jContentPane.add(copyrightLbl, BorderLayout.SOUTH);
JFrame app = new JFrame();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setContentPane(jContentPane);
app.pack();
app.setLocationRelativeTo(null); // center on screen
app.setTitle("Animated GIF");
app.setVisible(true);
}
}

Signature
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
Steve W. Jackson - 12 May 2005 16:55 GMT
> >>I added an animated GIF to a JLabel's Icon, but the animation doesn't
> >>complete a full cycle. It gets through half of the frames and then freezes
[quoted text clipped - 68 lines]
> }
> }
I wrote my response based on an experience in our application, in which
animated images did not update without a nudge when we used one in a
JButton. Either we've corrected whatever we did wrong in the time since
that was implemented, or it's been affected by our use of a newer Java
release, or maybe my memory is just faulty. But it does indeed continue
to update on the JButton in our application. Oddly enough, it stops
when rolling the mouse over the button, but that shouldn't be an issue
with the OP's attempted use in a JLabel.
Thanks for the insight.
= Steve =

Signature
Steve W. Jackson
Montgomery, Alabama
Roland - 12 May 2005 17:49 GMT
>>>>I added an animated GIF to a JLabel's Icon, but the animation doesn't
>>>>complete a full cycle. It gets through half of the frames and then freezes
[quoted text clipped - 24 lines]
>>
>>Demo:
[snip]
> I wrote my response based on an experience in our application, in which
> animated images did not update without a nudge when we used one in a
[quoted text clipped - 8 lines]
>
> = Steve =
I've noted that if you use ImageIO to create an image of an animated
GIF, the animation does not work (I think it only displays the first
frame of the GIF).
Using Toolkit creatImage or getImage, the animation is handled correctly
(at least on Windows platform, using the sun.awt.windows.WToolkit
implementation). Maybe there are issues with the Toolkit implementations
on other platforms.
To OP: verify that your GIF does indeed repeat.

Signature
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
Bud Curtis - 13 May 2005 04:55 GMT
Thank you, Roland, for the example. I tried your example, and your URL GIF
works fine. I then tried several of my animated GIFS and found a real
mixture of display results. The GIF I wanted to use did exactly what I was
experiencing witrh the original code (stoped after the first few frames).
One of the five GIFs worked just like I wanted. A couple GIFs animated in a
very jerky repeated fashion.
Is there something special about each of these GIFS? Each was loaded into
GIF Movie Gear (an animated GIF editor) and all animated just fine. Can you
suggest some thing I should look at.

Signature
Bud Curtis
Colorado Springs, CO
>
> >>I added an animated GIF to a JLabel's Icon, but the animation doesn't
[quoted text clipped - 69 lines]
> }
> }
Roland - 13 May 2005 08:29 GMT
> Thank you, Roland, for the example. I tried your example, and your URL GIF
> works fine. I then tried several of my animated GIFS and found a real
[quoted text clipped - 6 lines]
> GIF Movie Gear (an animated GIF editor) and all animated just fine. Can you
> suggest some thing I should look at.
You could try to display them in another image viewer (e.g. IrfanView)
or in your browser, and see how they behave in them.

Signature
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
Bud Curtis - 14 May 2005 16:51 GMT
I tried IrfanView and all of them display fine.

Signature
Bud Curtis
Colorado Springs, CO
>
> > Thank you, Roland, for the example. I tried your example, and your URL GIF
[quoted text clipped - 10 lines]
> You could try to display them in another image viewer (e.g. IrfanView)
> or in your browser, and see how they behave in them.