Sending Parameters to the Applet

Because the Animate applet relies on parameters to specify the image files it should display, you need to create a web page containing these filenames before you can test the program. After saving and compiling the Animate.java file, open up a new file in your word processor and call it Animate.html. Enter Listing 24.2 into that file and save it when you're done.

Listing 24.2. The Full Text of Animate.html
1: <applet code="Animate.class" >
2: <param value="lh0.gif">
3: <param value="lh1.gif">
4: <param value="lh2.gif">
5: <param value="lh3.gif">
6: <param value="800">
7: </applet>


This file specifies four image files: lh0.gif, lh1.gif, lh2.gif, and lh3.gif. These files are listed as the values for the parameters image0 through image3. You can find the files used in this example on the Hour 24 page of the tutorial's website at http://www.java24hours.com. You also can specify any of your own GIF or JPEG files, if desired. Whichever files you choose should be placed in the same folder as the Animate.class and Animate.html files. With the pause parameter, you can specify how long the program should pause after each image is displayed.

By the Way

You might be wondering why the files and the parameters are given names that start numbering with 0 instead of 1. This is done because the first element of an array in a Java program is numbered 0. Putting an image0 called lh0.gif into pictures[0] makes it easier to know where these images are being stored.


Once the files have been put in the right place, you're ready to try out the Animate applet. Type the following command to open the page in your web browser and view the applet:

Animate.html


Screenshot shows one of the four images being displayed as the applet runs.

Screenshot Animating GIF files in an applet.

Java ScreenShot


Although this is a simple animation program, hundreds of applets on the web use similar functionality to present a series of image files as an animation. Presenting a sequence of image files through Java is similar to the animated GIF files that are becoming more commonplace on web pages. Although Java applets are often slower to load than these GIF files, they can provide more control of the animation and allow for more complicated effects.

      
Comments