Java Graphics  «Prev  Next»

Developing a slide show applet- Exercise

Objective: Develop an interactive slide show applet that allows the user to flip through multiple images.

Instructions

Create an interactive applet named SlideShow that will allow the user to flip through a series of images much like the slides in a slide projector.
To keep things simple, the user will move to the next slide by simply clicking the mouse button.
The SlideShow applet should accept the number of slides and the base image name of the slides as parameters.
Here are the design steps for the SlideShow applet:
  1. Create member variables named slides and curSlide that contain an array of Image objects and the index of the current slide, respectively
  2. Get the number of slides as an applet parameter named numSlides
  3. Get the base name of the images as an applet parameter named imgBase
  4. Using the base name and a counter, load the images into the slides array
  5. Draw the current slide image in the paint() method
  6. Increment the curSlide index and repaint the applet window in response to mouse clicks

Source files We have supplied the source files in the download file available from the Sitemap page.The specific files for this exercise can be found in the 03-07 folder.

Hints

The images for the slide show are described as applet parameters, which makes the applet very flexible. Since most slide images are photographs, it's okay to assume that the images are stored in the JPEG image format.
This assumption makes it possible to rely solely on an image base name to describe all of the images in a slide show. For example, an image base name might be "Vacation". If there are 3 slides in the slide show, then the images.Vacation1.jpg, Vacation2.jpg, and Vacation3.jpg are used.
Before you get started, there is one part of this applet that you are not prepared to develop. That part is the mouse click code, which involves handling events. To register the mouse event handler, the following line of code should be placed in the applet's init() method:
addMouseListener(new MouseHandler());

In addition, the following mouse click handler code should be placed in the applet class definition:
class MouseHandler extends MouseAdapter {
  public void mouseClicked(MouseEvent e) {
    // Move to the next slide
    curSlide = (curSlide + 1) % slides.length;
    // Draw the next slide
    repaint();
  }
}


Placing the class declaration of MouseHandler inside the applet makes MouseHandler an inner class of the applet.
This allows the mouseClicked() method of MouseHandler to access the instance methods and variables of the applet.
This code (included in the 03-07 folder as Mousecode.txt) handles the details of incrementing the current slide index and repainting the applet window. You also need to import the java.awt.event package in order for the mouse event handling code to compile properly.
In this exercise you will need to convert the numSlides from a String to an int. The static method parseInt() in class Integer provides this functionality. Here's an example of parseInt() in action:
String s = new String("123");
int    n = Integer.parseInt(s);

You should have the knowledge and skills to finish the rest of the applet on your own.

Exercise scoring

The full credit for this exercise is 15 points. To receive full credit, you will need to successfully create the source code for the applet.

What to submit

In the text box below, cut and paste the source code for the SlideShow applet. Click, Submit to submit the code.