/** Every second update a time display. */

import java.awt.*;
import java.util.*;
import java.applet.*;

public class SimpleClock extends Applet implements Runnable {

  Thread runner = null;

/** To run as an application,
 * construct it and show it in a frame.
 * This must be a static (class not instance) method,
 * because the instance won't exist until we construct it here.
 * When run as an applet,
 * the browser provides the frame,
 * and calls the constructor and init methods. */

  public static void main (String []argv) {

      SimpleClock cd = new SimpleClock ();

      Frame ff = new Frame ("SimpleClock");
      ff.setBounds (100, 100, 200, 50);

      ff.add (cd);
      cd.init ();
      cd.start ();

      ff.show ();
  }

  public void init () {
      setForeground (Color.blue);
      setBackground (Color.white);
  }

/* The ''runner'' thread runs this method */

  public void run () {
      while (true) {
          repaint ();
          try { Thread.sleep (1000); }
          catch (Exception e) {}
      }
  }

  public void start () {
      if (null == runner) {
	  runner = new Thread (this);
	  runner.start ();
      }
  }

  public void stop () {
      if (null != runner) {
	  runner.stop ();
	  runner = null;
      }
  }

  public void paint (Graphics g) {
      Date now = new Date ();
      g.drawString (now.toString (), 0, 15);
  }

}
