/////// ImageSequence.java /////// import java.applet.Applet; import java.net.URL; import java.awt.*; public class ImageSequence { public ImageSequence(URL location, String img_file, String ext, int dim, Applet p, Toolkit tk) { pic = new Image[dim]; app = p; n = dim; tracker = new MediaTracker(app); // get annimation frames (id == 0) for (int i = 0; i < n; i++) { pic[i] = (tk != null) ? tk.getImage(img_file+i+ext) : app.getImage(location, img_file+i+ext); tracker.addImage(pic[i], 0); } tracker.checkID(0, true); // starts image loading } protected ImageSequence() {} // gets next frame, cycling back to 1st frame or not public Image next(boolean cycle) { if ( loaded() ) // waits { if ( ! cycle && index == n-1 ) // end reached { index= -1; return null; } index = (index+1) % n; return pic[index]; } return null; // error } public int width() { if ( loaded() ) return pic[0].getWidth(null); else return -1; } public int height() { if ( loaded() ) return pic[0].getHeight(null); else return -1; } private boolean loaded() { if ( ready ) return true; try { tracker.waitForID(0); // loading terminated } catch (InterruptedException e) { ERROR = true; return false; } if ( tracker.isErrorID(0) ) // check for error { ERROR = true; return false; } else return (ready = true); } public static boolean ERROR = false; protected boolean ready=false; protected Applet app; protected Image[] pic = null; protected int n; protected MediaTracker tracker; protected int index=-1; }