public class DigitalClockApplet extends java
1: //DigitalClockApplet.java
2: /*
3: * Copyright (c) 1999-2002, Xiaoping Jia.
4: * All Rights Reserved.
5: */
6: import java.awt.*;
7: import java.util.Calendar;
8:
9: /**
10: * This is an applet that displays the time in the following format:
11: * HH:MM:SS
12: */
13: public class DigitalClockApplet extends java.applet.Applet
14: implements Runnable
15: {
16: protected Thread clockThread = null;
17: protected Font font = new Font("Monospaced", Font.BOLD, 48);
18: protected Color color = Color.black;
19:
20: public void start()
21: {
22: if (clockThread == null)
23: {
24: clockThread = new Thread(this);
25: clockThread.start();
26: }
27: }
28:
29: public void stop()
30: {
31: clockThread = null;
32: }
33:
34: public void run()
35: {
36: while (Thread.currentThread() == clockThread)
37: {
38: repaint();
39: try
40: {
41: Thread.currentThread().sleep(500);
42: }
43: catch (InterruptedException e)
44: {
45: }
46: }
47: }
48:
49: public void paint(Graphics g)
50: {
51: Calendar calendar = Calendar.getInstance();
52: int hour = calendar.get(Calendar.HOUR_OF_DAY);
53: int minute = calendar.get(Calendar.MINUTE);
54: int second = calendar.get(Calendar.SECOND);
55: int millisecond = calendar.get(Calendar.MILLISECOND);
56: g.setFont(font);
57: g.setColor(color);
58: g.drawString(hour + ":" + minute / 10 + minute % 10 +
59: ":" + second / 10 + second % 10 +
60: ":" + millisecond / 100
61: + millisecond / 10 % 10
62: + millisecond % 10, 10, 60);
63: }
64: }
65: