home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unuy2wen / cybcerone / control / clock.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  5.2 KB  |  228 lines

  1. // Clock.java
  2. // 19.03.96
  3. //
  4. // the clock on the control panel
  5.  
  6. package cybcerone.control;
  7.  
  8. import java.awt.Image;
  9. import java.awt.Graphics;
  10. import java.awt.Rectangle;
  11. import java.awt.Point;
  12. import java.awt.Color;
  13. import java.awt.MediaTracker;
  14. import java.awt.Event;
  15.  
  16. import cybcerone.utils.Date;
  17. import cybcerone.utils.Appletlike;
  18. import cybcerone.utils.IdPanel;
  19. import cybcerone.utils.Scaler;
  20.  
  21. /**
  22.  * It's a clock, what more can I say.
  23.  */
  24. class Clock extends IdPanel implements Runnable {
  25.   private static final Rectangle placement = new Rectangle (50, 0, 100, 100);
  26.   
  27.   private static final String id = "clock";
  28.   private static final String statusText = "Current time";
  29.   
  30.   public static final String imagePath = "control/horloge/";
  31.   private static final String bgFile = imagePath + "clock.gif";
  32.  
  33.   Thread timer;
  34.  
  35.   private Image numbers[];
  36.   private Image colon;
  37.   private Point secondPos[];  /* where the dots for the seconds go */
  38.  
  39.   private final int pointSize = 1;
  40.  
  41.   /* to speed up paint () */
  42.   private Image hours1;
  43.   private Image hours2;
  44.   private Image minutes1;
  45.   private Image minutes2;
  46.   private int temp;
  47.   private int seconds;
  48.   private boolean drawLastSecond;
  49.  
  50.   /* for double buffering */
  51.   private Image offscreenImg;
  52.   private Graphics offscreenG;
  53.  
  54.   private final int x1 = scale (17);
  55.   private final int x2 = scale (32);
  56.   private final int x3 = scale (46);
  57.   private final int x4 = scale (53);
  58.   private final int x5 = scale (68);
  59.   private final int y = scale (38);
  60.  
  61.   private boolean small;
  62.  
  63.   public Clock (Appletlike app) {
  64.     super (id, statusText, app);
  65.     reshape (placement);
  66.     
  67.     initImages ();
  68.     initPositions ();
  69.     setForeground (Color.white);
  70.     setBackground (Color.black);
  71.     setBackground (bgFile, 0);
  72.  
  73.     small = (Scaler.getScale () < 1.0);
  74.   }
  75.  
  76.   /**
  77.    * Load up the images used for the numbers.
  78.    */
  79.   private void initImages () {
  80.     numbers = new Image[10];  /* 10 digits and the colon */
  81.     for (int i = 0; i < 10; i++) {
  82.       numbers[i] = app.getImage (imagePath + i + ".gif", 0);
  83.     }
  84.     colon = app.getImage (imagePath + "points.gif", 0);
  85.   }
  86.  
  87.   /**
  88.    * I never was all that good at trig.
  89.    */
  90.   private void initPositions () {
  91.     int w = scale (placement.width - 14);
  92.     double p = Math.PI / 30;
  93.     double h = 0.5;
  94.  
  95.     secondPos = new Point[61];
  96.     
  97.     int w1 = w / 2 - scale (1);
  98.     int w6 = w / 2 - scale (6);
  99.     
  100.     int j = scale (7);
  101.  
  102.     for (int i = 0; i < 61; i++) {
  103.       secondPos[i] = new Point ((int)(w1 + w6 * Math.sin (i * p) + h + j),
  104.                 (int)(w1 - w6 * Math.cos (i * p) + h + j));
  105.     }
  106.   }
  107.  
  108.   /**
  109.    * Start 'er tickin'.
  110.    */
  111.   public void start () {
  112.     if (timer == null) {
  113.       timer = new Thread (this);
  114.       timer.setPriority (Thread.MAX_PRIORITY);
  115.       timer.start ();
  116.     }
  117.   }
  118.  
  119.   /**
  120.    * Lo and behold, time stands still.
  121.    */
  122.   public void stop () {
  123.     if (timer != null) 
  124.       timer.stop ();
  125.     timer = null;
  126.   }
  127.  
  128.   /**
  129.    * Wake up once a second and update yourself.
  130.    */
  131.   public void run () {
  132.     long now;
  133.     long updateTime = 0;
  134.     Date time;
  135.     Point pos;
  136.  
  137.     int hours, minutes;
  138.     int oldSeconds = -2;
  139.  
  140.     while (timer != null) {
  141.       if ((updateTime += 1000) < (now = System.currentTimeMillis ()))
  142.     updateTime = now;
  143.  
  144.       time = new Date ();
  145.       hours = time.getHours ();
  146.       minutes = time.getMinutes ();
  147.       seconds = time.getSeconds ();
  148.  
  149.       hours1 = numbers[hours / 10];
  150.       hours2 = numbers[hours % 10];
  151.       minutes1 = numbers[minutes / 10];
  152.       minutes2 = numbers[minutes % 10];
  153.  
  154.       if (oldSeconds == seconds - 1) {
  155.     drawLastSecond = true;
  156.       } else {
  157.     oldSeconds = seconds;
  158.     drawLastSecond = false;
  159.       }
  160.       repaint ();
  161.       
  162.       try {
  163.     timer.sleep (updateTime - System.currentTimeMillis ());
  164.       } catch (InterruptedException e) {
  165.       }
  166.     }
  167.   }
  168.  
  169.   /**
  170.    * The pretty little clock in the lower left-hand corner.
  171.    */
  172.   public void paint (Graphics g) {
  173.     g.setColor (getBackground ());
  174.     g.fillRect (0, 0, size().width, size().height);
  175.  
  176.     super.paint (g);
  177.     
  178.     g.setColor (getForeground ());
  179.     for (temp = seconds; temp >= 0; temp--) {
  180.       if (small) {
  181.     if (temp % 2 == 0)
  182.       g.fillRect (secondPos[temp].x, secondPos[temp].y,
  183.               pointSize, pointSize);
  184.       } else {
  185.     g.fillRect (secondPos[temp].x, secondPos[temp].y,
  186.             pointSize, pointSize);
  187.       }
  188.     }
  189.  
  190.     g.drawImage (hours1, x1, y, this);
  191.     g.drawImage (hours2, x2, y, this);
  192.     
  193.     g.drawImage (colon, x3, y, this);
  194.     
  195.     g.drawImage (minutes1, x4, y, this);
  196.     g.drawImage (minutes2, x5, y, this);
  197.   }
  198.   
  199.   /**
  200.    * Use clipping and double-buffering to keep the clock moving at a
  201.    * decent pace, without flicker.
  202.    */
  203.   public void update (Graphics g) {
  204.     // for double buffering;
  205.  
  206.     if (offscreenG == null) {
  207.       offscreenImg = createImage (size().width, size().height);
  208.       offscreenG = offscreenImg.getGraphics ();
  209.     }
  210.  
  211.     if (drawLastSecond) {
  212.       g.clipRect (secondPos[seconds].x, secondPos[seconds].y, 
  213.           pointSize, pointSize);
  214.       drawLastSecond = false;
  215.     }
  216.  
  217.     paint (offscreenG);
  218.     g.drawImage (offscreenImg, 0, 0, this);
  219.   }
  220.  
  221.   /** So that the ControlPanel doesn't clear the status bar */
  222.   public boolean mouseMove (Event evt, int x, int y) {
  223.     return true;
  224.   }
  225.  
  226.  
  227. }
  228.