< Ch. 9 Lab - The Slide Show Applet

Ch9 Lab: Slide Show Applet

Authors: Ralph Morelli and Bronzell Dinkins
Trinity College, Hartford, CT
For use with Chapter 9

For Instructors

This is a short lab that is suitable for Chapter 9. It uses an array to store images which are then displayed in sequence as a slideshow in a JApplet applet.

Objectives

The objectives of this lab are:

Problem Statement: Slide Show Applet

Design a Java applet that displays a set of slides. You will be provided with several GIF files that store the images that make up the slide show. The JApplet interface should provide next and previous buttons that allow the user to display the next or previous slides. These buttons need to be added to the content pane of the applet.

Wrap around: If there are N images, numbered 0 to N-1, and if the present image is N-1, then if the user hits the Next button, the program should display image number 0. Use an array to store the images when they are loaded into the applet.

Downloads

Download the following images and save them in the folder for this lab. To download, just click on the link. When the image is displayed in you browser, choose the Save As item in the File menu.

Design Specifications

You need only 1 class for this problem, one that extends JApplet. Your class should implement the init() and paint() methods of JApplet class. Some GUI techniques are needed that have not been covered in the text up to this point. For the most part you can model your applet after the PongApplet class shown on page 699 of Java, Java, Java, 3E except that the class should implement the ActionListener interface rather than the KeyListener interface. The getImage() method of JApplet can be used to download images from the directory where the class code for the applet resides. The statement
Image myImage = getImage(getCodeBase(), "demo2.gif");
assigns the image in file named demo2.gif to the variable myImage.

The paint() method should be implemented to display the images. The drawImage() method of the Graphics class can be used to the display the image in the applet. The statement
g.drawImage(myImage, 0, 100, this);
will display the image with upper left corner of the image located at left edge of the applet region 100 pixels down from the upper edge of the applet region. The fillRect() statement can be used to erase a previous image before displaying the current image as is done in the PongApplet example. Care should be taken to avoid erasing the area of the applet where the two buttons are displayed. Designing an applet using both Graphics drawing commands directly in the applet content pane and also buttons may cause some display problems and inconsistencies between platforms. However this design should suffice to demonstrate a practical use of arrays.

The actionPerformed() method should call repaint() each time the user clicks on one of the buttons. repaint() should also be called at the end of the init() method.

Hand in

Hand in your appropriately documented source code. For documentation guidelines, see Documentation Specifications.

You're done. Great work!