// Program: dc.java // Purpose: This program implements a digital clock // Author : Chunyen Liu // Date : 1999/03/10 import java.awt.*; import java.applet.*; import java.util.Date; public class dc extends Applet implements Runnable { Graphics og; Image oi; Thread t = null; int width, height, delay = 500; Date currTime; String time; int timewidth, timeheight, timedescent, timeH, timeM, timeS; public void init() { super.init(); width = size().width; height = size().height; resize(width, height); // 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); og.setFont(new Font("Helvetica", Font.BOLD, 15)); og.setColor(new Color(20, 20, 20)); currTime = new Date(); timeH = currTime.getHours(); timeM = currTime.getMinutes(); timeS = currTime.getSeconds(); time = ""; if (timeH < 10) time = "0" + timeH; else time = "" + timeH; if (timeM < 10) time += ":0" + timeM; else time += ":" + timeM; if (timeS < 10) time += ":0" + timeS; else time += ":" + timeS; timewidth = (og.getFontMetrics()).stringWidth(time); timeheight = (og.getFontMetrics()).getHeight(); timedescent = (og.getFontMetrics()).getDescent(); og.drawString(time, (width - timewidth) / 2, height - (height - timeheight) / 2 - timedescent); og.setColor(new Color(230, 230, 230)); og.drawString(time, (width - timewidth) / 2 - 1, height - (height - timeheight) / 2 - timedescent - 1); g.drawImage(oi, 0, 0, this); } }