import java.awt.Graphics;
import java.util.*;
import java.awt.*;

/********************************************************************
 *
 * This extension to the Applet class tells you what time it is.
 *
 * The time is displayed in a 24 point Bold Arial font, and is
 * updated every second (so the display may occasionally skip
 * a beat).
 *
 * The applet is an example used in a presentation on Java
 * programming.
 *
 * @author  Dave Wylie
 * @version 1.0, 31-Jul-1996
 *
 ********************************************************************/
public class Clock extends java.applet.Applet implements Runnable
{
   Thread clockThread;     // Thread that handles the display

   /*****************************************************************
    *
    * Main entry point for our simple Applet. All we do is make
    * ourself visible
    *
    *****************************************************************/
   public void init()
   {
      // Make our Applet visible
      show( );
   }

   /*****************************************************************
    *
    * Called each time the browser displays the page.
    * If we haven't got a running thread, then create one,
    * and start the new thread running.
    *
    *****************************************************************/
   public void start()
   {
      if( clockThread == null )
      {
        clockThread = new Thread( this );
        clockThread.start();
      }
   }

   /*****************************************************************
    *
    * Called each time the browser leaves the page.
    * Stop and junk any thread that may be running.
    *
    *****************************************************************/
   public void stop()
   {
      if( clockThread != null )
      {
         clockThread.stop();
         clockThread = null;
      }
   }

   /*****************************************************************
    *
    * The main thread of execution.
    * While we have a running thread, update the display,
    * sleep for the specified time and go round again.
    *
    *****************************************************************/
   public void run()
   {
      // Keep going until the end of time itself
      while( true )
      {
         // Force our display area to be re-painted
         // This will result in a call to our "update" method.
         repaint();

         // It is possible that our attempt to sleep may fail
         try
         {
            // Sleeping is part of the Thread class, but we can
            // call the static "sleep" method even when we have
            // not actually created any thread objects.
            Thread.sleep( 1000 );
         }
         catch (InterruptedException e)
         {
            // If the sleep failed, then log the problem on
            // the Java console.
            System.out.println( "Exception:  " + e );
         }
      }
   }

   /*****************************************************************
    *
    * Display update method.
    *
    * This applet overrides "update" (rather than providing a
    * "paint" method) because we want to clear our background
    * to a non-default colour. By doing this directly here,
    * we avoid having our background cleared for us and
    * so the clock display shouldn't flicker so much.
    *
    * @param g     Graphics context we are to display the time in
    *
    *****************************************************************/
   public void update( Graphics g )
   {
      // Declare a Date object, and create a new one that
      // contains the current date and time.
      Date now = new Date();

      // Declare a Font object, and create a new one that
      // is Big and Bold.
      Font bigFont = new Font("Arial", Font.BOLD, 24 );

      // Declare a string to contain the time we will display
      String theTime;

      // Build up a time string in HH:MM:SS format,
      // including leading zeros where necessary.
      theTime = LeadingZero( now.getHours()   ) + ":"
              + LeadingZero( now.getMinutes() ) + ":"
              + LeadingZero( now.getSeconds() );

      // Clear the background to dark blue (we create
      // a new color of our own, specifying Red, Green and
      // Blue values because the Color class doesn't have
      // my favourite shade in it)
      g.setColor( new Color( 0, 0, 127 ) );
      g.fillRect( 0, 0, size().width, size().height );

      // Display the time in big green numbers (this time we
      // use the normall green defined in the Color class)
      g.setFont( bigFont );
      g.setColor( Color.green );
      g.drawString( theTime, 0, size().height );
   }

   /*****************************************************************
    *
    * Utility routine to convert an int to a string, adding a
    * leading zero if the value is less than ten.
    *
    * Note that the conversion from int to String is done by
    * using the "+" operator to append the int to a constant
    * string. This is the one time in Java where an operator
    * appears to be overloaded. However, it is really just
    * acting as a string concatenation operator - an implicit
    * call to the toString() method is done on the int before
    * the operation is performed.
    *
    * @param   value The number to be converted
    * @return        String representation of the number
    *
    *****************************************************************/
   public String LeadingZero( int value )
   {
      String retValue;           // To hold the converted number

      // Do we need a leading zero?
      if( value < 10 )
      {
         // Yes - add one.
         retValue = "0" + value;
      }
      else
      {
         // No - force conversion to a string by "adding" the
         //      value to an empty string.
         retValue = "" + value;
      }

      // Pass back the String we created.
      return( retValue );
   }
}

