- Animating a Sequence of Images
- Sending Parameters to the Applet
- Workshop: Follow the Bouncing Ball
- Summary
- Q&A
- Quiz
- Activities
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 file names 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" width=215 height=298> 2: <param name="image0" value="lh0.gif"> 3: <param name="image1" value="lh1.gif"> 4: <param name="image2" value="lh2.gif"> 5: <param name="image3" value="lh3.gif"> 6: <param name="pause" 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 book's Web site at http://www.java24hours.com.
You can also specify any of your own .GIF or .JPG 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.
NOTE
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
Figure 24.1 shows one of the four images being displayed as the applet runs.
Figure 24.1 Animating GIF files in an applet.
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.