Hello, this is related to the last thread I posted, but it is also different. I found something about ImageObserver with a google search and can't find much on it, I tried doing what the site on google did, but it didn't work with the simple test I wrote.
here's the test:

import java.awt.* ;
import java.awt.event.* ;
import java.awt.geom.* ;
import javax.swing.* ;
import javax.swing.event.* ;
import java.awt.image.* ;
import java.io.* ;
import javax.imageio.* ;
public class GifTest extends JFrame {

	public GifTest ( ) {
		setSize ( 400 , 400 ) ;
		this.add(new GifTest2());
		this.setBackground ( new Color ( 0 , 0 , 0 ) ) ;
		this.setVisible ( true ) ;
		repaint ( ) ;
	}

	public static void main ( String [ ] args ) {
		new GifTest ( ) ;
	}
}
import java.awt.* ;
import java.awt.event.* ;
import java.awt.geom.* ;
import javax.swing.* ;
import javax.swing.event.* ;
import java.awt.image.* ;
import java.io.* ;
import javax.imageio.* ;
import java.util.* ;

/**
 * Write a description of class GifTest2 here.
 * @author (your name)
 * @version (a version number or a date)
 */
public class GifTest2 extends JComponent implements MouseListener, ImageObserver {
    Image img = null ;
    ArrayList < Integer > xs = new ArrayList < Integer > ( ) ;
    ArrayList < Integer > ys = new ArrayList < Integer > ( ) ;
    int cx;
    int cy;

    public void mouseClicked ( MouseEvent e ) {
       xs.add(e.getX());
       ys.add(e.getY());
       cx=e.getX();
       cy=e.getY();
       repaint();
    }

    public void mousePressed ( MouseEvent e ) {}

    public void mouseReleased ( MouseEvent e ) {}

    public void mouseExited ( MouseEvent e ) {}

    public void mouseEntered ( MouseEvent e ) {}
    /**make a new GifTest*/
    public GifTest2 ( ) {
        addMouseListener(this);
        try {
            img = ImageIO.read ( new File ( "gifTest2.gif" ) ) ;
        } catch ( IOException ioe ) {
            System.out.println ( ioe ) ;
        }
        xs.clear();
        ys.clear();
        setBackground ( new Color ( 0 , 0 , 0 ) ) ;
        repaint ( ) ;
    }

    public void paint ( Graphics g ) {
        g.setColor ( new Color ( 0 , 0 , 0 ) ) ;
        g.fillRect ( 0 , 0 , getWidth ( ) , getHeight ( ) ) ;
        for ( int i =0;i<xs.size();i++ ) {
            g.drawImage ( img , xs.get(i) , ys.get(i) , this ) ;
        }
    }
    public boolean imageUpdated(Image img, int infoflags, int x, int y, int w, int h){
        repaint();
        return true;
    }
}

why doesn't this work to animate the GIF, what am I misunderstanding?

Recommended Answers

All 2 Replies

Ok, two things:
1) Load the Image through an ImageIcon

img = new ImageIcon("gifTest2.gif").getImage() ;

ImageIO.read() returns a BufferedImage, which doesn't update the ImageObserver. I couldn't tell you the exact reason why because I don't really mess with images much, but perhaps an animated gif needs a VolatileImage instead of the BufferedImage the read methods gives you.

2) Make sure your signature matches when you override a method. Instead of imageUpdated(), you need

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

thanks for the reply, no one else seems to not know, I did however find a way to do it, I found it while searching google, http://www.velocityreviews.com/forums/t147024-accessing-animated-gifs-with-imageio.html came up with a solution, after you get this,

import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.stream.*;

public class Main {
public static void main(String[] args) throws IOException {
Object input = new File("animated.gif");
// or Object input = new FileInputStream("animated.gif");
ImageInputStream stream = ImageIO.createImageInputStream(input);
Iterator readers = ImageIO.getImageReaders(stream);
if (!readers.hasNext())
throw new RuntimeException("no image reader found");
ImageReader reader = (ImageReader) readers.next();
reader.setInput(stream); // don't omit this line!
int n = reader.getNumImages(true); // don't use false!
System.out.println("numImages = " + n);
for (int i = 0; i < n; i++) {
BufferedImage image = reader.read(i);
System.out.println("image[" + i + "] = " + image);
}
stream.close();
}
}

store all the images in an array, and use one per frame...

but thanks for your help anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.