Skip to main content

Drawing an animated gif on a bufferedImage

1 reply [Last post]
karys
Offline
Joined: 2008-09-18

Hi !!!

I've another question !!
Is it possible to draw an animated gif on a bufferedImage ? I've tried :

Image im = new ImageIcon("test.gif").getImage() ; // test.gif is an animated gif
...

BufferedImage bi = new BufferedImage(im.getWidth(null), im.getHeigth(null), BufferedImage.type_int_ARGB);
Graphics g = bi.getGraphicss();
g.drawImage(im, 0,0,null);
g.dispose();
...

// method from JPanel
public void paintComponent(Graphics g)
{
g.drawImage(bi, 0,0, null);
}

It doesn't seem to work :(

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Jim Graham

You can try, but it will only draw the "current" frame of the GIF to the
BufferedImage. BufferedImage objects are just static buckets of pixels
so they can't "animate".

The last parameter to the drawImage call is an optional reference to an
ImageObserver that will be notified if/when a new frame in the source
image is ready to be painted (it is then up to you to paint the new
frame wherever you want it using another call to drawImage() - the
notification callback just tells you that it is "ready"). These
callbacks will happen asynchronously based on the thread that is
decoding the GIF image.

Component implements ImageObserver, but it has a very basic
implementation that just repaints the entire component (with a delay for
batching) whenever there is any new data ready to be rendered. You
probably want to provide your own custom implementation of ImageObserver
(it's only 1 method call to implement) that does something more
intelligent about updating your BufferedImage instance when it gets a
FRAMEBITS or ALLBITS notification...

...jim

java2d@JAVADESKTOP.ORG wrote:
> Hi !!!
>
> I've another question !!
> Is it possible to draw an animated gif on a bufferedImage ? I've tried :
>
> Image im = new ImageIcon("test.gif").getImage() ; // test.gif is an animated gif
> ...
>
> BufferedImage bi = new BufferedImage(im.getWidth(null), im.getHeigth(null), BufferedImage.type_int_ARGB);
> Graphics g = bi.getGraphicss();
> g.drawImage(im, 0,0,null);
> g.dispose();
> ...
>
> // method from JPanel
> public void paintComponent(Graphics g)
> {
> g.drawImage(bi, 0,0, null);
> }
>
> It doesn't seem to work :(
> [Message sent by forum member 'karys' (karys)]
>
> http://forums.java.net/jive/thread.jspa?messageID=300130
>
> ===========================================================================
> 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".

===========================================================================
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".