Wednesday, February 21, 2007

java and animate gif

Recently, I got a question about how to draw an animated gif using java. Drawing an image is not trivial in Java, you can use graphics.drawImage, but to draw an animated gif?

I look up in javadoc, and curious about ImageObserver, well I never use this object,  and always pass a null object when  drawing an image. Apperently ImageObserver object has a ImageUpdate method, so I tried when drawing animated gif, I specified and extend the ImageUpdate method.
Whoalla, the fact is when an image need to update (change the frame in animated gif), it will call imageUpdate method, so to refresh the image, the hook is in ImageObsert imageUpdate.

perhaps the code is

public void paint(Graphics g)
  {
    g.drawImage(img,0,0,this); 
  }

  public boolean imageUpdate( Image img, int flags, int x, int y, 
    int w, int h ) 
  {
   
    repaint();
    return true;
  }

1 comment:

Anonymous said...

You write very well.