/* 
 * Ketan's Clock Applet: Inspired by a digital clock I saw somewhere.
 * Permission to copy freely, only send me an email if you do. 
 * Just so I feel good! :) 
 */

import java.awt.Color;
import java.awt.Graphics;
import java.util.Date;

/* Class definition */
public class Clock extends java.applet.Applet implements Runnable {

	Thread clockThread;
	int radius;
	int ht, wd;
	String height, width;
	int cx, cy;

	public void start() {
		height = getParameter("height");
		ht = (height == null) ? 40 : Integer.valueOf(height).intValue();
		width = getParameter("width");
		wd = (width == null) ? 40 : Integer.valueOf(width).intValue();
		radius = (ht < wd) ? ht / 2 : wd / 2;
		resize(ht, wd);
		cx = cy = radius;
		if (clockThread == null) {
			clockThread = new Thread(this, "Clock");
			clockThread.start();
		}
	}

	public void run() {
		while (clockThread != null) {
			repaint();
			try {
				clockThread.sleep(1000);
			} catch (InterruptedException e) {
				System.out.println("Interrupt Exception");
			}
		}
	}

	public void paint(Graphics g) {
		Date now = new Date();

		// Set background color
		setBackground(Color.gray);

		// Draw the second hand
		g.setColor(Color.blue);
		g.drawLine(cx, cy, cx
				+ (int) (Math.round(0.9 * radius
						* Math.sin(now.getSeconds() * 3.14 / 30.0))), cy
				- (int) (Math.round(0.9 * radius
						* Math.cos(now.getSeconds() * 3.14 / 30.0))));

		// Draw the minute hand
		g.setColor(Color.red);
		g.drawLine(cx, cy, cx
				+ (int) (Math.round(0.8 * radius
						* Math.sin(now.getMinutes() * 3.14 / 30.0))), cy
				- (int) (Math.round(0.8 * radius
						* Math.cos(now.getMinutes() * 3.14 / 30.0))));

		// Draw the hour hand
		g.setColor(Color.yellow);
		g.drawLine(cx, cy, cx
				+ (int) (Math.round(radius
						* 0.5
						* Math.sin((now.getHours() % 12) * 3.14 / 6.0
								+ (3.14 * now.getMinutes() / 360.0)))), cy
				- (int) (Math.round(radius
						* 0.5
						* Math.cos((now.getHours() % 12) * 3.14 / 6.0
								+ (3.14 * now.getMinutes() / 360.0)))));
	}

	public void stop() {
		clockThread.stop();
		clockThread = null;
	}
}


