001: /*
002: * Copyright (c) 2002 Sun Microsystems, Inc. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * -Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * -Redistribution in binary form must reproduct the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the distribution.
014: *
015: * Neither the name of Sun Microsystems, Inc. or the names of contributors
016: * may be used to endorse or promote products derived from this software
017: * without specific prior written permission.
018: *
019: * This software is provided "AS IS," without a warranty of any kind. ALL
020: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
021: * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
022: * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
023: * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
024: * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
025: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
026: * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
027: * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
028: * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
029: * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
030: *
031: * You acknowledge that Software is not designed, licensed or intended for
032: * use in the design, construction, operation or maintenance of any nuclear
033: * facility.
034: */
035:
036: /*
037: * @(#)Clock.java 1.11 02/06/13
038: */
039: /**
040: * Included without modification (except package name) as applet demonstration
041: * for JSF applet tag in Sakai.
042: *
043: * @modification "package example;" $Id $
044: */package example;
045:
046: import java.util.*;
047: import java.awt.*;
048: import java.applet.*;
049: import java.text.*;
050:
051: /**
052: * Time!
053: *
054: * @author Rachel Gollub
055: * @modified Daniel Peek replaced circle drawing calculation, few more changes
056: */
057: public class Clock extends Applet implements Runnable {
058: private volatile Thread timer; // The thread that displays clock
059: private int lastxs, lastys, lastxm, lastym, lastxh, lastyh; // Dimensions used to draw hands
060: private SimpleDateFormat formatter; // Formats the date displayed
061: private String lastdate; // String to hold date displayed
062: private Font clockFaceFont; // Font for number display on clock
063: private Date currentDate; // Used to get date to display
064: private Color handColor; // Color of main hands and dial
065: private Color numberColor; // Color of second hand and numbers
066: private int xcenter = 80, ycenter = 55; // Center position
067:
068: public void init() {
069: int x, y;
070: lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
071: formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss yyyy",
072: Locale.getDefault());
073: currentDate = new Date();
074: lastdate = formatter.format(currentDate);
075: clockFaceFont = new Font("Serif", Font.PLAIN, 14);
076: handColor = Color.blue;
077: numberColor = Color.darkGray;
078:
079: try {
080: setBackground(new Color(Integer.parseInt(
081: getParameter("bgcolor"), 16)));
082: } catch (NullPointerException e) {
083: } catch (NumberFormatException e) {
084: }
085: try {
086: handColor = new Color(Integer.parseInt(
087: getParameter("fgcolor1"), 16));
088: } catch (NullPointerException e) {
089: } catch (NumberFormatException e) {
090: }
091: try {
092: numberColor = new Color(Integer.parseInt(
093: getParameter("fgcolor2"), 16));
094: } catch (NullPointerException e) {
095: } catch (NumberFormatException e) {
096: }
097: resize(300, 300); // Set clock window size
098: }
099:
100: // Paint is the main part of the program
101: public void update(Graphics g) {
102: int xh, yh, xm, ym, xs, ys;
103: int s = 0, m = 10, h = 10;
104: String today;
105:
106: currentDate = new Date();
107:
108: formatter.applyPattern("s");
109: try {
110: s = Integer.parseInt(formatter.format(currentDate));
111: } catch (NumberFormatException n) {
112: s = 0;
113: }
114: formatter.applyPattern("m");
115: try {
116: m = Integer.parseInt(formatter.format(currentDate));
117: } catch (NumberFormatException n) {
118: m = 10;
119: }
120: formatter.applyPattern("h");
121: try {
122: h = Integer.parseInt(formatter.format(currentDate));
123: } catch (NumberFormatException n) {
124: h = 10;
125: }
126:
127: // Set position of the ends of the hands
128: xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);
129: ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);
130: xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);
131: ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);
132: xh = (int) (Math.cos((h * 30 + m / 2) * Math.PI / 180 - Math.PI
133: / 2) * 30 + xcenter);
134: yh = (int) (Math.sin((h * 30 + m / 2) * Math.PI / 180 - Math.PI
135: / 2) * 30 + ycenter);
136:
137: // Get the date to print at the bottom
138: formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
139: today = formatter.format(currentDate);
140:
141: g.setFont(clockFaceFont);
142: // Erase if necessary
143: g.setColor(getBackground());
144: if (xs != lastxs || ys != lastys) {
145: g.drawLine(xcenter, ycenter, lastxs, lastys);
146: g.drawString(lastdate, 5, 125);
147: }
148: if (xm != lastxm || ym != lastym) {
149: g.drawLine(xcenter, ycenter - 1, lastxm, lastym);
150: g.drawLine(xcenter - 1, ycenter, lastxm, lastym);
151: }
152: if (xh != lastxh || yh != lastyh) {
153: g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);
154: g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);
155: }
156:
157: // Draw date and hands
158: g.setColor(numberColor);
159: g.drawString(today, 5, 125);
160: g.drawLine(xcenter, ycenter, xs, ys);
161: g.setColor(handColor);
162: g.drawLine(xcenter, ycenter - 1, xm, ym);
163: g.drawLine(xcenter - 1, ycenter, xm, ym);
164: g.drawLine(xcenter, ycenter - 1, xh, yh);
165: g.drawLine(xcenter - 1, ycenter, xh, yh);
166: lastxs = xs;
167: lastys = ys;
168: lastxm = xm;
169: lastym = ym;
170: lastxh = xh;
171: lastyh = yh;
172: lastdate = today;
173: currentDate = null;
174: }
175:
176: public void paint(Graphics g) {
177: g.setFont(clockFaceFont);
178: // Draw the circle and numbers
179: g.setColor(handColor);
180: g.drawArc(xcenter - 50, ycenter - 50, 100, 100, 0, 360);
181: g.setColor(numberColor);
182: g.drawString("9", xcenter - 45, ycenter + 3);
183: g.drawString("3", xcenter + 40, ycenter + 3);
184: g.drawString("12", xcenter - 5, ycenter - 37);
185: g.drawString("6", xcenter - 3, ycenter + 45);
186:
187: // Draw date and hands
188: g.setColor(numberColor);
189: g.drawString(lastdate, 5, 125);
190: g.drawLine(xcenter, ycenter, lastxs, lastys);
191: g.setColor(handColor);
192: g.drawLine(xcenter, ycenter - 1, lastxm, lastym);
193: g.drawLine(xcenter - 1, ycenter, lastxm, lastym);
194: g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);
195: g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);
196: }
197:
198: public void start() {
199: timer = new Thread(this );
200: timer.start();
201: }
202:
203: public void stop() {
204: timer = null;
205: }
206:
207: public void run() {
208: Thread me = Thread.currentThread();
209: while (timer == me) {
210: try {
211: Thread.sleep(100);
212: } catch (InterruptedException e) {
213: }
214: repaint();
215: }
216: }
217:
218: public String getAppletInfo() {
219: return "Title: A Clock \n" + "Author: Rachel Gollub, 1995 \n"
220: + "An analog clock.";
221: }
222:
223: public String[][] getParameterInfo() {
224: String[][] info = {
225: { "bgcolor", "hexadecimal RGB number",
226: "The background color. Default is the color of your browser." },
227: { "fgcolor1", "hexadecimal RGB number",
228: "The color of the hands and dial. Default is blue." },
229: { "fgcolor2", "hexadecimal RGB number",
230: "The color of the second hand and numbers. Default is dark gray." } };
231: return info;
232: }
233: }
|