Java Reference
In-Depth Information
Figure 8.7
Output of the
DataSyncApplet
program
with
Sensor
and
DataGetter
classes.
floor and gradually comes to a rest (see Figure 8.8). The interface consists of a
subclass of
JPanel
called
Drop2DPanel
and a button to initiate a new drop of
the ball.
Drop2DPanel
displays the ball and calculates its position.
The applet implements
Runnable
and in the
start()
method it creates
a thread to which it passes a reference to itself. The applet's
run()
method
first does some initialization and then enters a loop that draws each frame of the
animation. The loop begins with a 25 millisecond pause using the static
sleep()
method in the
Thread
class. Then the
Drop2DPanel
is told to paint the next
frame. If the drop is done, the process jumps from the loop and exits the
run()
method, thus killing this thread.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Figure 8.8
The
Drop2DApplet
program
demonstrates how to
create a simple
animation by simulating
a dropped ball that
bounces when it hits the
floor and gradually
comes to rest.
/** This applet implements Runnable and uses a thread
*tocreate a simple dropped ball demonstration.**/
public class
Drop2DApplet
extends JApplet
implements Runnable, ActionListener
{
// Will use thread reference as a flag
Search WWH ::
Custom Search