A thread is an independent unit of execution in a program. Let's take a look at the SineWaveform applet.
A button is used to stop and start the thread, which controls the animation. Each animation frame is created as an offscreen image, and then the image is shown on the screen. This is called double buffering, and is one method of avoiding flicker on the screen. When an image is drawn on the screen and takes too much time to render, flicker can result. In double buffering, the image is prepared before-hand in order to reduce the time that it takes to put it on the screen. The SineWaveform applet cycles through eight images.
When the repaint() method is called in a program, the default action is for it to first cover the screen with the background color in order to erase it, and then to call the paint(Graphics g) method. This is one reason why a time-consuming paint(Graphics g) method can cause flickering. The screen changes from the previous image to a background color, and then the new image must be constructed. However, our applet overrides the default update(Graphics g) method, and does not erase the screen.
If you examine the source code for SineWaveform, you will notice that all of the work of creating the offscreen image is handled by update(Graphics g). Once the image is ready, all the paint(Graphics g) method needs to do is put it on the screen.
The code that handles button clicks resides in handleEvent(Event e). Be informed, however, that this method is being replaced by other event-handling methods in JDK 1.1.1, the most recent release of Sun's Java Development Kit. In Sun's words, the handleEvent(Event e) method has been deprecated. I have used the deprecated method here because browsers like Netscape have not yet incorporated the new JDK. Undoubtedly, these browsers will be updated in the near future.
By changing the parameters in the applet tag, we can use SineWaveform to create a variety of wave motions. With horizontalradius set to 0 and verticalradius set to 8, we create a shear wave.
If we set horizontalradius to 8 and verticalradius to 0, we have a compressional wave.
Last modified May 7, 1997