package awt;

import stdlib.Color;
import stdlib.StdDraw;


/*************************************************************************
 *  Compilation:  javac Clock.java
 *  Execution:    java Clock
 *  Dependencies: StdDraw.java
 *
 *************************************************************************/

public class Clock { 

    public static void main(String[] args) { 
    	StdDraw sd = new StdDraw();

        for (int t = 0; true; t++) {

            // remainder operator with doubles so all hands move every second
            double seconds = t % 60;
            double minutes = (t / 60.0) % 60;
            double hours   = (t / 3600.0) % 12;

            StdDraw.clear(StdDraw.getClearColor());
            sd.setPenRadius();

            // clock face
            sd.setPenColor(Color.BLACK);
            StdDraw.filledCircle(0.5, 0.5, 0.45);

            // hour markers
            sd.setPenColor(Color.BLUE_GREEN);
            for (int i = 0; i < 12; i++) {
                double theta = Math.toRadians(i * 30);
                StdDraw.filledCircle(0.5 + 0.4 * Math.cos(theta), 0.5 + 0.4 * Math.sin(theta), .025);
            }

            // second hand
            sd.setPenRadius(.01);
            sd.setPenColor(Color.ORANGE);
            double angle1 = Math.toRadians(6 * seconds);
            double r1 = 0.4;
            sd.line(0.5, 0.5, 0.5 + r1 * Math.sin(angle1), 0.5 + r1 * Math.cos(angle1));

            // minute hand
            sd.setPenRadius(.02);
            sd.setPenColor(Color.DARK_GRAY);
            double angle2 = Math.toRadians(6 * minutes);
            double r2 = 0.3;
            sd.line(0.5, 0.5, 0.5 + r2 * Math.sin(angle2), 0.5 + r2 * Math.cos(angle2));

            // hour hand
            sd.setPenRadius(.025);
            sd.setPenColor(Color.GRAY);
            double angle3 = Math.toRadians(30 * hours);
            double r3 = 0.2;
            sd.line(0.5, 0.5, 0.5 + r3 * Math.sin(angle3), 0.5 + r3 * Math.cos(angle3));

            // digital time
            int second = t          % 60;
            int minute = (t / 60)   % 60;
            int hour   = (t / 3600) % 12;
            String time = String.format("%2d:%02d:%02d", hour, minute, second);
            sd.setPenColor(Color.DARK_RED);
            StdDraw.text(0.5, 0.0, time);

            // 1000 miliseconds = 1 second
            sd.show(1000);
        }
    }

}