As of: Thu 2010-09-02 18:18:56 UTC [=1283451536. 14:18:56-04:00]
>> clock.java L: 18,182. A: 100444. M: 2004-04-22 17:42:22 UTC [=1082655742] -2324.02d >>
//
// Analog + digital clock Java applet (Coded for Java 1.2; runs also on Java 1.1)
// Brad McCormick . bradmcc@cloud9.net . http://www.cloud9.net/~bradmcc/
// Copyright 1999, 2000, 2002 . Free for non-commercial educational use;
// all other rights reserved.
//
// Revision history:
//
// 2.0 Added 24-hour GMT clock hand
// 2.1 Corrected time acquisition code to use same "moment"
// for computing both local and GMT time
// 2.20 Made second hand extend to both sides of pivot point --
// low screen resolution makes this a little "jerky", but so be it.
// 2.3 Added day_of_month complication.
// 2.5 Added (Made in) "USA" as .gif image -- at 6pt, black type shows as gray.
// 2.6 Removed MyMouseListener inner class and made clock class implement
// MouseListener directly
// 2.9 Add GMT time zone offset to digital time display
// 5.0 Reset version stamp 09 Feb 2002
// 6.0 merge UNIX font size kludge fixup code (v 4.7) into v 5.0 code
// 6.1 improve UNIX font size fudging of vertical position of time text string
// 8.03 Move balls on hands in from ends. 24Jan04
// 9.0 Add Day/Night 12 hr hand indication. 21 Apr 04
// 9.1 Color background ivory and Day/Night ind. yellow. 21 Apr 04
import java.applet.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;
/*
<applet code="clock.class" width=156 height=186>
Sorry, but your web browser needs Java 1.1+ for you to see this clock.
</applet>
*/
public class clock extends Applet implements Runnable, MouseListener {
// ***** Update version number whenever this file changed *****
private String versionID = "v9.22";
private Thread runner, runnerx;
private Font f;
private Color bgc, fgc;
static final private Color Ivory = new Color(255,255,250);
static final private Color SunYellow = new Color(255,255,152);
static final private Color Midnight = new Color(25,25,112);
private int lim;
private int tWidth, tHeight;
private static final int tRate = 1000;
private int runnerstatus;
private Image buffered_image, sunguyImage, USAImage;
private Graphics gbuf;
private int char_ascent;
private int str_width, date_width;
private int halfWidth;
private int tzFudge;
private int digdispy;
private int ydelta;
private boolean imageupdating;
private boolean loading;
private int iheight = 0, iwidth = 0;
private boolean dialog;
private Calendar now;
private Calendar nowGMT;
private int hr24, hr, min, sec, hrx, minx, secx, dx, dy, date_x, date_y, dom;
private String ss, sm, sh, sdom;
private Date thisMoment;
private int fontSizeDelta = 0; // Solaris fonts are *huge*
private int yUNIXfudge = 0;
private String remoteLocation = "http://java.sun.com/";
public void init() {
}
public void start() {
String str = getParameter("goto");
if (str != null) {
remoteLocation = str;
}
now = new GregorianCalendar();
nowGMT = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
dialog = false;
iheight = iwidth = -1;
tWidth = 156;
tHeight = tWidth + (digdispy = 30);
buffered_image = createImage(tWidth, 2*tHeight);
gbuf = buffered_image.getGraphics();
FontMetrics fm;
int fontSize = 10; // Try to cope with Solaris *big* fonts
for (;;) {
f = new Font("Monospaced",Font.PLAIN,fontSize);
gbuf.setFont(f);
fm = gbuf.getFontMetrics(f);
if (fm.stringWidth("000000") > 54) {
break;
}
fontSize += 2;
}
if (fontSize <= 12) {
fontSizeDelta = 12 - fontSize + 2;
}
String osName = System.getProperty("os.name");
String osType = "";
if (osName.equals("AIX")) {
yUNIXfudge = 2;
osType = "a";
} else if (osName.equals("HP-UX")) {
yUNIXfudge = 1;
osType = "h";
} else if (osName.equals("SunOS")) {
yUNIXfudge = 1;
osType = "s";
} else if (osName.equals("Linux")) {
osType = "L";
}
String versionString = versionID + osType;
f = new Font("Monospaced",Font.PLAIN,12 - fontSizeDelta);
gbuf.setFont(f);
fm = gbuf.getFontMetrics(f);
char_ascent = ((fm.getAscent() + fm.getLeading()) * 2 + 1) / 2;
thisMoment = new Date(); // Add in time zone offset text width
now.setTime(thisMoment);
String TZdiffString = getTZdiff(now);
tzFudge = fm.stringWidth(TZdiffString);
str_width = fm.stringWidth("00:00:00") + tzFudge;
tzFudge /= 2;
date_width = fm.stringWidth("00");
gbuf.dispose();
//addMouseListener(new MyMouseListener());
addMouseListener(this);
runnerstatus = 1;
if (runner == null) {
int dx, dy;
gbuf = buffered_image.getGraphics();
gbuf.setFont(f);
gbuf.setColor(Color.gray);
gbuf.fillRect(0,0,tWidth,tHeight);
gbuf.setColor(Ivory);
gbuf.fillArc(8,8,tWidth-16,tWidth-16,0,360);
gbuf.setColor(Color.black);
gbuf.drawOval(8,8,tWidth-16,tWidth-16);
halfWidth = tWidth / 2;
for (dy = 15;dy < 360;dy += 30) {
gbuf.drawLine(halfWidth, halfWidth, halfWidth + getdx(dy,halfWidth -8),
halfWidth + getdy(dy,halfWidth-8));
}
gbuf.setColor(Ivory);
gbuf.fillArc(12,12,tWidth-24,tWidth-24,0,360);
gbuf.setColor(Color.black);
for (dy = 0;dy < 360;dy += 30) {
gbuf.drawLine(halfWidth, halfWidth, halfWidth + getdx(dy,halfWidth -8),
halfWidth + getdy(dy,halfWidth-8));
}
gbuf.setColor(Ivory);
gbuf.fillArc(15,15,tWidth-30,tWidth-30,0,360);
gbuf.setColor(Color.black);
// gbuf.drawArc(12,12,tWidth-24,tWidth-24,0,360);
// gbuf.drawArc(17,17,tWidth-34,tWidth-34,0,360);
//**** Make clock face hour markers at 12, 3, 6, 9 extra long
gbuf.drawLine(halfWidth, 12, halfWidth, 17); // 12:00
gbuf.drawLine(12, halfWidth, 17, halfWidth); // 9:00
gbuf.drawLine(tWidth - 12, halfWidth, tWidth - 17, halfWidth); // 3:00
gbuf.drawLine(halfWidth, tWidth - 12, halfWidth, tWidth - 23); // 6:00
Font fi = new Font("Monospaced",Font.ITALIC+Font.BOLD,16 - fontSizeDelta);
gbuf.setFont(fi);
FontMetrics fmi = gbuf.getFontMetrics(fi);
String jid = "Java";
int str_widthi = fmi.stringWidth(jid);
gbuf.drawString(jid,halfWidth - str_widthi/2,halfWidth-31);
fi = new Font("Monospaced",Font.PLAIN,10 - fontSizeDelta);
gbuf.setFont(fi);
fmi = gbuf.getFontMetrics(fi);
jid = System.getProperty("java.version");
str_widthi = fmi.stringWidth(jid);
int lineheight = fmi.getHeight() - fmi.getDescent();
gbuf.drawString(jid,halfWidth - str_widthi/2,halfWidth-31 + lineheight);
// Write clock version level number to clock face
fi = new Font("Monospaced",Font.PLAIN,10 - fontSizeDelta);
gbuf.setFont(fi);
fmi = gbuf.getFontMetrics(fi);
jid = versionString; // applet revision level
str_widthi = fmi.stringWidth(jid);
gbuf.drawString(jid,halfWidth - str_widthi/2,halfWidth+41);
gbuf.setColor(Color.lightGray);
ydelta = halfWidth - 8 + (digdispy+8) / 2;
gbuf.fillRect(halfWidth-str_width/2-2 - tzFudge, ydelta + halfWidth-char_ascent/2-0,
str_width + 5, char_ascent+ 3);
gbuf.setColor(Color.black);
gbuf.drawRect(halfWidth-str_width/2-3 - tzFudge, ydelta + halfWidth-char_ascent/2-0,
str_width + 6, char_ascent + 3);
gbuf.setColor(Color.white);
gbuf.fillRect((date_x = halfWidth + 45 - date_width/2) - 2, (date_y = halfWidth + 0) - char_ascent/2 + 1,
date_width + 4, char_ascent + 2); // day_of_month "window"
gbuf.setColor(Color.black);
gbuf.drawRect(date_x - 2, date_y - char_ascent/2 + 1,
date_width + 4, char_ascent + 2); // day_of_month "window"
date_y += char_ascent / 2;
loading = true;
repaint();
runner = new Thread(this);
runner.start();
}
}
public void stop() {
if (runner != null) {
runnerx = runner;
runner = null;
runnerstatus = 0;
try {
runnerx.join();
} catch (Exception e) {
}
gbuf.dispose();
// Toolkit.getDefaultToolkit().beep();
}
}
public void destroy() {
stop();
}
public void run() {
//**** Load Java creature image copied from Sun Java website (15 x 12 pixels)
boolean gotimage = true;
sunguyImage = null;
try {
// sunguyImage = getImage(getClass().getResource("sunguy.gif"));
sunguyImage = getImage(getDocumentBase(), "gif/sunguy.gif");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(sunguyImage,0);
tracker.waitForID(0);
iwidth = sunguyImage.getWidth(null);
iheight = sunguyImage.getHeight(null);
} catch (Exception e) {
gotimage = false;
}
if (gotimage) {
gbuf.drawImage(sunguyImage, halfWidth - iwidth /2, halfWidth - 56,
iwidth, iheight, this);
gbuf.setColor(Ivory);
gbuf.fillRect(tWidth - 9 - iwidth, tHeight - 8 - iheight, iwidth + 4, iheight + 4);
gbuf.setColor(Color.black);
gbuf.drawRect(tWidth - 10 - iwidth, tHeight - 9 - iheight, iwidth + 6, iheight + 6);
gbuf.copyArea(halfWidth - iwidth /2, halfWidth - 56, iwidth, iheight,
tWidth - iwidth - 7 - (halfWidth - iwidth /2),
tHeight - iheight - 6 - (halfWidth - 56));
}
//**** Load (Made in) "USA" image -- Should be able to print this, but black type
// so small shows as gray
gotimage = true;
USAImage = null;
try {
// USAImage = getImage(getClass().getResource("USA.gif"));
USAImage = getImage(getDocumentBase(), "gif/USA.gif");
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(USAImage,1);
tracker.waitForID(1);
iwidth = USAImage.getWidth(null);
iheight = USAImage.getHeight(null);
} catch (Exception e) {
gotimage = false;
}
if (gotimage) {
gbuf.drawImage(USAImage, halfWidth - iwidth /2, tWidth - iheight - 2 - 8,
iwidth, iheight, this);
}
gbuf.copyArea(0,0,tWidth,tHeight,0,tHeight);
gbuf.setFont(f);
loading = false;
while (runnerstatus == 1) {
try { Thread.sleep(tRate); }
catch (InterruptedException e) { }
if (!dialog) repaint();
}
}
public void update(Graphics g) {
paint(g);
}
private int getdx(int angle, int len) {
return (int)((len) * Math.cos((angle - 90) * Math.PI / 180));
}
private int getdy(int angle, int len) {
return (int)((len) * Math.sin((angle - 90) * Math.PI / 180));
}
private int getdx_ssec(int angle, int len) {
return (int)(-(len / 5 + 1) * Math.cos((angle - 90) * Math.PI / 180));
}
private int getdy_ssec(int angle, int len) {
return (int)(-(len / 5 + 1) * Math.sin((angle - 90) * Math.PI / 180));
}
public void paint(Graphics g) {
if (loading) {
String jid = System.getProperty("java.version");
Font fL = new Font("Monospaced",Font.BOLD,14 - fontSizeDelta);
g.setColor(Color.lightGray);
g.fillRect(0,0,tWidth,tHeight);
g.setColor(Color.black);
g.setFont(fL);
if (jid.compareTo("1.1") < 0) {
g.drawString("Sorry, Java 1.1", 6,40);
g.drawString("needed. Your", 6,70);
g.drawString("browser has "+jid,6,100);
return;
}
g.drawString("Loading clock.",8,40);
g.drawString("Please wait!",8,70);
g.drawString("Thank you.",8,100);
return;
}
if (dialog) {
Font fL = new Font("Monospaced",Font.BOLD,14 - fontSizeDelta);
g.setColor(Color.lightGray);
g.fillRect(0,0,tWidth,tHeight);
g.setColor(Color.black);
g.drawString("Browser running locally.",8,30);
g.drawString("Cannot connect to",8,55);
g.drawString(remoteLocation + ".",8,80);
// g.drawString("Thank you.",8,105);
g.drawString("Click here to continue...",8,115);
return;
}
thisMoment = new Date(); // Set all clock values from same "moment"
now.setTime(thisMoment);
String TZdiffString = getTZdiff(now);
nowGMT.setTime(thisMoment);
dom = now.get(Calendar.DAY_OF_MONTH);
hr24 = now.get(Calendar.HOUR_OF_DAY);
hr = hr24 > 12 ? hr24 - 12 : hr24;
min = now.get(Calendar.MINUTE);
sec = now.get(Calendar.SECOND);
secx = sec * 6;
minx = min * 6 + sec / 10;
hrx = hr * 30 + min / 2;
sh = new String(hr24 < 10 ? "0" : "");
sm = new String(min < 10 ? "0" : "");
ss = new String(sec < 10 ? "0" : "");
sdom = new String(dom < 10 ? "0" : "");
gbuf.copyArea(0,tHeight,tWidth,tHeight,0,-tHeight); // Restore image background
// Add clock hands in correct "Z-order": 24hr bottom, 12hr next up, min next up, sec top
// Add 24-hour GMT hour hand
// now = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gbuf.setColor(Color.red);
// now.setTimeZone(TimeZone.getTimeZone("GMT")); // (Why doesn't this work?)
int hr24x = nowGMT.get(Calendar.HOUR_OF_DAY) * 15 + nowGMT.get(Calendar.MINUTE) / 4;
gbuf.drawLine(halfWidth, halfWidth, halfWidth + getdx(hr24x,halfWidth - 23), halfWidth + getdy(hr24x,halfWidth - 23)); // hr24
dx = halfWidth + getdx(hr24x,halfWidth - 58);
dy = halfWidth + getdy(hr24x,halfWidth - 58);
gbuf.fillArc(dx-5,dy-5,11,11,0,360);
gbuf.setColor(Color.gray);
gbuf.fillArc(dx-4,dy-4,9,9,0,360);
gbuf.setColor(Color.black);
gbuf.drawLine(halfWidth, halfWidth, halfWidth + getdx(hrx,halfWidth - 25), halfWidth + getdy(hrx,halfWidth - 25)); // hr
dx = halfWidth + getdx(hrx,halfWidth - 51);
dy = halfWidth + getdy(hrx,halfWidth - 51);
int dxMinus5 = dx - 5;
int dyMinus5 = dy - 5;
gbuf.setColor(SunYellow);
gbuf.fillArc(dxMinus5,dyMinus5,11,11,0,360);
gbuf.setColor(Midnight);
// Day
if (hr24 >= 8 && hr24 <= 15) {
// Night
} else if (hr24 <= 3 || hr24 >= 20) {
gbuf.fillArc(dxMinus5,dyMinus5,11,11,0,360);
// Morning
} else if (hr24 < 12) {
gbuf.fillArc(dxMinus5,dyMinus5,11,11,180,180);
// Evening
} else {
gbuf.fillArc(dxMinus5,dyMinus5,11,11,0,180);
gbuf.drawLine(dxMinus5, dyMinus5+5, dxMinus5+11, dyMinus5+5);
}
gbuf.setColor(Color.black);
gbuf.drawArc(dxMinus5,dyMinus5,11,11,0,360);
gbuf.drawLine(halfWidth, halfWidth, halfWidth + getdx(minx,halfWidth - 19), halfWidth + getdy(minx,halfWidth - 19)); // min
dx = halfWidth + getdx(minx,halfWidth - 37);
dy = halfWidth + getdy(minx,halfWidth - 37);
gbuf.fillArc(dx-3,dy-3,7,7,0,360);
gbuf.setColor(Color.blue);
//gbuf.drawLine(halfWidth, halfWidth, (dx = halfWidth + (dx2 = getdx(secx,halfWidth - 16))), //sec
// (dy = halfWidth + (dy2 = getdy(secx,halfWidth - 16))));
gbuf.drawLine((halfWidth + getdx(secx,halfWidth - 16)), (halfWidth + getdy(secx,halfWidth - 16)),
(dx = halfWidth + (getdx_ssec(secx,halfWidth - 16))), //sec (extends on both sides of...
(dy = halfWidth + (getdy_ssec(secx,halfWidth - 16)))); //...pivot point)
gbuf.fillArc(dx-2,dy-2,5,5,0,360);
gbuf.setColor(Color.black);
gbuf.drawString(sh+hr24+":"+sm+min+":"+ss+sec+TZdiffString,halfWidth-str_width/2-tzFudge,
ydelta + halfWidth+char_ascent/2+yUNIXfudge); // digital HH:MM:SS
gbuf.drawString(sdom+dom, date_x, date_y+yUNIXfudge); // day of month
gbuf.fillArc(halfWidth-5, halfWidth-5,10,10,0,360); // ...of clock face
gbuf.setColor(Color.lightGray);
gbuf.fillArc(halfWidth-4, halfWidth-4,8,8,0,360);
g.drawImage(buffered_image, 0, 0, null);
}
//tWidth - 10 - iwidth, tHeight - 11 - iheight, iwidth + 6, iheight + 6);
//class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent me) {
if (iheight == -1 || iwidth == -1) {
return;
}
// System.out.println("MouseListener " + dialog);
if (dialog) {
dialog = false;
repaint();
return;
}
int xoff = me.getX();
int yoff = me.getY();
URL theURL;
// System.out.println("URL is: " + getDocumentBase().getProtocol());
if (xoff >= tWidth - 10 - iwidth && yoff >= tHeight - 9 - iheight &&
xoff <= tWidth - 10 + 6 && yoff <= tHeight - 9 + 6) {
if (getDocumentBase().getProtocol().equalsIgnoreCase("http")) {
try {
theURL = new URL(remoteLocation);
} catch (Exception e) {
return;
}
getAppletContext().showDocument(theURL);
return;
}
dialog = true;
repaint();
}
}
public String getTZdiff(Calendar cal) {
int TZdiff = cal.get(Calendar.DST_OFFSET) + cal.get(Calendar.ZONE_OFFSET);
int TZmdiff = TZdiff % (60 * 60 * 1000);
int TZhdiff = TZdiff / (60 * 60 * 1000);
String TZdiffString = (TZhdiff > 0 ? "+" : "") + TZhdiff;
if (TZmdiff == 0) {
return TZdiffString;
}
if (TZmdiff == (30 * 60 * 1000)) {
return TZdiffString + ".5";
}
return TZdiffString + ":" + (TZmdiff / (60 * 1000));
}
public void mousePressed(MouseEvent me) {
}
public void mouseExited(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
}
public void mouseReleased(MouseEvent me) {
}
//} // end inner class MyMouseListener
} // end class clock
<< clock.java L: 18,182. A: 100444. M: 2004-04-22 17:42:22 UTC [=1082655742] -2324.02d <<
|
| Page generated by: http://www.users.cloud9.net/~bradmcc/cgi-bin/ascii.pl?f=clock.java Copyright © 2001-2005 Brad McCormick, Ed.D. bradmcc@cloud9.net 26 April 2008CE v10.05 |
|||||||||||||||||||||||||||||||||||||||