// A Clock with client and server timings. // Amit C. (ConnectSoft Ruksun, Pune, India) amit@maverick.corus.co.in import java.awt.Color ; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.net.Socket; import java.net.URL; import java.io.DataInputStream; import java.io.IOException; import java.util.Date; import java.util.StringTokenizer; // Class NewClock. // The class that displays the current time ands the server time too. public class NewClock extends java.applet.Applet implements Runnable { Thread clockThread ; String Server; String sServerTime; Date ServerTime = new Date(); Color clrTextColor = new Color (255, 255, 255 ) ; Color clrBackGndColor = new Color (0, 0, 0 ) ; String sFontName ; // Text font, max = 72 ? int iFontSize ; String sRemotePrompt, sLocalPrompt; int iRemotePos, iLocalPos; String Text_Pos ; private Image offScreenImage; private Graphics offScreenGraphics; private Dimension offScreenSize; ///////////////////////////////////////////////////////////////////////// // init() - get the server and the time at this server. Get the applet params. public void init() { GetParams (); // Get the info passed in params. // NOTE : font set in above func! // Now set the font specified setFont( new Font( sFontName, Font.PLAIN, iFontSize ) ); // Get the server name from the applet. Server = new String (GetServerName() ); // Use the daytime protocol to get server time sServerTime = GetServerTime (Server ); // Get Time from String!! StringTokenizer st = new StringTokenizer (sServerTime, ":") ; ServerTime.setHours (Integer.valueOf(st.nextToken()).intValue()); ServerTime.setMinutes (Integer.valueOf(st.nextToken()).intValue()); ServerTime.setSeconds (Integer.valueOf(st.nextToken()).intValue()); } public void GetParams() { // get the position of text, Before/After time Text_Pos = new String(MygetStringParam("text_position", "Before" )); // get the prompt for Remote time sRemotePrompt = new String(MygetStringParam("remote_prompt", "" )); // get the position for remote time iRemotePos = MygetintParam ("remote_xpos", 150) ; // get the prompt for Remote time sLocalPrompt = new String(MygetStringParam("local_prompt", "" )); // get the position for remote time iLocalPos = MygetintParam ("local_xpos", 50) ; // Get the specified color for text, else // use black clrTextColor = StringToColor ( MygetStringParam("text_color", null), Color.black ) ; // Get the specified color for background, // else use red clrBackGndColor = StringToColor ( MygetStringParam("background_color", null), Color.white ) ; // get the Font name sFontName = new String(MygetStringParam("font_name", "TimesRoman" )); // get the Font size iFontSize = MygetintParam ("font_size", 14) ; } //////////////////////////////////////////////////////////////////// // start() - The applet starts again, so start the thread too! public void start() { if (clockThread == null) { clockThread = new Thread(this, "Clock"); clockThread.start(); } } //////////////////////////////////////////////////////////////////// // run() - For the thread, repaint and sleep for .8 sec, (so little I tell you, :-) ) public void run() { while (clockThread != null) { repaint(); try { clockThread.sleep(800); // Sleep for 800 millisec } catch (InterruptedException e) { } } } //////////////////////////////////////////////////////////////////// // stop() - The applet has stopped, so kill the thread, we will start again!! Yess!! public void stop() { clockThread.stop(); clockThread = null; } ///////////////////////////////////////////////////////////////////////// // GetServerTime() - Use the server name and daytime protocol to get time! public String GetServerTime (String Server ) // throws java.io.IOException { Socket Client; DataInputStream ServerOut; int port = 13; // The daytime port! String incoming; try { // Open server Client = new Socket (Server, port); // Convert to DataInput to read Server output ServerOut = new DataInputStream(Client.getInputStream()); // Read the first line! incoming = ServerOut.readLine(); Client.close(); } catch (IOException e) { return null; } StringTokenizer st = new StringTokenizer (incoming); while (st.hasMoreTokens() ) // Get the last incoming = new String (st.nextToken()) ; return incoming; } ///////////////////////////////////////////////////////////////////////// // GetServerName() - Get the server name. public String GetServerName () { String BaseURL; // Where the applet is located String Tmp = null; // Get the whole URL BaseURL = new String (getCodeBase().toString() ); // Extract the server from this StringTokenizer st = new StringTokenizer (BaseURL, "//") ; if (st.hasMoreTokens() ) Tmp = new String (st.nextToken()) ; if (st.hasMoreTokens() ) Tmp = new String (st.nextToken()) ; // if ( Tmp.indexOf("www") != -1 ) // Tmp = new String (Tmp.toCharArray(), 4, Tmp.length()-4 ); return Tmp; } //////////////////////////////////////////////////////////////////// // MygetStringParam() - My version of getparam, can be used to get string params. public String MygetStringParam (String att, String def) { String ret; try { ret = getParameter(att); if (ret.length() < 1) return def; else return ret; } catch(Exception e) { return def; } } //////////////////////////////////////////////////////////////////// // MygetintParam() - My version of getparam, can be used to get integer params. public int MygetintParam (String att, int def) { Integer RetParam ; try { RetParam = new Integer (getParameter(att) ) ; if (RetParam.intValue() == 0) return def; else return RetParam.intValue() ; } catch(Exception e) { return def; } } //////////////////////////////////////////////////////////////////// // StringToColor() - Given a 'String' parses it to get the 'Color'. public Color StringToColor (String strColor_p, Color clrDefault_p ) { if (strColor_p.length ( ) == 0) return clrDefault_p ; int r; int g; int b; // Delimiter is ',' StringTokenizer st = new StringTokenizer (strColor_p, ",") ; try { r = Integer.valueOf(st.nextToken()).intValue(); g = Integer.valueOf(st.nextToken()).intValue(); b = Integer.valueOf(st.nextToken()).intValue(); return new Color(r,g,b); } catch (Exception e) { return clrDefault_p; } } //////////////////////////////////////////////////////////////////// // getAppletInfo() - Returns all info, hope Netscape will support it one day! public String getAppletInfo() { return "Clock2, a Java Applet by Amit Chaudhary \n Email -> (amit@maverick.corus.co.in), 27/1/96"; } public void paint(Graphics g) { String sTimeNow ; // showStatus ("Running out of time here..") ; g.setColor (clrBackGndColor ) ; // The background specified g.fillRect (0, 0, size().width, size().height ) ; g.setColor (clrTextColor ) ; // The text color specified // Cheating here by just incrementing by 1 sec, // All in a good cause. saves a socket call. int Seconds = ServerTime.getSeconds(); if (Seconds >= 59 ) { ServerTime.setSeconds (0 ); int Minutes = ServerTime.getMinutes(); if (Minutes >= 59 ) { ServerTime.setMinutes (0 ); int Hours = ServerTime.getHours( ); ServerTime.setHours (Hours >= 24? 0: Hours+1 ); } else ServerTime.setMinutes (Minutes + 1 ); } else ServerTime.setSeconds (Seconds + 1 ); sServerTime = ServerTime.getHours() + ":" + ServerTime.getMinutes()/10 + ServerTime.getMinutes()%10 + ":" + ServerTime.getSeconds()/10 + ServerTime.getSeconds()%10; Date now = new Date(); // Get current time here, now! sTimeNow = now.getHours() + ":" + now.getMinutes()/10 + now.getMinutes()%10 + ":" + now.getSeconds()/10 + now.getSeconds()%10; if (Text_Pos.compareTo ("Before") == 0 ) { g.drawString (sRemotePrompt + " " + sServerTime, 10, iRemotePos ) ; g.drawString (sLocalPrompt + " " + sTimeNow, 10, iLocalPos ) ; } else { g.drawString (sServerTime + " " + sRemotePrompt, 10, iRemotePos ) ; g.drawString (sTimeNow + " " + sLocalPrompt, 10, iLocalPos ) ; } } //////////////////////////////////////////////////////////////////// // update() - Uses double buffering for flicker free update public final synchronized void update (Graphics theG) { Dimension d = size(); if((offScreenImage == null) || (d.width != offScreenSize.width) || (d.height != offScreenSize.height)) { offScreenImage = createImage(d.width, d.height); offScreenSize = d; offScreenGraphics = offScreenImage.getGraphics(); offScreenGraphics.setFont(getFont()); } offScreenGraphics.fillRect(0,0,d.width, d.height); paint(offScreenGraphics); theG.drawImage(offScreenImage, 0, 0, null); } }