// Program: ac.java // Purpose: This program implements an analog clock // Author : Chunyen Liu // Date : 1999/03/10 import java.awt.*; import java.applet.*; import java.util.Date; public class ac extends Applet implements Runnable { Graphics og; Image oi; Thread t = null; int width, height, delay = 500; Date currTime; int timeH, timeM, timeS, radius = 50, lenH, lenM, lenS, lenIn, cx, cy, x1, y1, x2, y2; public void init() { super.init(); width = size().width; height = size().height; resize(width, height); lenH = 5 * radius / 10; lenM = 6 * radius / 10; lenS = 7 * radius / 10; lenIn = 8 * radius / 10; cx = width / 2; cy = height / 2; // double buffering oi = createImage(width, height); og = oi.getGraphics(); } public void start() { if (t == null) { t = new Thread(this); t.start(); } } public void stop() { t.stop(); t = null; } public void run() { Thread.currentThread().setPriority(Thread.NORM_PRIORITY - 1); while (true) { repaint(); try { Thread.sleep(delay); } catch (InterruptedException e) {} } } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { og.setColor(new Color(170, 170, 170)); og.fillRect(0, 0, width, height); currTime = new Date(); timeH = currTime.getHours(); timeM = currTime.getMinutes(); timeS = currTime.getSeconds(); if (timeH >= 12) timeH -= 12; for (int i = 1; i < 13; i++) { og.setColor(new Color(20, 20, 20)); x2 = (int)(cx + radius * Math.sin(i * 2 * 3.14159f / 12)); y2 = (int)(cy - radius * Math.cos(i * 2 * 3.14159f / 12)); if (i % 3 != 0) { x1 = (int)(cx + 0.9f * radius * Math.sin(i * 2 * 3.14159f / 12)); y1 = (int)(cy - 0.9f * radius * Math.cos(i * 2 * 3.14159f / 12)); } else { x1 = (int)(cx + 0.8f * radius * Math.sin(i * 2 * 3.14159f / 12)); y1 = (int)(cy - 0.8f * radius * Math.cos(i * 2 * 3.14159f / 12)); } og.drawLine(x1, y1, x2, y2); og.setColor(new Color(230, 230, 230)); og.drawLine(x1 - 1, y1 - 1, x2 - 1, y2 - 1); } og.setColor(new Color(20, 20, 20)); x2 = (int)(cx + lenH * Math.sin((timeH + timeM / 60.0f + timeS / 3600.0f) * 2 * 3.14159f / 12)); y2 = (int)(cy - lenH * Math.cos((timeH + timeM / 60.0f + timeS / 3600.0f) * 2 * 3.14159f / 12)); og.drawLine(cx, cy, x2, y2); og.setColor(Color.red); og.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1); og.setColor(new Color(20, 20, 20)); x2 = (int)(cx + lenM * Math.sin((timeM + timeS / 60.0f) * 2 * 3.14159f / 60)); y2 = (int)(cy - lenM * Math.cos((timeM + timeS / 60.0f) * 2 * 3.14159f / 60)); og.drawLine(cx, cy, x2, y2); og.setColor(Color.green); og.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1); og.setColor(new Color(20, 20, 20)); x2 = (int)(cx + lenS * Math.sin(timeS * 2 * 3.14159f / 60)); y2 = (int)(cy - lenS * Math.cos(timeS * 2 * 3.14159f / 60)); og.drawLine(cx, cy, x2, y2); og.setColor(Color.blue); og.drawLine(cx - 1, cy - 1, x2 - 1, y2 - 1); og.setColor(new Color(20, 20, 20)); og.drawOval((width - 2 * radius) / 2, (height - 2 * radius) / 2, 2 * radius, 2 * radius); og.setColor(new Color(230, 230, 230)); og.drawOval((width - 2 * radius) / 2 - 1, (height - 2 * radius) / 2 - 1, 2 * radius, 2 * radius); g.drawImage(oi, 0, 0, this); } }