home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 12,000 to 12,999 / 12000.zip / AOLDLs / Online-Tools / Java-Applets / JAVAAPPS.lzh / JAVAAPPS / CLOCK / CLOCK.EXE / Clock.java < prev   
Encoding:
Java Source  |  1996-04-24  |  3.8 KB  |  80 lines

  1. /* 
  2.  * Ketan's Clock Applet: Inspired by a digital clock I saw somewhere.
  3.  * Permission to copy freely, only send me an email if you do. 
  4.  * Just so I feel good! :) 
  5.  */ 
  6.  
  7. import java.awt.Graphics;
  8. import java.awt.Color;
  9. import java.util.Date;
  10.  
  11. /* Class definition */
  12. public class Clock extends java.applet.Applet implements Runnable {
  13.  
  14.         Thread clockThread;
  15.         int radius;
  16.         int ht, wd;
  17.         String height, width;
  18.         int cx, cy;
  19.  
  20.         public void start() {
  21.                 height = getParameter("height");
  22.                 ht = (height == null) ? 40 : Integer.valueOf(height).intValue();
  23.                 width = getParameter("width");
  24.                 wd = (width == null) ? 40 : Integer.valueOf(width).intValue();
  25.                 radius = (ht < wd) ? ht / 2 : wd / 2;
  26.                 resize(ht, wd);
  27.                 cx = cy = radius;
  28.                 if (clockThread == null) {
  29.                         clockThread = new Thread(this, "Clock");
  30.                         clockThread.start();
  31.                 }
  32.         }
  33.  
  34.         public void run() {
  35.                 while (clockThread != null) {
  36.                         repaint();
  37.                         try {
  38.                                 clockThread.sleep(1000);
  39.                         } catch (InterruptedException e) {
  40.                                 System.out.println("Interrupt Exception");
  41.                         }
  42.                 }
  43.         }
  44.  
  45.         public void paint(Graphics g) {
  46.                 Date now = new Date();
  47.  
  48.                 // Set background color
  49.                 setBackground(Color.gray);
  50.  
  51.                 // Draw the second hand
  52.                 g.setColor(Color.blue);
  53.                 g.drawLine(cx,cy,
  54.                         cx+(int)(Math.round(0.9*radius*Math.sin(now.getSeconds()*3.14/30.0))),                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
  55.                         cy-(int)(Math.round(0.9*radius*Math.cos(now.getSeconds()*3.14/30.0))));
  56.  
  57.                 // Draw the minute hand
  58.                 g.setColor(Color.red);
  59.                 g.drawLine(cx,cy,
  60.                         cx+(int)(Math.round(0.8*radius*Math.sin(now.getMinutes()*3.14/30.0))),
  61.                         cy-(int)(Math.round(0.8*radius*Math.cos(now.getMinutes()*3.14/30.0))));
  62.  
  63.                 // Draw the hour hand
  64.                 g.setColor(Color.yellow);
  65.                 g.drawLine(cx,cy,
  66.                         cx+(int)(Math.round(radius*0.5*Math.sin(
  67.                                         (now.getHours()%12)*3.14/6.0
  68.                                         + (3.14*now.getMinutes()/360.0)))),
  69.                         cy-(int)(Math.round(radius*0.5*Math.cos(
  70.                                         (now.getHours()%12)*3.14/6.0
  71.                                         + (3.14*now.getMinutes()/360.0)))));
  72.         }
  73.  
  74.         public void stop() {
  75.                 clockThread.stop();
  76.                 clockThread = null;
  77.         }
  78. }
  79.  
  80.