I need to create a class AnimatedImage that will take an array of Image objects and display them in a round robin fashion given some time interval.
The idea is to have Swing components use this AnimatedImage class the same way they use regular BufferedImage to display images. The AnimatedImage will switch actual Images in a round-robin fasion and the swing components will be notified to display the next actual image.
My problem is that I can't find a way to notify the swing component that AnimatedImage has switched the actual Image.
I hope you can understand what I mean.
Any help will be appreciated!
What I have done so far:
public class AnimatedImage extends BufferedImage
{
private final Image[] images;
private int imageIndex = 0;
public AnimatedImage(Image[] images, int interval)
{
super(images[0].getWidth(null), images[0].getHeight(null), BufferedImage.TYPE_INT_ARGB);
this.images = images;
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
iterate();
}
};
Timer timer = new Timer(interval, listener);
timer.start();
}
private void iterate()
{
imageIndex = imageIndex + 1 >= images.length ? 0 : imageIndex + 1;
}
public float getAccelerationPriority()
{
return images[imageIndex].getAccelerationPriority();
}
public ImageCapabilities getCapabilities(GraphicsConfiguration gc)
{
return images[imageIndex].getCapabilities(gc);
}
public Image getScaledInstance(int width, int height, int hints)
{
return images[imageIndex].getScaledInstance(width, height, hints);
}
@Override
public Graphics getGraphics()
{
return images[imageIndex].getGraphics();
}
@Override
public int getHeight(ImageObserver observer)
{
return images[imageIndex].getHeight(observer);
}
@Override
public Object getProperty(String name, ImageObserver observer)
{
return images[imageIndex].getProperty(name, observer);
}
@Override
public ImageProducer getSource()
{
return images[imageIndex].getSource();
}
@Override
public int getWidth(ImageObserver observer)
{
return images[imageIndex].getWidth(observer);
}
}