18.8 JAVA THREADS FOR APPLETS


18.8 JAVA THREADS FOR APPLETS

We will illustrate how a thread can be used for animating a Java web page. The idea is to construct an applet that loads in a sequence of images sufficiently rapidly so that an illusion of continuous motion is created.

The following example assumes that we have available to us four GIF images bird0.gif, bird1.gif, bird2.gif, and bird3.gif, corresponding to four consecutive shots of a bird in flight. We wish to create a thread that would load and display these images consecutively by cycling through them. The thread would belaunched by an applet that is called by the following HTML code that resides ina file named Animator.html:

 
<HTML> <TITLE> Animation Applet </TITIE> <BODY BGCOLOR="#000000"> <APPLET CODE="Animator.class" WIDTH=480 HEIGHT=590> <PARAM name=imagename value="bird"> <PARAM name=imagecount value="4"> </APPLET> </BODY> </HTML>

Note the two parameters we pass to the applet: imagename and imagecount. The former is the base name of our "bird" images, and the latter tells the applet how many images it will need to load for animation.

Regarding the code for the applet itself, we will use the data members shown in lines (A) through (E). The data member imageNameBase of line (A) will store the base name of all the image files to be used for animation, and the data member imageCount of line (B) the number of images to be displayed in each cycle of animation. The images will be loaded into the Image[] array imageArr of line (E) whose size will be set to the value of the variable imageCount. The image currently being displayed in the animation sequence will be value of the data member image of type Image in line (D). So, basically, the value of image will sequence through the values in the array imageArr as each image is displayed in the applet. Finally, the variable runner of line (C) will hold a reference to the animation thread.

As was mentioned in Chapter 17, when a web browser launches a new applet for the first time, the initialization code in the init method is automatically executed. Our init method in line (F) first gets the applet parameters in lines (G) and (H), and then it loads in the images as Image objects in line (I). The images are loaded by invoking the getImage method of the Applet class. As described in the previous chapter, the MediaTracker object keeps track of the loading process.

After the init method, Java automatically calls the start method. This method, defined in line (J), is also called whenever a user returns to the page containing this applet. The animation thread is started afresh each time start is invoked, as should be clear from the syntax in lines (K) and (L). When the user goes off the page in which the applet sits, Java automatically calls the stop method defined in line (M). By nulling the object reference held by the variable runner, as we do in line (N), the execution of the thread is stopped.

The animation itself is carried out by calling the paint method defined in line (O). This method is invoked by the call to repaint in line (Q) of the run method defined in line (P). The source code for this example is shown below:

 
//Animator.java import java.applet.*; import java.awt.*; public class Animator extends Applet implements Runnable { private String imageNameBase; //(A) private int imageCount; //(B) private Thread runner; //(C) private Image image = null; //(D) private Image[] imageArr; //(E) public void init() { //(F) imageNameBase = getParameter("imagename"); //(G) imageCount = Integer.parseInt( getParameter("imagecount") ); //(H) imageArr = new Image[ imageCount ]; int i = 0; while (i < imageCount) { String imageName = imageNameBase + i + ".gif"; imageArr[i] = getImage(getDocumentBase(), imageName); //(I) MediaTracker tracker = new MediaTracker(this); tracker.addImage(imageArr[i], 0); try { tracker.waitForID(0); } catch(InterruptedException e) {} i++; } } public void start() { //(J) runner = new Thread(this); //(K) runner.start(); //(L) } public void stop() { //(M) runner = null; //(N) } public void paint(Graphics g) { //(O) if (image == null) return; g.drawImage(image, 100, 100, this); } public void run() { //(P) int i = 0; while (true) { image = imageArr[i]; i = ++i % imageCount; try { Thread.sleep(200); } catch(InterruptedException e){} repaint(); //(Q) } } }




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net