// jclock.java // A java clock. How cliché. // Takes parameters "pointsize" and "fontname" from HTML. // I used thread/sleep. I don't suppose there's a timer event. // // This software comes with NO WARRANTY whatsoever. // It may be used as "free software" per the GNU GPL. // See http://www.gnu.org/copyleft/gpl.html for license information. // Copyright 1999 Demetri Patukas. rev 99.06.19 import java.applet.*; import java.awt.*; import java.util.*; public class jclock extends Applet { /// ----- instance vars private int ptsize = 18; private String fontname = "Helvetica"; private Graphics ggg; private Font font; private CClock clk_thread; /// ----- applet handlers public void init() { ggg = this.getGraphics(); } public void start() { String str; str = this.getParameter("pointsize"); if (str != null) { Integer ii2 = new Integer(str); ptsize = ii2.intValue(); ii2 = null; } str = this.getParameter("fontname"); if (str != null) fontname = str; font = new Font(fontname, Font.BOLD, ptsize); ggg.setFont(font); clk_thread = new CClock(); clk_thread.start(); } public void stop() { clk_thread.runflag = false; clk_thread = null; } public void paint(Graphics g) { update_clock(); } /// ----- private helpers private String digits2(int x) { return ((x < 10) ? "0":"") + x; } private void update_clock() { ggg.clearRect(0, 0, 400, 50); Date dt = new Date(); String str = ""; str += digits2(dt.getHours()) + ":"; str += digits2(dt.getMinutes()) + ":"; str += digits2(dt.getSeconds()); str += " "; str += digits2(dt.getMonth()+1) + "/"; str += digits2(dt.getDate()) + "/"; str += digits2(dt.getYear() % 100); ggg.drawString(str, 10, ptsize); dt = null; } /// ----- local class definition for clock thread. private class CClock extends Thread { boolean runflag; public void run() { runflag = true; while (runflag) { update_clock(); try this.sleep(1000); catch (InterruptedException e) { } } } // When thread.run returns, the thread is (apparently) // destroyed. (cannot just be restarted) } /// ----- end local class CClock } //end class jclock