1 /* 2 * File: AnalogClock.java 3 * 4 * An analog clock (UTC) 5 * 6 * Copyright: Northeast Parallel Architectures Center 7 * 8 */ 9 10 import java.applet.Applet; 11 import java.awt.Graphics; 12 import java.awt.Color; 13 import java.awt.Point; 14 15 public class AnalogClock extends Applet implements Runnable { 16 17 // a thread: 18 private Thread thread; 19 20 // the current hour, minute, and second: 21 private double h, m; 22 private int s; 23 24 // applet parameters: 25 private int size, border; 26 private int center_x, center_y; 27 28 // the length of each hand: 29 private int h_length, m_length, s_length; 30 31 // the endpoint of each hand: 32 private Point s_point, m_point, h_point; 33 34 public void init() { 35 36 // local constants: 37 final int msPerMinute = 60 * 1000; 38 final int msPerHour = 60 * msPerMinute; 39 40 // number of milliseconds since midnight, January 1, 1970 UTC: 41 long t = new java.util.Date().getTime(); 42 //long t = System.currentTimeMillis(); 43 System.out.println( t ); // debugging 44 45 // number of milliseconds since midnight today UTC: 46 t %= 24 * msPerHour; 47 48 // calculate current hour, minute, and second: 49 h = t / ( double ) msPerHour; // this is wrong! 50 m = t / ( double ) msPerMinute; 51 s = ( int ) t % msPerHour; 52 53 // calculate applet parameters: 54 size = Math.min( getSize().width, getSize().height ); 55 border = ( int ) Math.round( 0.05 * size ); 56 center_x = center_y = ( int ) Math.round( size/2 ); 57 58 // calculate the length of each hand: 59 int diff = size/2 - border; 60 s_length = m_length = ( int ) Math.round( diff * 0.9 ); 61 h_length = ( int ) Math.round( diff * 0.7 ); 62 63 // calculate the endpoint of each hand: 64 double theta = ( 15 - s )*Math.PI/30; // PI/2 - s*2*PI/60 65 s_point = computeEndpoint( s_length, theta ); 66 theta = ( 15 - m )*Math.PI/30; // PI/2 - m*2*PI/60 67 m_point = computeEndpoint( m_length, theta ); 68 theta = ( 3 - h )*Math.PI/6; // PI/2 - h*2*PI/12 69 h_point = computeEndpoint( h_length, theta ); 70 71 } 72 73 // Override java.applet.Applet.start(): 74 public void start() { 75 if ( thread == null ) { 76 // Start a new thread: 77 thread = new Thread( this ); 78 thread.start(); 79 } 80 } 81 82 // Override java.applet.Applet.stop(): 83 public void stop() { 84 if ( thread != null ) { 85 thread.stop(); 86 thread = null; 87 } 88 } 89 90 // Implement java.lang.Runnable.run(): 91 public void run() { 92 while ( thread != null ) { 93 try { 94 // sleep for one second: 95 Thread.sleep( 1000 ); 96 } catch ( InterruptedException e ) { 97 // Do nothing 98 }; 99 repaint(); 100 } 101 } 102 103 public void paint( Graphics g ) { 104 105 // local variables: 106 int width, height; 107 108 // draw the clock face: 109 g.setColor( Color.black ); 110 width = height = size - 2*border; 111 g.drawOval( border, border, width, height ); 112 113 // erase the second hand: 114 g.setColor( getBackground() ); 115 g.drawLine( center_x, center_y, s_point.x, s_point.y ); 116 117 // draw the second hand: 118 g.setColor( Color.red ); 119 s++; 120 s_point = computeEndpoint( s_length, ( 15 - s )*Math.PI/30 ); 121 g.drawLine( center_x, center_y, s_point.x, s_point.y ); 122 123 // erase the minute hand: 124 g.setColor( getBackground() ); 125 g.drawLine( center_x, center_y, m_point.x, m_point.y ); 126 127 // draw the minute hand: 128 g.setColor( Color.black ); 129 m += 1.0/60.0; 130 m_point = computeEndpoint( m_length, ( 15 - m )*Math.PI/30 ); 131 g.drawLine( center_x, center_y, m_point.x, m_point.y ); 132 133 // erase the hour hand: 134 g.setColor( getBackground() ); 135 g.drawLine( center_x, center_y, h_point.x, h_point.y ); 136 137 // draw the hour hand: 138 g.setColor( Color.black ); 139 h += 1.0/3600.0; // this is wrong! 140 h_point = computeEndpoint( h_length, ( 3 - h )*Math.PI/6 ); 141 g.drawLine( center_x, center_y, h_point.x, h_point.y ); 142 143 } 144 145 private Point computeEndpoint( int length, double theta ) { 146 int x = center_x + ( int ) Math.round( length*Math.cos( theta ) ); 147 int y = center_y - ( int ) Math.round( length*Math.sin( theta ) ); 148 return new Point( x, y ); 149 } 150 151 }