|
| 1 | +import java.applet.*; |
| 2 | +import java.awt.*; |
| 3 | +import java.util.*; |
| 4 | +import java.text.*; |
| 5 | + |
| 6 | +public class MyClock extends Applet implements Runnable { |
| 7 | + |
| 8 | + int width, height; |
| 9 | + Thread t = null; |
| 10 | + boolean threadSuspended; |
| 11 | + int hours=0, minutes=0, seconds=0; |
| 12 | + String timeString = ""; |
| 13 | + |
| 14 | + public void init() { |
| 15 | + width = getSize().width; |
| 16 | + height = getSize().height; |
| 17 | + setBackground( Color.black ); |
| 18 | + } |
| 19 | + |
| 20 | + public void start() { |
| 21 | + if ( t == null ) { |
| 22 | + t = new Thread( this ); |
| 23 | + t.setPriority( Thread.MIN_PRIORITY ); |
| 24 | + threadSuspended = false; |
| 25 | + t.start(); |
| 26 | + } |
| 27 | + else { |
| 28 | + if ( threadSuspended ) { |
| 29 | + threadSuspended = false; |
| 30 | + synchronized( this ) { |
| 31 | + notify(); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public void stop() { |
| 38 | + threadSuspended = true; |
| 39 | + } |
| 40 | + |
| 41 | + public void run() { |
| 42 | + try { |
| 43 | + while (true) { |
| 44 | + |
| 45 | + Calendar cal = Calendar.getInstance(); |
| 46 | + hours = cal.get( Calendar.HOUR_OF_DAY ); |
| 47 | + if ( hours > 12 ) hours -= 12; |
| 48 | + minutes = cal.get( Calendar.MINUTE ); |
| 49 | + seconds = cal.get( Calendar.SECOND ); |
| 50 | + |
| 51 | + SimpleDateFormat formatter |
| 52 | + = new SimpleDateFormat( "hh:mm:ss", Locale.getDefault() ); |
| 53 | + Date date = cal.getTime(); |
| 54 | + timeString = formatter.format( date ); |
| 55 | + |
| 56 | + // Now the thread checks to see if it should suspend itself |
| 57 | + if ( threadSuspended ) { |
| 58 | + synchronized( this ) { |
| 59 | + while ( threadSuspended ) { |
| 60 | + wait(); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + repaint(); |
| 65 | + t.sleep( 1000 ); // interval specified in milliseconds |
| 66 | + } |
| 67 | + } |
| 68 | + catch (Exception e) { } |
| 69 | + } |
| 70 | + |
| 71 | + void drawHand( double angle, int radius, Graphics g ) { |
| 72 | + angle -= 0.5 * Math.PI; |
| 73 | + int x = (int)( radius*Math.cos(angle) ); |
| 74 | + int y = (int)( radius*Math.sin(angle) ); |
| 75 | + g.drawLine( width/2, height/2, width/2 + x, height/2 + y ); |
| 76 | + } |
| 77 | + |
| 78 | + void drawWedge( double angle, int radius, Graphics g ) { |
| 79 | + angle -= 0.5 * Math.PI; |
| 80 | + int x = (int)( radius*Math.cos(angle) ); |
| 81 | + int y = (int)( radius*Math.sin(angle) ); |
| 82 | + angle += 2*Math.PI/3; |
| 83 | + int x2 = (int)( 5*Math.cos(angle) ); |
| 84 | + int y2 = (int)( 5*Math.sin(angle) ); |
| 85 | + angle += 2*Math.PI/3; |
| 86 | + int x3 = (int)( 5*Math.cos(angle) ); |
| 87 | + int y3 = (int)( 5*Math.sin(angle) ); |
| 88 | + g.drawLine( width/2+x2, height/2+y2, width/2 + x, height/2 + y ); |
| 89 | + g.drawLine( width/2+x3, height/2+y3, width/2 + x, height/2 + y ); |
| 90 | + g.drawLine( width/2+x2, height/2+y2, width/2 + x3, height/2 + y3 ); |
| 91 | + } |
| 92 | + |
| 93 | + public void paint( Graphics g ) { |
| 94 | + g.setColor( Color.gray ); |
| 95 | + drawWedge( 2*Math.PI * hours / 12, width/5, g ); |
| 96 | + drawWedge( 2*Math.PI * minutes / 60, width/3, g ); |
| 97 | + drawHand( 2*Math.PI * seconds / 60, width/2, g ); |
| 98 | + g.setColor( Color.white ); |
| 99 | + g.drawString( timeString, 10, height-10 ); |
| 100 | + } |
| 101 | +} |
0 commit comments