1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 import java.applet.Applet;
19 import java.awt.Color;
20 import java.awt.Font;
21 import java.awt.Graphics;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 import java.util.Locale;
25 
26 /**
27  * Time!
28  *
29  * @author Rachel Gollub
30  */
31 
32 public class Clock2 extends Applet implements Runnable {
33     private static final long serialVersionUID = 1L;
34     Thread timer;                // The thread that displays clock
35     int lastxs, lastys, lastxm,
36         lastym, lastxh, lastyh;  // Dimensions used to draw hands
37     SimpleDateFormat formatter;  // Formats the date displayed
38     String lastdate;             // String to hold date displayed
39     Font clockFaceFont;          // Font for number display on clock
40     Date currentDate;            // Used to get date to display
41     Color handColor;             // Color of main hands and dial
42     Color numberColor;           // Color of second hand and numbers
43 
44     @Override
init()45     public void init() {
46         lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
47         formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault());
48         currentDate = new Date();
49         lastdate = formatter.format(currentDate);
50         clockFaceFont = new Font("Serif", Font.PLAIN, 14);
51         handColor = Color.blue;
52         numberColor = Color.darkGray;
53 
54         try {
55             setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),16)));
56         } catch (Exception e) {
57             // Ignored
58         }
59         try {
60             handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),16));
61         } catch (Exception e) {
62             // Ignored
63         }
64         try {
65             numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),16));
66         } catch (Exception e) {
67             // Ignored
68         }
69         resize(300,300);              // Set clock window size
70     }
71 
72     // Plotpoints allows calculation to only cover 45 degrees of the circle,
73     // and then mirror
plotpoints(int x0, int y0, int x, int y, Graphics g)74     public void plotpoints(int x0, int y0, int x, int y, Graphics g) {
75         g.drawLine(x0+x,y0+y,x0+x,y0+y);
76         g.drawLine(x0+y,y0+x,x0+y,y0+x);
77         g.drawLine(x0+y,y0-x,x0+y,y0-x);
78         g.drawLine(x0+x,y0-y,x0+x,y0-y);
79         g.drawLine(x0-x,y0-y,x0-x,y0-y);
80         g.drawLine(x0-y,y0-x,x0-y,y0-x);
81         g.drawLine(x0-y,y0+x,x0-y,y0+x);
82         g.drawLine(x0-x,y0+y,x0-x,y0+y);
83     }
84 
85     // Circle is just Bresenham's algorithm for a scan converted circle
circle(int x0, int y0, int r, Graphics g)86     public void circle(int x0, int y0, int r, Graphics g) {
87         int x,y;
88         float d;
89         x=0;
90         y=r;
91         d=5/4-r;
92         plotpoints(x0,y0,x,y,g);
93 
94         while (y>x){
95             if (d<0) {
96                 d=d+2*x+3;
97                 x++;
98             } else {
99                 d=d+2*(x-y)+5;
100                 x++;
101                 y--;
102             }
103             plotpoints(x0,y0,x,y,g);
104         }
105     }
106 
107     // Paint is the main part of the program
108     @Override
paint(Graphics g)109     public void paint(Graphics g) {
110         int xh, yh, xm, ym, xs, ys, s = 0, m = 10, h = 10, xcenter, ycenter;
111         String today;
112 
113         currentDate = new Date();
114         SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault());
115         try {
116             s = Integer.parseInt(formatter.format(currentDate));
117         } catch (NumberFormatException n) {
118             s = 0;
119         }
120         formatter.applyPattern("m");
121         try {
122             m = Integer.parseInt(formatter.format(currentDate));
123         } catch (NumberFormatException n) {
124             m = 10;
125         }
126         formatter.applyPattern("h");
127         try {
128             h = Integer.parseInt(formatter.format(currentDate));
129         } catch (NumberFormatException n) {
130             h = 10;
131         }
132         formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
133         today = formatter.format(currentDate);
134         xcenter=80;
135         ycenter=55;
136 
137     // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00)
138     // x = r(cos a) + xcenter, y = r(sin a) + ycenter
139 
140         xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
141         ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
142         xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
143         ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
144         xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + xcenter);
145         yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + ycenter);
146 
147     // Draw the circle and numbers
148 
149         g.setFont(clockFaceFont);
150         g.setColor(handColor);
151         circle(xcenter,ycenter,50,g);
152         g.setColor(numberColor);
153         g.drawString("9",xcenter-45,ycenter+3);
154         g.drawString("3",xcenter+40,ycenter+3);
155         g.drawString("12",xcenter-5,ycenter-37);
156         g.drawString("6",xcenter-3,ycenter+45);
157 
158     // Erase if necessary, and redraw
159 
160         g.setColor(getBackground());
161         if (xs != lastxs || ys != lastys) {
162             g.drawLine(xcenter, ycenter, lastxs, lastys);
163             g.drawString(lastdate, 5, 125);
164         }
165         if (xm != lastxm || ym != lastym) {
166             g.drawLine(xcenter, ycenter-1, lastxm, lastym);
167             g.drawLine(xcenter-1, ycenter, lastxm, lastym); }
168         if (xh != lastxh || yh != lastyh) {
169             g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
170             g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }
171         g.setColor(numberColor);
172         g.drawString("", 5, 125);
173         g.drawString(today, 5, 125);
174         g.drawLine(xcenter, ycenter, xs, ys);
175         g.setColor(handColor);
176         g.drawLine(xcenter, ycenter-1, xm, ym);
177         g.drawLine(xcenter-1, ycenter, xm, ym);
178         g.drawLine(xcenter, ycenter-1, xh, yh);
179         g.drawLine(xcenter-1, ycenter, xh, yh);
180         lastxs=xs; lastys=ys;
181         lastxm=xm; lastym=ym;
182         lastxh=xh; lastyh=yh;
183         lastdate = today;
184         currentDate=null;
185     }
186 
187     @Override
start()188     public void start() {
189         timer = new Thread(this);
190         timer.start();
191     }
192 
193     @Override
stop()194     public void stop() {
195         timer = null;
196     }
197 
198     @Override
run()199     public void run() {
200         Thread me = Thread.currentThread();
201         while (timer == me) {
202             try {
203                 Thread.sleep(100);
204             } catch (InterruptedException e) {
205             }
206             repaint();
207         }
208     }
209 
210     @Override
update(Graphics g)211     public void update(Graphics g) {
212         paint(g);
213     }
214 
215     @Override
getAppletInfo()216     public String getAppletInfo() {
217         return "Title: A Clock \nAuthor: Rachel Gollub, 1995 \nAn analog clock.";
218     }
219 
220     @Override
getParameterInfo()221     public String[][] getParameterInfo() {
222         String[][] info = {
223             {"bgcolor", "hexadecimal RGB number", "The background color. Default is the color of your browser."},
224             {"fgcolor1", "hexadecimal RGB number", "The color of the hands and dial. Default is blue."},
225             {"fgcolor2", "hexadecimal RGB number", "The color of the seconds hand and numbers. Default is dark gray."}
226         };
227         return info;
228     }
229 }
230