Hi,
Here is the sample code with media tracker class.
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class MultiFrameAnimation extends Applet
implements Runnable, MouseListener {
private static final int FRAMES_PER_SECOND = 10;
private static final int NBR_FRAMES = 10;
private int frameNumber = -1;
private int delay = 0;
private Thread animatorThread = null;
private boolean isFrozen = false;
private Dimension offDimension = null;
private Image offImage = null;
private Graphics offGraphics = null;
private Image[] images = new Image[NBR_FRAMES];
private MediaTracker gallery = null;
public void init() {
setBackground(Color.white);
gallery = new MediaTracker(this);
delay = 1000 / FRAMES_PER_SECOND;
for (int index = 1; index <= NBR_FRAMES; index++) {
images[index - 1] = getImage(getCodeBase(), "T" + index + ".gif");
gallery.addImage(images[index - 1], 0);
}
addMouseListener(this);
}
public void start() {
if (!isFrozen) {
if (animatorThread == null)
animatorThread = new Thread(this);
animatorThread.start();
}
}
public void stop() {
animatorThread = null;
offGraphics = null;
offImage = null;
}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mousePressed(MouseEvent event) {
if (isFrozen) {
isFrozen = false;
start();
} else {
isFrozen = true;
animatorThread = null;
}
}
public void run() {
try {
gallery.waitForAll();
} catch (InterruptedException e) {
System.out.println(e);
}
while (Thread.currentThread() == animatorThread) {
frameNumber++;
repaint();
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
break;
}
}
}
public void paint(Graphics g) {
Dimension display = getSize();
if (!gallery.checkAll()) {
g.clearRect(0, 0, display.width, display.height);
g.drawString("Loading images...", 0, display.height / 2);
return;
}
if ((offGraphics == null) || (display.width != offDimension.width)
|| (display.height != offDimension.height)) {
offDimension = display;
offImage = createImage(offDimension.width, offDimension.height);
offGraphics = offImage.getGraphics();
}
offGraphics.setColor(getBackground());
offGraphics.fillRect(0, 0, display.width, display.height);
offGraphics.setColor(Color.black);
offGraphics.drawImage(images[frameNumber % 10], 0, 0, this);
g.drawImage(offImage, 0, 0, this);
}
public void update(Graphics g) {
paint(g);
}
}
When multiple Image objects are required, using the imageUpdate method to track loading progress becomes overly complex
The MediaTracker class has been provided as a collection class for Image objects, and allows easy tracking of loading progress for any one or all Image objects belonging to the
MediaTracker.
Once added to a MediaTracker, the loading progress of Image objects can be monitored with methods such as waitForAll and checkAll, both of which return true if image
loading has completed
The animation is performed in the run method of a Thread object to allow other processing to continue
The stop and start methods halt and restart the animation whenever the page containing the Applet is no longer active
The Image objects are rendered on off-screen buffers and then transferred to the screen for the appearance of instant updating
The update method is overridden to just invoke the paint method - the screen clearing is now performed in paint
Also please refer this URL
http://www.csse.monash.edu.au/courseware/cse3420/Lectures/Module8/module.html
I hope this will help you.
Thanks
Bakrudeen