blob: 92221126129384154b79352ed54a41e78a86e369 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Sun Microsystems nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 */
34
35import java.util.*;
36import java.awt.*;
37import java.applet.*;
38import java.text.*;
39
40/**
41 * Time!
42 *
43 * @author Rachel Gollub
44 * @modified Daniel Peek replaced circle drawing calculation, few more changes
45 */
46public class Clock extends Applet implements Runnable {
47 private volatile Thread timer; // The thread that displays clock
48 private int lastxs, lastys, lastxm,
49 lastym, lastxh, lastyh; // Dimensions used to draw hands
50 private SimpleDateFormat formatter; // Formats the date displayed
51 private String lastdate; // String to hold date displayed
52 private Font clockFaceFont; // Font for number display on clock
53 private Date currentDate; // Used to get date to display
54 private Color handColor; // Color of main hands and dial
55 private Color numberColor; // Color of second hand and numbers
56 private int xcenter = 80, ycenter = 55; // Center position
57
58 public void init() {
59 int x,y;
60 lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
61 formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy",
62 Locale.getDefault());
63 currentDate = new Date();
64 lastdate = formatter.format(currentDate);
65 clockFaceFont = new Font("Serif", Font.PLAIN, 14);
66 handColor = Color.blue;
67 numberColor = Color.darkGray;
68
69 try {
70 setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),
71 16)));
72 } catch (NullPointerException e) {
73 } catch (NumberFormatException e) {
74 }
75 try {
76 handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),
77 16));
78 } catch (NullPointerException e) {
79 } catch (NumberFormatException e) {
80 }
81 try {
82 numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),
83 16));
84 } catch (NullPointerException e) {
85 } catch (NumberFormatException e) {
86 }
87 resize(300,300); // Set clock window size
88 }
89
90 // Paint is the main part of the program
91 public void update(Graphics g) {
92 int xh, yh, xm, ym, xs, ys;
93 int s = 0, m = 10, h = 10;
94 String today;
95
96 currentDate = new Date();
97
98 formatter.applyPattern("s");
99 try {
100 s = Integer.parseInt(formatter.format(currentDate));
101 } catch (NumberFormatException n) {
102 s = 0;
103 }
104 formatter.applyPattern("m");
105 try {
106 m = Integer.parseInt(formatter.format(currentDate));
107 } catch (NumberFormatException n) {
108 m = 10;
109 }
110 formatter.applyPattern("h");
111 try {
112 h = Integer.parseInt(formatter.format(currentDate));
113 } catch (NumberFormatException n) {
114 h = 10;
115 }
116
117 // Set position of the ends of the hands
118 xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);
119 ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);
120 xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);
121 ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);
122 xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
123 + xcenter);
124 yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
125 + ycenter);
126
127 // Get the date to print at the bottom
128 formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
129 today = formatter.format(currentDate);
130
131 g.setFont(clockFaceFont);
132 // Erase if necessary
133 g.setColor(getBackground());
134 if (xs != lastxs || ys != lastys) {
135 g.drawLine(xcenter, ycenter, lastxs, lastys);
136 g.drawString(lastdate, 5, 125);
137 }
138 if (xm != lastxm || ym != lastym) {
139 g.drawLine(xcenter, ycenter-1, lastxm, lastym);
140 g.drawLine(xcenter-1, ycenter, lastxm, lastym);
141 }
142 if (xh != lastxh || yh != lastyh) {
143 g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
144 g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
145 }
146
147 // Draw date and hands
148 g.setColor(numberColor);
149 g.drawString(today, 5, 125);
150 g.drawLine(xcenter, ycenter, xs, ys);
151 g.setColor(handColor);
152 g.drawLine(xcenter, ycenter-1, xm, ym);
153 g.drawLine(xcenter-1, ycenter, xm, ym);
154 g.drawLine(xcenter, ycenter-1, xh, yh);
155 g.drawLine(xcenter-1, ycenter, xh, yh);
156 lastxs = xs; lastys = ys;
157 lastxm = xm; lastym = ym;
158 lastxh = xh; lastyh = yh;
159 lastdate = today;
160 currentDate = null;
161 }
162
163 public void paint(Graphics g) {
164 g.setFont(clockFaceFont);
165 // Draw the circle and numbers
166 g.setColor(handColor);
167 g.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360);
168 g.setColor(numberColor);
169 g.drawString("9", xcenter-45, ycenter+3);
170 g.drawString("3", xcenter+40, ycenter+3);
171 g.drawString("12", xcenter-5, ycenter-37);
172 g.drawString("6", xcenter-3, ycenter+45);
173
174 // Draw date and hands
175 g.setColor(numberColor);
176 g.drawString(lastdate, 5, 125);
177 g.drawLine(xcenter, ycenter, lastxs, lastys);
178 g.setColor(handColor);
179 g.drawLine(xcenter, ycenter-1, lastxm, lastym);
180 g.drawLine(xcenter-1, ycenter, lastxm, lastym);
181 g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
182 g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
183 }
184
185 public void start() {
186 timer = new Thread(this);
187 timer.start();
188 }
189
190 public void stop() {
191 timer = null;
192 }
193
194 public void run() {
195 Thread me = Thread.currentThread();
196 while (timer == me) {
197 try {
198 Thread.currentThread().sleep(100);
199 } catch (InterruptedException e) {
200 }
201 repaint();
202 }
203 }
204
205 public String getAppletInfo() {
206 return "Title: A Clock \n"
207 + "Author: Rachel Gollub, 1995 \n"
208 + "An analog clock.";
209 }
210
211 public String[][] getParameterInfo() {
212 String[][] info = {
213 {"bgcolor", "hexadecimal RGB number",
214 "The background color. Default is the color of your browser."},
215 {"fgcolor1", "hexadecimal RGB number",
216 "The color of the hands and dial. Default is blue."},
217 {"fgcolor2", "hexadecimal RGB number",
218 "The color of the second hand and numbers. Default is dark gray."}
219 };
220 return info;
221 }
222}