import java.awt.*; import java.net.*; import java.lang.Math; import java.lang.Integer; import java.util.Date; public class Clock extends java.applet.Applet implements Runnable { Thread runner; //thread for applet Color facecolor, clockcolor, //color for clock face and markings fontcolor; //color for text displayed on clock int secx, secy, //coordinates for second hand minx, miny, //coordinates for minute hand hrx, hry, //coordinates for hour hand tzoneoffset = 0, //offset for local time zone gmtoffset = 0; //offset for time to display boolean statusBar, //flag for displaying on the status bar tickmarks, //flag for 5 min. tick mark display sechand; //flag for second hand display URL lclick = null; //the execution string for a left-click public Color nameToColor(String name) { //This function converts a color name String to a Color //This takes a string form of a color name and converts it to a color Color color; if (name.equalsIgnoreCase("blue")) color = Color.blue; else if (name.equalsIgnoreCase("cyan")) color = Color.cyan; else if (name.equalsIgnoreCase("darkGray")) color = Color.darkGray; else if (name.equalsIgnoreCase("darkGrey")) color = Color.darkGray; else if (name.equalsIgnoreCase("gray")) color = Color.gray; else if (name.equalsIgnoreCase("grey")) color = Color.gray; else if (name.equalsIgnoreCase("green")) color = Color.green; else if (name.equalsIgnoreCase("lightGray")) color = Color.lightGray; else if (name.equalsIgnoreCase("lightGrey")) color = Color.lightGray; else if (name.equalsIgnoreCase("magenta")) color = Color.magenta; else if (name.equalsIgnoreCase("orange")) color = Color.orange; else if (name.equalsIgnoreCase("pink")) color = Color.pink; else if (name.equalsIgnoreCase("red")) color = Color.red; else if (name.equalsIgnoreCase("white")) color = Color.white; else if (name.equalsIgnoreCase("yellow")) color = Color.yellow; else color = Color.black; return color; } //end of Clock.Color(String) public void init() { //init routine gets variables and initializes variables Date curDate; //holder of current time/date String tstr; Integer toffset; // Determine the color of the clock's face (default white) tstr = getParameter("facecolor"); if (tstr == null) tstr="white"; this.facecolor=nameToColor(tstr); // Determine the color of the clock's markings (default black) tstr = getParameter("clockcolor"); if (tstr == null) tstr="black"; this.clockcolor=nameToColor(tstr); // Determine the color of the clock's text (default grey) tstr = getParameter("fontcolor"); if (tstr == null) tstr="grey"; this.fontcolor=nameToColor(tstr); // Determine the color of the applet's background tstr = getParameter("background"); if (tstr == null) tstr="white"; this.setBackground(nameToColor(tstr)); // Determine if the second hand is to be shown tstr = getParameter("sechand"); if (tstr != null && tstr.equalsIgnoreCase("false")) this.sechand = false; else this.sechand = true; // Determine if the second hand is to be shown tstr = getParameter("tickmarks"); if (tstr != null && tstr.equalsIgnoreCase("true")) this.tickmarks = true; else this.tickmarks = false; // Determine the leftclick action tstr = getParameter("lclick"); if (tstr != null) try { lclick = new URL(tstr); } catch (MalformedURLException e) { System.out.println ("Bad URL: " + lclick); } // Determine the time to show based on the gmtoffset parameter passed // in by the html document. A "5" would be equal to EST. A "7" would // be equal to MST. Etc... // Since this is a constant, daylight time is adjusted by your adding // a 1 to the standard time value when you specify the value in your // html document. curDate = new Date(); tzoneoffset = curDate.getTimezoneOffset() / 60; tstr = getParameter("gmtoffset"); if (tstr == null) gmtoffset = tzoneoffset; else { toffset = new Integer(tstr); gmtoffset = toffset.intValue(); } } //end of Clock.init() public void start() { //start routine for applet if (runner==null) { runner = new Thread(this); runner.start(); } } //end of Clock.start() public void stop() { //stop routine for applet if (runner != null) { runner.stop(); runner = null; } } //end of Clock.stop() public void run() { //paint routine for applet while (true) { painthands(this.getGraphics()); try { Thread.sleep(1000); } catch (InterruptedException e) { } } } //end of Clock.run() public void paint(Graphics g) { //paint routine for applet paintclock(g); painthands(g); } //end of Clock.paint(Graphics) public void paintclock(Graphics g) { //paint routine for the clock markings int ratio = 20, diameter = this.size().height fm.stringWidth(am) ? fm.stringWidth(pm) : fm.stringWidth(am); // remove the hands to be redrawn if (hrx != 0 && hry !=0) { g.setColor(this.facecolor); g.drawLine(radius, radius, this.hrx, this.hry); g.drawLine(radius-1, radius, this.hrx, this.hry); g.drawLine(radius+1, radius, this.hrx, this.hry); g.drawLine(radius, radius-1, this.hrx, this.hry); g.drawLine(radius, radius+1, this.hrx, this.hry); } if (minx != 0 && miny != 0) { g.setColor(this.facecolor); g.drawLine(radius, radius, this.minx, this.miny); g.drawLine(radius-1, radius, this.minx, this.miny); g.drawLine(radius+1, radius, this.minx, this.miny); g.drawLine(radius, radius-1, this.minx, this.miny); g.drawLine(radius, radius+1, this.minx, this.miny); } if (this.sechand && secx != 0 && secy != 0) { g.setColor(this.facecolor); g.drawLine(radius, radius, this.secx, this.secy); } //redraw am/pm tmpx = (int)(radius-(strwidth/2)); tmpy = (int)(radius+1); g.setFont(f); g.setColor(this.facecolor); g.fillRect(tmpx, tmpy, strwidth, fm.getHeight()); g.setColor(this.fontcolor); if (hours >= 12) g.drawString(pm, tmpx, tmpy+fm.getAscent()); else g.drawString(am, tmpx, tmpy+fm.getAscent()); // Draw Hour hand cosi = Math.cos(((Math.PI/6)*hours)-(Math.PI/2))+1; sini = Math.sin(((Math.PI/6)*hours)-(Math.PI/2))+1; this.hrx = (int)((cosi*(diameter-(marksize*12))/2)+(marksize*12/2)); this.hry = (int)((sini*(diameter-(marksize*12))/2)+(marksize*12/2)); g.setColor(this.clockcolor); g.drawLine(radius, radius, this.hrx, this.hry); g.drawLine(radius-1, radius, this.hrx, this.hry); g.drawLine(radius+1, radius, this.hrx, this.hry); g.drawLine(radius, radius-1, this.hrx, this.hry); g.drawLine(radius, radius+1, this.hrx, this.hry); // Draw Minute Hand cosi = Math.cos(((Math.PI/30)*minutes)-(Math.PI/2))+1; sini = Math.sin(((Math.PI/30)*minutes)-(Math.PI/2))+1; this.minx = (int)((cosi*(diameter-(marksize*4))/2)+(marksize*2)); this.miny = (int)((sini*(diameter-(marksize*4))/2)+(marksize*2)); g.setColor(this.clockcolor); g.drawLine(radius, radius, this.minx, this.miny); g.drawLine(radius-1, radius, this.minx, this.miny); g.drawLine(radius+1, radius, this.minx, this.miny); g.drawLine(radius, radius-1, this.minx, this.miny); g.drawLine(radius, radius+1, this.minx, this.miny); // Draw second hand cosi = Math.cos(((Math.PI/30)*seconds)-(Math.PI/2))+1; sini = Math.sin(((Math.PI/30)*seconds)-(Math.PI/2))+1; this.secx = (int)((cosi*(diameter-(marksize*4))/2)+(marksize*2)); this.secy = (int)((sini*(diameter-(marksize*4))/2)+(marksize*2)); if (this.sechand) { g.setColor(Color.red); g.drawLine(radius, radius, this.secx, this.secy); } // statusBar display if (this.statusBar) getAppletContext().showStatus(date.toLocaleString()); } public boolean mouseEnter(Event evt, int x, int y) { //routine to handle mouseEnter events Date date = new Date(); getAppletContext().showStatus(date.toLocaleString()); this.statusBar = true; return true; } // end of mouseEnter(Event, int, int) public boolean mouseExit(Event evt, int x, int y) { //routine to handle mouseExit events getAppletContext().showStatus(""); this.statusBar = false; return true; } // end of mouseEnter(Event, int, int) public boolean mouseUp(Event evt, int x, int y) { //routine to handle mouseUp events if ( this.lclick != null && this.statusBar == true ) getAppletContext().showDocument(lclick); return true; } // end of mouseUp(Event, int, int) }