/**
 *
 * Class Clock implements a digital clock applet, which gives
 * the time in the format:
 * Day_Of_Month Month Day, Year hh:mm;ss AM|PM
 *
 * @author  Daniel Burgner
 * @version 1.0
 * @class   CS5730
 * @date    02.05.2002
 */

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

public class Clock
	extends java.applet.Applet implements Runnable {
		protected Thread clockThread = null;
		protected Font font = new Font("Monospaced", Font.BOLD, 18);
		protected Color color = Color.black;

		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 cal = Calendar.getInstance();
			int wday = cal.get(Calendar.DAY_OF_WEEK)-1;
			String swday[] = {"Sunday", "Monday", "Tuesday", "Wednesday", 
					   "Thursday", "Friday", "Saturday"};
			int month = cal.get(Calendar.MONTH);
			String smonth[] = {"January", "February", "March", "April",
					     "May", "June", "July", "August",
					     "September", "October", "November", "December"};
			int day = cal.get(Calendar.DAY_OF_MONTH);
			int year = cal.get(Calendar.YEAR);
			int hour = cal.get(Calendar.HOUR_OF_DAY);
			if (hour == 0) {
				hour = hour + 12;
			}
			if (hour > 12) {
				hour = hour - 12;
			}
			int ampm = cal.get(Calendar.AM_PM);
			String sampm = "";
			sampm = (ampm == 0) ? "AM" : "PM";
			int min = cal.get(Calendar.MINUTE);
			int sec = cal.get(Calendar.SECOND);
			Dimension d = getSize();
			g.setColor(Color.white);
			g.fillRect(0,0,d.width,d.height);
			g.setFont(font);
			g.setColor(color);
			g.drawString("Today is:  " + swday[wday] + " " + smonth[month] + 
				     " " + day + ", " + year + " " + 
				     hour + ":" + min / 10 + min % 10 +
				     ":" + sec / 10 + sec % 10 + " " + sampm, 5, 15);
		}
}
