1 /* 2 * File: AnalogClock2.java 3 * 4 * An analog clock (local time) 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 import java.util.*; 15 16 public class AnalogClock2 extends Applet implements Runnable { 17 18 // a thread: 19 private Thread thread; 20 21 // the current hour, minute, and second: 22 private double h, m; 23 private int s; 24 25 // applet parameters: 26 private int size, border; 27 private int center_x, center_y; 28 29 // the length of each hand: 30 private int h_length, m_length, s_length; 31 32 // the endpoint of each hand: 33 private Point s_point, m_point, h_point; 34 35 public void init() { 36 37 // local constants: 38 final int msPerMinute = 60 * 1000; 39 final int msPerHour = 60 * msPerMinute; 40 41 // number of milliseconds since midnight, January 1, 1970 UTC: 42 //long t = getMilliseconds(); 43 44 // number of milliseconds since midnight today UTC: 45 //t %= 24 * msPerHour; 46 47 // number of milliseconds since midnight today EST: 48 int ms = getMilliseconds(); 49 50 // calculate current hour, minute, and second: 51 h = ms / ( double ) msPerHour; 52 m = ms / ( double ) msPerMinute; 53 s = ( int ) ms % msPerHour; 54 55 // calculate applet parameters: 56 size = Math.min( getSize().width, getSize().height ); 57 border = ( int ) Math.round( 0.05 * size ); 58 center_x = center_y = ( int ) Math.round( size/2 ); 59 60 // calculate the length of each hand: 61 int diff = size/2 - border; 62 s_length = m_length = ( int ) Math.round( diff * 0.9 ); 63 h_length = ( int ) Math.round( diff * 0.7 ); 64 65 // calculate the endpoint of each hand: 66 double theta = ( 15 - s )*Math.PI/30; // PI/2 - s*2*PI/60 67 s_point = computeEndpoint( s_length, theta ); 68 theta = ( 15 - m )*Math.PI/30; // PI/2 - m*2*PI/60 69 m_point = computeEndpoint( m_length, theta ); 70 theta = ( 3 - h )*Math.PI/6; // PI/2 - h*2*PI/12 71 h_point = computeEndpoint( h_length, theta ); 72 73 } 74 75 // Override java.applet.Applet.start(): 76 public void start() { 77 if ( thread == null ) { 78 // Start a new thread: 79 thread = new Thread( this ); 80 thread.start(); 81 } 82 } 83 84 // Override java.applet.Applet.stop(): 85 public void stop() { 86 if ( thread != null ) { 87 thread.stop(); 88 thread = null; 89 } 90 } 91 92 // Implement java.lang.Runnable.run(): 93 public void run() { 94 while ( thread != null ) { 95 try { 96 // sleep for one second: 97 Thread.sleep( 1000 ); 98 } catch ( InterruptedException e ) { 99 // Do nothing 100 }; 101 repaint(); 102 } 103 } 104 105 public void paint( Graphics g ) { 106 107 // local variables: 108 int width, height; 109 110 // draw the clock face: 111 g.setColor( Color.black ); 112 width = height = size - 2*border; 113 g.drawOval( border, border, width, height ); 114 115 // erase the second hand: 116 g.setColor( getBackground() ); 117 g.drawLine( center_x, center_y, s_point.x, s_point.y ); 118 119 // draw the second hand: 120 g.setColor( Color.red ); 121 s++; 122 s_point = computeEndpoint( s_length, ( 15 - s )*Math.PI/30 ); 123 g.drawLine( center_x, center_y, s_point.x, s_point.y ); 124 125 // erase the minute hand: 126 g.setColor( getBackground() ); 127 g.drawLine( center_x, center_y, m_point.x, m_point.y ); 128 129 // draw the minute hand: 130 g.setColor( Color.black ); 131 m += 1.0/60.0; 132 m_point = computeEndpoint( m_length, ( 15 - m )*Math.PI/30 ); 133 g.drawLine( center_x, center_y, m_point.x, m_point.y ); 134 135 // erase the hour hand: 136 g.setColor( getBackground() ); 137 g.drawLine( center_x, center_y, h_point.x, h_point.y ); 138 139 // draw the hour hand: 140 g.setColor( Color.black ); 141 h += 1.0/3600.0; 142 h_point = computeEndpoint( h_length, ( 3 - h )*Math.PI/6 ); 143 g.drawLine( center_x, center_y, h_point.x, h_point.y ); 144 145 } 146 147 private int getMilliseconds() { 148 149 int ms; // local variable 150 151 // compute offset for GMT - 05:00 (Eastern Standard Time): 152 //int msPerHour = 60 * 60 * 1000; 153 //int rawOffset = -5 * msPerHour; 154 // get the supported ids: 155 //String[] ids = TimeZone.getAvailableIDs( rawOffset ); 156 // create a Eastern Standard Time time zone: 157 //SimpleTimeZone edt = new SimpleTimeZone( rawOffset, ids[0] ); 158 // set up rules for daylight savings time: 159 //edt.setStartRule( Calendar.APRIL, 1, Calendar.SUNDAY, 2*msPerHour ); 160 //edt.setEndRule( Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*msPerHour ); 161 // create a GregorianCalendar: 162 //GregorianCalendar calendar = new GregorianCalendar( edt ); 163 GregorianCalendar calendar = new GregorianCalendar(); 164 Date today = new Date(); 165 calendar.setTime( today ); 166 ms = calendar.get( Calendar.MILLISECOND ); 167 return ms; 168 } 169 170 private Point computeEndpoint( int length, double theta ) { 171 int x = center_x + ( int ) Math.round( length*Math.cos( theta ) ); 172 int y = center_y - ( int ) Math.round( length*Math.sin( theta ) ); 173 return new Point( x, y ); 174 } 175 176 }