import java.awt.*; import java.util.*; public class Clock extends java.applet.Applet implements Runnable { Thread clockThread; int radius, diameter, diameterOval, radiusNumbers; int cx, cy; Point[] points; public void start() { resize(50, 50); radius = 20; cx = cy = 25; diameter = 40; diameterOval = 48; if (clockThread == null) { clockThread = new Thread(this, "Clock"); clockThread.start(); } } public void stop() { clockThread.stop(); clockThread = null; } public void run() { while (clockThread != null) { repaint(); try { clockThread.sleep(1000); } catch (InterruptedException e) { System.out.println("Interrupt Exception"); } } } public void paint(Graphics g) { Date now = new Date(); setBackground(Color.white); g.setColor(Color.black); g.drawOval(1, 1, diameterOval, diameterOval); //set font size Font font = new Font("Arial", Font.BOLD, 7); g.setFont(font); FontMetrics fontMetrics = g.getFontMetrics(font); setClockNumbers(); for (int i = 0; i < 12; i++) { if ( i == 0 ) { g.drawString("12", points[i].x, points[i].y); } else { Integer number = new Integer (i); g.drawString(number.toString(), points[i].x, points[i].y); } } //second hand g.setColor(Color.red); g.drawLine ( cx ,cy ,cx+(int)(Math.round(0.8*radius*Math.sin(now.getSeconds()*3.14/30.0))) ,cy-(int)(Math.round(0.8*radius*Math.cos(now.getSeconds()*3.14/30.0))) ); //minute hand g.setColor(Color.green); g.drawLine ( cx ,cy ,cx+(int)(Math.round(0.6*radius*Math.sin(now.getMinutes()*3.14/30.0))) ,cy-(int)(Math.round(0.6*radius*Math.cos(now.getMinutes()*3.14/30.0))) ); //hour hand g.setColor(Color.blue); g.drawLine ( cx ,cy ,cx+(int)(Math.round(radius*0.4*Math.sin((now.getHours()%12)*3.14/6.0 + (3.14*now.getMinutes()/360.0)))) ,cy-(int)(Math.round(radius*0.4*Math.cos((now.getHours()%12)*3.14/6.0+ (3.14*now.getMinutes()/360.0)))) ); } private void setClockNumbers() { points = new Point[12]; double twelth = Math.PI / 6; double half = Math.PI / 2; int radiusNumbers = 20; for (int i = 0; i < points.length; i++) { int nx = (int) ((Math.cos((i * twelth) - half) * radiusNumbers) + radiusNumbers + 3.5); int ny = (int) ((Math.sin((i * twelth) - half) * radiusNumbers) + radiusNumbers + 8.5); points[i] = new Point(nx, ny); } } }