Skip to main content

How do I animate a javax.swing.Icon?

2 replies [Last post]
robross
Offline
Joined: 2003-06-13

If I supply an Icon to a JLabel constructor, and that Icon happens to represent an animated GIF, then the JLabel automatically handles animating the frames of the aGIF. So somehow, the Icon class is giving it enough information to be able to support the animation.

Now, say I have a reference to an Icon, and that's it. Not an ImageIcon, just a javax.swing.Icon. How do I get notified to draw the other frames of the animation? Right now, I am creating a BufferedImage, and having the Icon paint itself (via paintIcon) into the Graphics of that BufferedImage. I am passing in an ImageObserver to the paintIcon method (actually it's a Component that has overriden imageUpdate, but that never gets called.

The relevant code:

Image iconImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);

g = (Graphics2D) ((BufferedImage) iconImage).getGraphics();
g.setComposite(AlphaComposite.Src);
icon.paintIcon(observer, g, 0, 0); //observer overrides imageUpate

I want to get notified when I should render the next frame of the animation; I will respond by painting the Icon into a BufferedImage.

But I'm not sure how to do this.

Thanks,

Rob Ross

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
twalljava
Offline
Joined: 2004-07-26
jaz@zukowski.net

The ImageIcon has a setImageObserver method that is utilized. You
would need to do something similar.

Quoting java2d@JAVADESKTOP.ORG:

> If I supply an Icon to a JLabel constructor, and that Icon happens
> to represent an animated GIF, then the JLabel automatically handles
> animating the frames of the aGIF. So somehow, the Icon class is
> giving it enough information to be able to support the animation.
>
> Now, say I have a reference to an Icon, and that's it. Not an
> ImageIcon, just a javax.swing.Icon. How do I get notified to draw
> the other frames of the animation? Right now, I am creating a
> BufferedImage, and having the Icon paint itself (via paintIcon) into
> the Graphics of that BufferedImage. I am passing in an
> ImageObserver to the paintIcon method (actually it's a Component
> that has overriden imageUpdate, but that never gets called.
>
> The relevant code:
>
> Image iconImage = new BufferedImage(icon.getIconWidth(),
> icon.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
>
> g = (Graphics2D) ((BufferedImage) iconImage).getGraphics();
> g.setComposite(AlphaComposite.Src);
> icon.paintIcon(observer, g, 0, 0); //observer overrides imageUpate
>
>
> I want to get notified when I should render the next frame of the
> animation; I will respond by painting the Icon into a BufferedImage.
>
> But I'm not sure how to do this.
>

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".