Return to article

Displaying a Sequence of Images: Example8Applet


The code below shows the important methods that we had to update to use one image for each frame of the animation. The code for the entire applet can be found in Example8Applet.java.

    Image frames[];

    /**
     * Initialize the applet and compute the delay between frames.
     */
    public void init() {
	String str = getParameter("fps");
	int fps = (str != null) ? Integer.parseInt(str) : 10;
	delay = (fps > 0) ? (1000 / fps) : 100;

	frames = new Image[10];
	for (int i = 1 ; i <= 10 ; i++) {
	    frames[i-1] = getImage(getCodeBase(), "duke/T" + i + ".gif");
	}
    }

    /**
     * Paint the previous frame (if any).
     */
    public void paint(Graphics g) {
	update(g);
    }

    /**
     * Paint a frame of animation.
     */
    public void paintFrame(Graphics g) {
	g.drawImage(frames[frame % 10], 0, 0, null);
    }