KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Clock


1 /*
2  * @(#)Clock.java 1.16 06/02/22
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)Clock.java 1.16 06/02/22
39  */

40
41 import java.util.*;
42 import java.awt.*;
43 import java.applet.*;
44 import java.text.*;
45
46 /**
47  * Time!
48  *
49  * @author Rachel Gollub
50  * @modified Daniel Peek replaced circle drawing calculation, few more changes
51  */

52 public class Clock extends Applet implements Runnable JavaDoc {
53     private volatile Thread JavaDoc timer; // The thread that displays clock
54
private int lastxs, lastys, lastxm,
55                 lastym, lastxh, lastyh; // Dimensions used to draw hands
56
private SimpleDateFormat formatter; // Formats the date displayed
57
private String JavaDoc lastdate; // String to hold date displayed
58
private Font clockFaceFont; // Font for number display on clock
59
private Date currentDate; // Used to get date to display
60
private Color handColor; // Color of main hands and dial
61
private Color numberColor; // Color of second hand and numbers
62
private int xcenter = 80, ycenter = 55; // Center position
63

64     public void init() {
65         int x,y;
66         lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
67         formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy",
68                                           Locale.getDefault());
69         currentDate = new Date();
70         lastdate = formatter.format(currentDate);
71         clockFaceFont = new Font("Serif", Font.PLAIN, 14);
72         handColor = Color.blue;
73         numberColor = Color.darkGray;
74
75         try {
76             setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),
77                                                      16)));
78         } catch (NullPointerException JavaDoc e) {
79         } catch (NumberFormatException JavaDoc e) {
80         }
81         try {
82             handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),
83                                                    16));
84         } catch (NullPointerException JavaDoc e) {
85         } catch (NumberFormatException JavaDoc e) {
86         }
87         try {
88             numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),
89                                                      16));
90         } catch (NullPointerException JavaDoc e) {
91         } catch (NumberFormatException JavaDoc e) {
92         }
93         resize(300,300); // Set clock window size
94
}
95
96     // Paint is the main part of the program
97
public void update(Graphics g) {
98         int xh, yh, xm, ym, xs, ys;
99         int s = 0, m = 10, h = 10;
100         String JavaDoc today;
101
102         currentDate = new Date();
103         
104         formatter.applyPattern("s");
105         try {
106             s = Integer.parseInt(formatter.format(currentDate));
107         } catch (NumberFormatException JavaDoc n) {
108             s = 0;
109         }
110         formatter.applyPattern("m");
111         try {
112             m = Integer.parseInt(formatter.format(currentDate));
113         } catch (NumberFormatException JavaDoc n) {
114             m = 10;
115         }
116         formatter.applyPattern("h");
117         try {
118             h = Integer.parseInt(formatter.format(currentDate));
119         } catch (NumberFormatException JavaDoc n) {
120             h = 10;
121         }
122     
123         // Set position of the ends of the hands
124
xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);
125         ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);
126         xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);
127         ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);
128         xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
129                    + xcenter);
130         yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
131                    + ycenter);
132     
133         // Get the date to print at the bottom
134
formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
135         today = formatter.format(currentDate);
136
137         g.setFont(clockFaceFont);
138         // Erase if necessary
139
g.setColor(getBackground());
140         if (xs != lastxs || ys != lastys) {
141             g.drawLine(xcenter, ycenter, lastxs, lastys);
142             g.drawString(lastdate, 5, 125);
143         }
144         if (xm != lastxm || ym != lastym) {
145             g.drawLine(xcenter, ycenter-1, lastxm, lastym);
146             g.drawLine(xcenter-1, ycenter, lastxm, lastym);
147         }
148         if (xh != lastxh || yh != lastyh) {
149             g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
150             g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
151         }
152
153         // Draw date and hands
154
g.setColor(numberColor);
155         g.drawString(today, 5, 125);
156         g.drawLine(xcenter, ycenter, xs, ys);
157         g.setColor(handColor);
158         g.drawLine(xcenter, ycenter-1, xm, ym);
159         g.drawLine(xcenter-1, ycenter, xm, ym);
160         g.drawLine(xcenter, ycenter-1, xh, yh);
161         g.drawLine(xcenter-1, ycenter, xh, yh);
162         lastxs = xs; lastys = ys;
163         lastxm = xm; lastym = ym;
164         lastxh = xh; lastyh = yh;
165         lastdate = today;
166         currentDate = null;
167     }
168
169     public void paint(Graphics g) {
170         g.setFont(clockFaceFont);
171         // Draw the circle and numbers
172
g.setColor(handColor);
173         g.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360);
174         g.setColor(numberColor);
175         g.drawString("9", xcenter-45, ycenter+3);
176         g.drawString("3", xcenter+40, ycenter+3);
177         g.drawString("12", xcenter-5, ycenter-37);
178         g.drawString("6", xcenter-3, ycenter+45);
179
180         // Draw date and hands
181
g.setColor(numberColor);
182         g.drawString(lastdate, 5, 125);
183         g.drawLine(xcenter, ycenter, lastxs, lastys);
184         g.setColor(handColor);
185         g.drawLine(xcenter, ycenter-1, lastxm, lastym);
186         g.drawLine(xcenter-1, ycenter, lastxm, lastym);
187         g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
188         g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
189     }
190
191     public void start() {
192         timer = new Thread JavaDoc(this);
193         timer.start();
194     }
195
196     public void stop() {
197         timer = null;
198     }
199
200     public void run() {
201         Thread JavaDoc me = Thread.currentThread();
202         while (timer == me) {
203             try {
204                 Thread.currentThread().sleep(100);
205             } catch (InterruptedException JavaDoc e) {
206             }
207             repaint();
208         }
209     }
210
211     public String JavaDoc getAppletInfo() {
212         return "Title: A Clock \n"
213             + "Author: Rachel Gollub, 1995 \n"
214             + "An analog clock.";
215     }
216   
217     public String JavaDoc[][] getParameterInfo() {
218         String JavaDoc[][] info = {
219             {"bgcolor", "hexadecimal RGB number",
220              "The background color. Default is the color of your browser."},
221             {"fgcolor1", "hexadecimal RGB number",
222              "The color of the hands and dial. Default is blue."},
223             {"fgcolor2", "hexadecimal RGB number",
224              "The color of the second hand and numbers. Default is dark gray."}
225         };
226         return info;
227     }
228 }
229
Popular Tags