import java.awt.*;
import java.util.Calendar;

/**
 *  This is an applet that displays the time in the following format:
 *        HH:MM:SS
 */
public class DigitalClock
  extends java.applet.Applet implements Runnable {

  protected Thread clockThread = null;
  protected Font font = new Font("Monospaced", Font.BOLD, 48);
  protected Color color = Color.green;

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

  public void stop() {
    clockThread = null;
  }

  public void run() {
    while (Thread.currentThread()  ==  clockThread) {
      repaint();
      try {
	Thread.currentThread().sleep(1000);
      } catch (InterruptedException e) {}
    }
  }

  public void paint(Graphics g) {
    Calendar calendar = Calendar.getInstance();
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    int minute = calendar.get(Calendar.MINUTE);
    int second = calendar.get(Calendar.SECOND);
    int year=calendar.get(Calendar.YEAR);
    int month=calendar.get(Calendar.MONTH);
    int date=calendar.get(Calendar.DAY_OF_MONTH);
    int dayOfWeek=calendar.get(Calendar.DAY_OF_WEEK);

    g.setFont(font);
    g.setColor(color);
    if (hour<=12) {
      g.drawString(hour + ":" + minute / 10 + minute % 10 +
                          ":" + second / 10 + second % 10 + "AM", 10, 60);
    }
    else {
      g.drawString(hour-12 + ":" + minute / 10 + minute % 10 +
                          ":" + second / 10 + second % 10 + "PM", 10, 60);
    }

    // Convert the number representation of month and weekday into alphabet representation.
    String[] months={"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
    String[] weekDays={"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
    g.drawString(date+" "+months[month]+" "+year+" "+weekDays[dayOfWeek-1], 10, 110);
  }

}
