[ Team LiB ] Previous Section Next Section

16.3 A Clock Applet

Example 16-2 is an applet that displays the current time, as shown in Figure 16-2, and updates it once every second. Unlike Example 16-1, which defines a paint( ) method and does its own text drawing with Graphics.drawString( ), this example uses a java.awt.Label component to do the drawing. While it is common for applets to do their own drawing with a paint( ) method, it is also important to remember that applets extend java.awt.Panel and can contain any type of GUI components. Clock defines an init( ) method that creates and configures the Label component.

Figure 16-2. A clock applet
figs/Jex3_1602.gif

In order to update the time every second, Clock implements the Runnable interface and creates a Thread that runs the run( ) method. The applet's start( ) and stop( ) methods are invoked by the browser when the applet becomes visible or is hidden; they start and stop the thread. (Although the example is written to use Java 1.1, it does not rely on the Thread.stop( ) method, which was deprecated in Java 1.2.)

Finally, the Clock applet implements getAppletInfo( ) to provide information about the applet. Sun's appletviewer tool is able to display this information, but most web browsers don't.

Example 16-2. Clock.java
package je3.applet;
import java.applet.*;         // Don't forget this import statement!
import java.awt.*;            // Or this one for the graphics!
import java.util.Date;        // To obtain the current time
import java.text.DateFormat;  // For displaying the time

/** 
 * This applet displays the time, and updates it every second 
 **/
public class Clock extends Applet implements Runnable {
    Label time;               // A component to display the time in
    DateFormat timeFormat;    // This object converts the time to a string
    Thread timer;             // The thread that updates the time
    volatile boolean running; // A flag used to stop the thread

    /**
     * The init method is called when the browser first starts the applet.
     * It sets up the Label component and obtains a DateFormat object
     **/
    public void init( ) {
        time = new Label( );
        time.setFont(new Font("helvetica", Font.BOLD, 12));
        time.setAlignment(Label.CENTER);
        setLayout(new BorderLayout( ));
        add(time, BorderLayout.CENTER);
        timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);
    }

    /**
     * This browser calls this method to tell the applet to start running.
     * Here, we create and start a thread that will update the time each
     * second.  Note that we take care never to have more than one thread
     **/
    public void start( ) {
        running = true;                // Set the flag 
        if (timer == null) {           // If we don't already have a thread
            timer = new Thread(this);  // Then create one
            timer.start( );             // And start it running
        }
    }

    /**
     * This method implements Runnable.  It is the body of the thread.  Once a
     * second, it updates the text of the Label to display the current time.
     * AWT and Swing components are not, in general, thread-safe, and should
     * typically only be updated from the event-handling thread. We can get
     * away with using a separate thread here because there is no event
     * handling in this applet, and this component will never be modified by
     * any other thread.
     **/
    public void run( ) {
        while(running) {     // Loop until we're stopped
            // Get current time, convert to a String, and display in the Label
            time.setText(timeFormat.format(new Date( )));  
            // Now wait 1000 milliseconds
            try { Thread.sleep(1000); }
            catch (InterruptedException e) {  }
        }
        // If the thread exits, set it to null so we can create a new one
        // if start( ) is called again.
        timer = null;
    }

    /**
     * The browser calls this method to tell the applet that it is not visible
     * and should not run.  It sets a flag that tells the run( ) method to exit
     **/
    public void stop( ) { running = false; } 

    /**
     * Returns information about the applet for display by the applet viewer
     **/
    public String getAppletInfo( ) {
        return "Clock applet Copyright (c) 2000 by David Flanagan";
    }
}
    [ Team LiB ] Previous Section Next Section