home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Technology Demos and Tools.iso / java / demo / Clock / Clock2.java < prev    next >
Encoding:
Java Source  |  1996-04-26  |  5.1 KB  |  178 lines

  1. /*
  2.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  6.  * without fee is hereby granted.
  7.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  8.  * for further important copyright and trademark information and to
  9.  * http://java.sun.com/licensing.html for further important licensing
  10.  * information for the Java (tm) Technology.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  20.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  21.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  22.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  23.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  24.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  25.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  26.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  27.  * HIGH RISK ACTIVITIES.
  28.  */
  29.  
  30. // author: Rachel Gollub, 1995
  31. // modified 96/04/24 Jim Hagen : use getBackground()
  32.  
  33. // Time!
  34.  
  35. import java.util.*;
  36. import java.awt.*;
  37. import java.applet.*;
  38.  
  39. public class Clock2 extends Applet implements Runnable {
  40.   Thread timer = null;
  41.   int lastxs=0, lastys=0, lastxm=0, lastym=0, lastxh=0, lastyh=0;
  42.   Date dummy = new Date();
  43.   String lastdate = dummy.toLocaleString();
  44.   
  45. public void init() {
  46.   int x,y;
  47.   resize(300,300);              // Set clock window size
  48. }
  49.  
  50.   // Plotpoints allows calculation to only cover 45 degrees of the circle,
  51.   // and then mirror
  52.  
  53. public void plotpoints(int x0, int y0, int x, int y, Graphics g) {
  54.  
  55.   g.drawLine(x0+x,y0+y,x0+x,y0+y);
  56.   g.drawLine(x0+y,y0+x,x0+y,y0+x);
  57.   g.drawLine(x0+y,y0-x,x0+y,y0-x);
  58.   g.drawLine(x0+x,y0-y,x0+x,y0-y);
  59.   g.drawLine(x0-x,y0-y,x0-x,y0-y);
  60.   g.drawLine(x0-y,y0-x,x0-y,y0-x);
  61.   g.drawLine(x0-y,y0+x,x0-y,y0+x);
  62.   g.drawLine(x0-x,y0+y,x0-x,y0+y);
  63. }
  64.  
  65.   // Circle is just Bresenham's algorithm for a scan converted circle
  66.  
  67. public void circle(int x0, int y0, int r, Graphics g) {
  68.   int x,y;
  69.   float d;
  70.  
  71.   x=0;
  72.   y=r;
  73.   d=5/4-r;
  74.   plotpoints(x0,y0,x,y,g);
  75.  
  76.   while (y>x){
  77.     if (d<0) {
  78.       d=d+2*x+3;
  79.       x++;
  80.     }
  81.     else {
  82.       d=d+2*(x-y)+5;
  83.       x++;
  84.       y--;
  85.     }
  86.     plotpoints(x0,y0,x,y,g);
  87.   }
  88. }
  89.  
  90.  
  91.   // Paint is the main part of the program
  92.  
  93. public void paint(Graphics g) {
  94.   int xh, yh, xm, ym, xs, ys, s, m, h, xcenter, ycenter;
  95.   String today;
  96.   Date dat = new Date();
  97.  
  98.   s = dat.getSeconds();
  99.   m = dat.getMinutes();
  100.   h = dat.getHours();
  101.   today = dat.toLocaleString();
  102.   xcenter=80;
  103.   ycenter=55;
  104.   
  105.   // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00)
  106.   // x = r(cos a) + xcenter, y = r(sin a) + ycenter
  107.   
  108.   xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
  109.   ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
  110.   xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
  111.   ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
  112.   xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + xcenter);
  113.   yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + ycenter);
  114.   
  115.   // Draw the circle and numbers
  116.   
  117.   g.setFont(new Font("TimesRoman", Font.PLAIN, 14));
  118.   g.setColor(Color.blue);
  119.   circle(xcenter,ycenter,50,g);
  120.   g.setColor(Color.darkGray);
  121.   g.drawString("9",xcenter-45,ycenter+3); 
  122.   g.drawString("3",xcenter+40,ycenter+3);
  123.   g.drawString("12",xcenter-5,ycenter-37);
  124.   g.drawString("6",xcenter-3,ycenter+45);
  125.  
  126.   // Erase if necessary, and redraw
  127.   
  128.   g.setColor(getBackground());
  129.   if (xs != lastxs || ys != lastys) {
  130.     g.drawLine(xcenter, ycenter, lastxs, lastys);
  131.     g.drawString(lastdate, 5, 125);
  132.   }
  133.   if (xm != lastxm || ym != lastym) {
  134.     g.drawLine(xcenter, ycenter-1, lastxm, lastym);
  135.     g.drawLine(xcenter-1, ycenter, lastxm, lastym); }
  136.   if (xh != lastxh || yh != lastyh) {
  137.     g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
  138.     g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }
  139.   g.setColor(Color.darkGray);
  140.   g.drawString(today, 5, 125);  
  141.   g.drawLine(xcenter, ycenter, xs, ys);
  142.   g.setColor(Color.blue);
  143.   g.drawLine(xcenter, ycenter-1, xm, ym);
  144.   g.drawLine(xcenter-1, ycenter, xm, ym);
  145.   g.drawLine(xcenter, ycenter-1, xh, yh);
  146.   g.drawLine(xcenter-1, ycenter, xh, yh);
  147.   lastxs=xs; lastys=ys;
  148.   lastxm=xm; lastym=ym;
  149.   lastxh=xh; lastyh=yh;
  150.   lastdate = today;
  151. }
  152.  
  153. public void start() {
  154.   if(timer == null)
  155.     {
  156.       timer = new Thread(this);
  157.       timer.start();
  158.     }
  159. }
  160.  
  161. public void stop() {
  162.   timer = null;
  163. }
  164.  
  165. public void run() {
  166.   while (timer != null) {
  167.     try {Thread.sleep(100);} catch (InterruptedException e){}
  168.     repaint();
  169.   }
  170.   timer = null;
  171. }
  172.  
  173. public void update(Graphics g) {
  174.   paint(g);
  175. }
  176. }
  177.  
  178.