-
Notifications
You must be signed in to change notification settings - Fork 0
/
DigitalClock.java
219 lines (158 loc) · 7.79 KB
/
DigitalClock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import java.awt.*;
import java.applet.*;
import java.awt.Font;
import java.util.Date;
import java.lang.*;
public class DigitalClock extends Applet implements Runnable {
Color textColor; // the color of the DigitalClock text
Color bgColor; // the color of the DigitalClock background
int appletHeight; // the height of the applet
int appletWidth; // the width of the applet
String fontName; // the name of the font to use (Courier, etc.)
int fontStyle; // the font style (PLAIN, BOLD, ITALIC)
int fontSize; // the font size (10, 12, 14, etc.)
Image offScreenImage; // double-buffering variable
Graphics offScreenGraphics; // double-buffering variable
String date; // the 'date' string (printed on screen)
String time; // the 'time' string (printed on screen)
int xDate; // the x-coordinate for the date string
int yDate; // the y-coordinate for the date string
int xTime; // the x-coordinate for the time string
int yTime; // the y-coordinate for the time string
Font theFont;
Thread runner;
String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
String[] days = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
String[] nums = { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
"40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
"50", "51", "52", "53", "54", "55", "56", "57", "58", "59" };
//-------------------------[ getDateString() ]-------------------------//
public String getDateString () {
Date theDate = new Date();
String dayOfWeek = days[theDate.getDay()];
String month = months[theDate.getMonth()];
String dayOfMonth = nums[theDate.getDate()];
String newDate = dayOfWeek + " " + month + " " + dayOfMonth;
return(newDate);
}
//-------------------------[ getTimeString() ]-------------------------//
public String getTimeString () {
Date theDate = new Date();
String hh = nums[theDate.getHours()];
String mm = nums[theDate.getMinutes()];
String ss = nums[theDate.getSeconds()];
String newTime = hh + ":" + mm + ":" + ss;
return(newTime);
}
//-------------------------[ getTimeString() ]-------------------------//
/*
* override the default version of 'update' to help reduce flicker
*
*/
public void update (Graphics g) {
paint(g);
}
//-------------------------[ paint() ]-------------------------//
public void paint(Graphics g) {
// to reduce flicker, draw the old date in the bgColor color
offScreenGraphics.setColor(bgColor);
offScreenGraphics.setFont(theFont);
offScreenGraphics.drawString(date, xDate, yDate);
offScreenGraphics.drawString(time, xTime, yTime);
// now draw all the new info to offScreenGraphics in the textColor
offScreenGraphics.setColor(textColor);
offScreenGraphics.setFont(theFont);
// determine the x/y location of the Date string
int stringHeight = getStringHeight(this, theFont);
date = getDateString();
time = getTimeString();
xDate = getXLoc (this, date, theFont);
yDate = getYLocDate (this, theFont);
// determine the x/y location of the Time string
xTime = getXLoc(this, time, theFont);
yTime = yDate + stringHeight;
offScreenGraphics.drawString(date, xDate, yDate);
offScreenGraphics.drawString(time, xTime, yTime);
// draw the new image all at once to 'g'
g.drawImage(offScreenImage, 0, 0, this);
}
//-------------------------[ start() ]-------------------------//
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
//-------------------------[ stop() ]-------------------------//
public void stop() {
if (runner != null) {
runner.stop();
runner = null;
}
}
//-------------------------[ run() ]-------------------------//
public void run() {
while (true) {
repaint();
try { Thread.sleep(1000); }
catch (InterruptedException e) {}
}
}
//-------------------------[ init() ]-------------------------//
public void init() {
super.init();
// read in the HTML <param> fields
bgColor = GetAppletParameter.toColor(this, "BG_COLOR", Color.white);
textColor = GetAppletParameter.toColor(this, "TEXT_COLOR", Color.black);
fontName = GetAppletParameter.toString(this, "FONT_NAME", "Helvetica");
fontStyle = GetAppletParameter.toInt(this, "FONT_STYLE", Font.PLAIN);
fontSize = GetAppletParameter.toInt(this, "FONT_SIZE", 16);
appletHeight = GetAppletParameter.toInt(this, "HEIGHT", 100);
appletWidth = GetAppletParameter.toInt(this, "WIDTH", 200);
theFont = new Font(fontName, fontStyle, fontSize);
setLayout(null);
addNotify();
resize(appletWidth, appletHeight);
setBackground(bgColor);
// initialize a few values for double-buffering (reduce flicker)
xDate = yDate = xTime = yTime = 0;
date = time = "";
offScreenImage = createImage(appletWidth, appletHeight);
offScreenGraphics = offScreenImage.getGraphics();
}
//---------------------------------------------------------------------//
// getXLoc() - determine the X-location of a String so the String is //
// properly centered on-screen. //
//---------------------------------------------------------------------//
public int getXLoc (Applet a, String s, Font f) {
FontMetrics fontMetrics = a.getFontMetrics(f);
int stringWidth = fontMetrics.stringWidth(s);
// next - assume (!) appletWidth > stringWidth
int extraPixelsOnSides = appletWidth - stringWidth;
int pixelsOnLeft = extraPixelsOnSides / 2;
return(pixelsOnLeft);
}
//-------------------------[ getYLocDate() ]-------------------------//
// Determine the proper y-location for the Date String (i.e., the //
// String that is printed on the top, above the Time). //
//---------------------------------------------------------------------//
public int getYLocDate (Applet a, Font f) {
FontMetrics fm = a.getFontMetrics(f);
int normalHeight = fm.getHeight();
int maxAscent = fm.getMaxAscent();
int extraPixelsTopBtm = appletHeight - (2*normalHeight);
return((extraPixelsTopBtm/2) + maxAscent);
}
//-------------------------[ getStringHeight() ]-------------------------//
public int getStringHeight (Applet a, Font f) {
FontMetrics fontMetrics = a.getFontMetrics(f);
return(fontMetrics.getHeight());
}
//-------------------------[ handleEvent() ]-------------------------//
public boolean handleEvent(Event event) {
return super.handleEvent(event);
}
}