//Draw a clock based upon the Domino display.

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

public class DominoClock extends Applet implements Runnable {

	int StartX, StartY, SquareD, DotRad;
  	Color bgColor, dominoColor, dotColor;
  	Thread clock = null;
	String background;
  
  	public void init() {

		try {
			background = getParameter( "bgcolor" );
		} catch (NullPointerException e) {};
			
		if (( background == null ) || 
		    ( background.charAt(0) != '#' ) || 
		    ( background.length() != 7 )) {
		    Integer rgbValue = new Integer(0);
		    bgColor = new Color(rgbValue.intValue());
		} else {
		    Integer rgbValue = new Integer(0);
		    rgbValue = Integer.valueOf( background.substring(1,7), 16 );
		    bgColor = new Color(rgbValue.intValue());
		}

		try {
			background = getParameter( "dominocolor" );
		} catch (NullPointerException e) {};
		if (( background == null ) || 
		    ( background.charAt(0) != '#' ) || 
		    ( background.length() != 7 )) {
		    Integer rgbValue = new Integer(14331680);
		    dominoColor = new Color(rgbValue.intValue());
		} else {
		    Integer rgbValue = new Integer(14331680);
		    rgbValue = Integer.valueOf( background.substring(1,7), 16 );
		    dominoColor = new Color(rgbValue.intValue());
		}

		try {
			background = getParameter( "dotcolor" );
		} catch (NullPointerException e) {};
		if (( background == null ) || 
		    ( background.charAt(0) != '#' ) || 
		    ( background.length() != 7 )) {
		    Integer rgbValue = new Integer(3100495); 
		    dotColor = new Color(rgbValue.intValue());
		} else {
		    Integer rgbValue = new Integer(3100495); 
		    rgbValue = Integer.valueOf( background.substring(1,7), 16 );
		    dotColor = new Color(rgbValue.intValue());
		}

		Dimension D = size();

		if (D.height > (3 * D.width)) { 
			SquareD = D.width;
			StartX = 0;
			StartY = 0; // (D.height / 3) - SquareD;
		}
		else {
			SquareD = (D.height / 3);
			StartY = 0;
			StartX = 0; //.width - (SquareD / 3); 
		}

		DotRad = (int) (Math.sqrt(2 * SquareD * SquareD) / 6);
	}

	public void paint(Graphics g) {
		
		int Hour, Min, Sec;
		Date D = new Date();

		Hour = D.getHours();
		if (Hour > 12) Hour = Hour - 12;
		Min  = D.getMinutes();
		Sec  = D.getSeconds();

		Tock(StartX, StartY, Hour, g);
		Tock(StartX, StartY + SquareD, (Min / 5), g);
		Tock(StartX, StartY + (2 * SquareD), (Sec / 5), g);
	}

	public void update(Graphics g) {
		paint(g);		// Overridden to prevent flicker.
	}

	public void start() {
		clock = new Thread(this);
		clock.start();
	}
		
	public void stop() {
		clock.stop();
	}	
	
	public void run() {
		while(true) {
			try {
				Thread.sleep(1000);
			}
			catch(InterruptedException e) {}
			repaint();
		}
	}
	
	private void Tock(int StartX, int StartY, int time, Graphics g) {

		g.setColor(dominoColor);
		g.fillRect(StartX, StartY, SquareD, SquareD);
		g.setColor(dotColor);

		if ((time == 1) || (time == 5) || (time == 8) || (time == 9) || (time == 11))
			g.fillOval(StartX + DotRad, StartY + DotRad, DotRad, DotRad);

		if ((time == 2) || (time == 5) || (time == 6) || (time == 10) || (time == 11))
			g.fillOval(StartX + SquareD - (2 * DotRad), StartY + DotRad, DotRad, DotRad);

		if ((time == 3) || (time == 6) || (time == 7) || (time == 9) || (time == 11))
			g.fillOval(StartX + SquareD - (2 * DotRad), StartY + SquareD - (2 * DotRad), DotRad, DotRad);

		if ((time == 4) || (time == 7) || (time == 8) || (time == 10) || (time == 11))
			g.fillOval(StartX + DotRad, StartY + SquareD - (2 * DotRad), DotRad, DotRad);

		g.drawLine(StartX, StartY + SquareD - 1, StartX + SquareD, StartY + SquareD - 1);

	}


        public static void main(String args[]) {
                    Frame f1 = new Frame("Domino Clock");
                    DominoClock s1 = new DominoClock();

                    f1.add("Center", s1);
                    f1.resize(300, 300);
                    f1.show();      

                    s1.init();
                    s1.start();
            }

}