18. Sockets and Networking

18.4. The Slide Show Program

Problem Specification

Let’s suppose our slide show will repeatedly display a set of images named “slide0.gif,” “slide1.gif,” and “slide2.gif.” Suppose these images are stored on a Web site on www.cs.trincoll.edu and are stored in a directory named /˜ram/jjj/slideshow. This means our program will have to load the following three URLs:

,,

 

 

 

J

We want our show to cycle endlessly through these images, leaving about 5 seconds between each slide.

User Interface Design

The user interface for this program is graphical, but it doesn’t contain any GUI components. It just needs to display an image every 5 seconds. It can use a simple paint() method to display an image each time it is repainted:

,,

 

 

 

J

The assumption here is that the currentImage instance variable will be set initially to null. Each time an image is downloaded, it will be set to refer to that image. Because paint() is called before the program starts downloading the images, it is necessary to guard against attempting to

draw a null image, which would lead to an exception.

Problem Decomposition

 

Figure 15.10: The SlideShow- Frame downloads and displays the images.


One problem we face with this program is getting it to pause between fig- p830f2 each slide. One way to do this is to set up a loop that does nothing for about 5 seconds:

,,

 

 

 

 

 

 

 

 

Timer

-frame : SlideShowFrame

+ Timer(in a : SlideShowFrame)

+ run()

 

FIGURE 15.11 The Timer class delays the frame thread between each slide.


J

However, this isn’t a very good solution. As we saw in Chapter 14, this is a form of busy waiting that monopolizes the CPU, making it very diffi- cult to break out of the loop. Another problem with this loop is that we don’t really know how many iterations to do to approximate 5 seconds of idleness.

 

A much better design would be to use a separate timer thread, which can sleep() for 5 seconds between each slide. So our program will have two classes: one to download and display the slides and one to serve as a timer (Figs. 15.10 and 15.11). ( Java Swing has a Timer class that works a little bit differently, see the javadoc for a description.)

 

SlideShowFrame—This JFrame subclass will take care of download- ing and displaying the images and starting the timer thread.

Timer—This class will implement the Runnable interface so that it can run as a separate thread. It will repeatedly sleep for 5 seconds and then tell the frame to display the next side.

 

 

The SlideShowFrame class

What should we do with the images we download? Should we repeatedly download and display them, or should we just download them once and store them in memory? The second of these alternatives seems more effi- cient. If an image has already been downloaded, it would be wasteful to download it again.

 

So we’ll need an array to store the images. Our slide show will then

consist of retrieving the next image from the array and displaying it. To What data do we need?

help us with this task, let’s use a nextImg variable as an array index to keep track of the next image. Even though it isn’t absolutely necessary, we could use a third variable here, currentImage, to keep track of the cur- rent image being displayed. Thus, our frame needs the following instance variables:

,,

 

 

 

J

Given these variables, let’s now write a method to take care of choos- Method design

ing the next slide. Recall that the paint() method is responsible for dis- playing currentImage, so all this method needs to do is to update both currentImage and nextImg. This method should be designed so that it can be called by the Timer thread whenever it is time to display the next slide, so it should be a public method. It can be a void method with no

 

parameters, because the frame already contains all the necessary informa- tion to display the next slide. Thus, there’s no need for information to be passed back and forth between Timer and this method:

,,

 

 

 

J

The method’s algorithm is very simple. It sets currentImage to what- ever slide is designated by nextImg and it then updates nextImg’s value. Note here the use of modular arithmetic to compute the value of nextImg. Given that NIMGS is 3, this algorithm will cause nextImg to take on the repeating sequence of values 0, 1, 2, 0, 1, 2, and so forth. Finally, the method calls repaint() to display the image.

 

 

 

 

 

 

The frame’s constructor, SlideShowFrame() method will have two tasks:

 

 

Download and store the images in slide[].

Start the Timer thread.

 

As we discussed, downloading Web resources for an application requires the use of the javax.imageio.ImageIO.read() method. Here we just place these method calls in a loop:

,,

 

 

J

Note here how we convert the loop variable k into a String and con- catenate it right into the URL specification. This allows us to have URLs containing “slide0.gif,” “slide1.gif,” and “slide2.gif.” This makes our pro- gram easily extensible should we later decide to add more slides to the

 

show. Note also the use of the class constant NIMGS as the loop bound. This too adds to the program’s extensibility.

 

The task of starting the Timer thread involves creating an instance of the Timer class and calling its start() method:

,,

 

 

 

Note that Timer is passed a reference to this frame. This enables Timer to call the frame’s nextSlide() method every 5 seconds. This program- ming technique is known as callback and the nextSlide() method is an example of a callback method (Fig. 15.12).

 

 

This completes our design and development of SlideShowFrame, which is shown in Figure 15.13.

The Timer Class

The Timer class is a subclass of Thread, which means it must implement the run() method. Recall that we never directly call a thread’s run() method. Instead, we call its start() method, which automatically calls run(). This particular thread has a very simple and singular function. It should call the SlideShowFrame.nextSlide() method and then sleep for 5 seconds. So its main algorithm will be:

,


J

 

 

 

 

 

 

 

1: start() nextSlide()

 

: SlideShowFrame

 

: Timer

 

 

 

 

 

Figure 15.12: Timer uses the nextSlide() method to call back the frame to remind it to switch to the next slide.

The timer thread

,

 

 

 

 

J

However, recall that Thread.sleep() throws the Interrupted- Exception. This means that we’ll have to embed this while loop in a try/catch block.

To call the frame’s nextSlide() method, we also need a reference to the SlideShowFrame, so we need to give it a reference, such as an instance variable, as well as a constructor that allows the frame to pass Timer a reference to itself.

Given these design decisions, the complete implementation of Timer is shown in Figure 15.14. To see how it works, download it from the Java, Java, Java Web site and run it.

 

,,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

J

Figure 15.13: The SlideShowFrame class.

 

SELF-STUDY EXERCISE

EXERCISE 15.6 Describe the design changes you would make to SlideShowFrame if you wanted to play a soundtrack along with your slides. Assume that the sounds are stored in a sequence of files, “sound0.au,” sound1.au,” and so forth, on your Web site.