import java.awt.*;
import java.util.Calendar;

public class AnalogClock
  extends java.applet.Applet implements Runnable {

  protected Thread clockThread = null;
  protected Font font = new Font("Monospaced", Font.BOLD, 24);
  protected Color color = Color.green;
  protected Image image;
  protected Graphics offscreen;
  protected Dimension d;

  public void init(){
	  d = getSize();
  }

  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 update(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 hourLength = 70;
    int minuteLength = 80;
    int secondLength = 90;
    //int hx =
    if (image == null) {
		  image = createImage(d.width, d.height);
		  offscreen = image.getGraphics();
    }

    //System.out.println(java.lang.Math.cos(angle));
    offscreen.setColor(Color.white);
    offscreen.fillRect(0,0,d.width,d.height);
    offscreen.setFont(font);
    offscreen.setColor(color);

    offscreen.drawOval(30,30,200,200);
    double angle;

    for (int i=1;i<=12;i++){
       angle = (i % 12) * 2.0 * Math.PI/12;
       offscreen.drawString(i+"", 120 + (int)((double) 90 * Math.sin(angle)), 140 - (int)((double) 90 * Math.cos(angle)));
    }

    angle = (second % 60) * 2.0 * Math.PI/60;
    int x = 130 +  (int)((double) secondLength * Math.sin(angle));
    int y = 130 -  (int)((double) secondLength * Math.cos(angle));
    offscreen.drawLine(130,130, x, y);

    angle = (minute % 60) * 2.0 * Math.PI/60;
	x = 130 +  (int)((double) minuteLength * Math.sin(angle));
	y = 130 -  (int)((double) minuteLength * Math.cos(angle));
	offscreen.drawLine(130,130, x, y);

    angle = (hour % 12) * 2.0 * Math.PI/12 + angle/12;
	x = 130 +  (int)((double) hourLength * Math.sin(angle));
	y = 130 -  (int)((double) hourLength * Math.cos(angle));
	offscreen.drawLine(130,130, x, y);

    g.drawImage(image, 0, 0, this);
  }

  public void paint(Graphics g) {
	  update(g);
  }
}
