Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Working with Graphics

Displaying a Sequence of Images

The example in this section gives you the basics of displaying an image sequence. The next section has hints for improving the appearance and performance of this animation. This section shows only applet code. The code for an application would be similar, except you would use different code to load the images, as described in Loading Images.

Below are the ten images this applet uses.

T1.gif: T2.gif: T3.gif: T4.gif: T5.gif:
T6.gif: T7.gif: T8.gif: T9.gif: T10.gif:

Here's a picture of what the applet looks like. Remember that you can click on the applet to stop or start the animation.

Click this figure to run the applet.
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.
[PENDING: that image should have a gray background]

The code for this example, in ImageSequenceTimer.java, is even simpler than for the previous example, which moved an image. Here is the code that differs significantly from the moving image example:

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

. . .//In the paintComponent method:
g.drawImage(images[ImageSequenceTimer.frameNumber % 10],
            0, 0, this);
Another way to implement this example would be to use a label to display the image. Instead of having custom painting code, the program would use the setIcon method to change the image displayed.


Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Working with Graphics