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

public
class Clock extends Applet implements Runnable{
   protected int sizeX;
   protected int sizeY;
   protected int r;
   protected int centerX;
   protected int centerY;
   protected Color clockColor;
   protected double hour;
   protected double minute;
   protected double second;
   protected Thread thread;
   public void init(){
      sizeX = getSize().width;
      sizeY = getSize().height;
      
      centerX = (int) (sizeX / 2);
      centerY = (int) (sizeY / 2);

      if (sizeX < sizeY){
         sizeY = sizeX;
      }
      else if (sizeX > sizeY){
         sizeX = sizeY;
      }
      else{
      }
      r = (int) (sizeX / 2);
      clockColor = Color.lightGray;
   }
   public void start(){
      thread = new Thread(this);
      thread.start();
   }
   public void run(){
      while(true){
         repaint();
         try{
            thread.sleep(1000);
         }
         catch(InterruptedException e){
         }
      }
   }
   public void stop(){
      thread.stop();
   }
   public void paint(Graphics gDC){
      Color oldColor = gDC.getColor();
      getTime();
      gDC.setColor(clockColor);
      gDC.fillOval(centerX - sizeX / 2, centerY - sizeY / 2, sizeX, sizeY);
      drawDigits(gDC);
      drawHands(gDC);
      gDC.setColor(oldColor);
   }
   public void drawDigits(Graphics gDC){
      Color oldColor = gDC.getColor();
      gDC.setColor(Color.black);
      for (int i = 1; i <= 12; i++){
         int digitWidth = gDC.getFontMetrics().stringWidth(Integer.toString(i));
         int digitHeight = gDC.getFontMetrics().getHeight();
         int x = (int) (centerX + (r - digitHeight) * Math.cos(2 * Math.PI * i / 12 - Math.PI/2));
         int y = (int) (centerY + (r - digitHeight) * Math.sin(2 * Math.PI * i / 12 - Math.PI/2));
         x -= digitWidth / 2;
         y += digitHeight / 2;
         gDC.drawString(Integer.toString(i), x, y);
      }
      gDC.setColor(oldColor);
   }
   public void drawHands(Graphics gDC){
      Color oldColor = gDC.getColor();
      gDC.setColor(Color.black);

      double hVal = hour + (double)minute / 60;

      int xH = (int) (centerX + (r * 0.5) * Math.cos(2 * Math.PI * hVal / 12  Math.PI/2));
      int yH = (int) (centerY + (r * 0.5) * Math.sin(2 * Math.PI * hVal / 12  Math.PI/2));
      
      int xM = (int) (centerX + (r * 0.6) * Math.cos(2 * Math.PI * minute / 60 - Math.PI/2));
      int yM = (int) (centerY + (r * 0.6) * Math.sin(2 * Math.PI * minute / 60 - Math.PI/2));
      
      int xS = (int) (centerX + (r * 0.7) * Math.cos(2 * Math.PI * second / 60 - Math.PI/2));
      int yS = (int) (centerY + (r * 0.7) * Math.sin(2 * Math.PI * second / 60 - Math.PI/2));
      
      gDC.setColor(Color.red);
      gDC.drawLine(centerX, centerY, xH, yH);
      gDC.setColor(Color.green);
      gDC.drawLine(centerX, centerY, xM, yM);
      gDC.setColor(Color.blue);
      gDC.drawLine(centerX, centerY, xS, yS);
      gDC.setColor(oldColor);      
   }
   public void getTime(){
      GregorianCalendar calendar = new GregorianCalendar();
      hour = calendar.get(Calendar.HOUR);
      minute = calendar.get(Calendar.MINUTE);
      second = calendar.get(Calendar.SECOND);
   }
}

