home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / FREI / CLOCK2.EXE / Clock2.java < prev    next >
Encoding:
Java Source  |  1997-09-04  |  6.8 KB  |  228 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. // modified 96/05/29 Rachel Gollub : add garbage collecting
  33. // modified 96/10/22 Rachel Gollub : add bgcolor, fgcolor1, fgcolor2 params
  34. // modified 97/07/30 Chris Bucchere : add getAppletInfo & getParameterInfo
  35. // Time!
  36.  
  37. import java.util.*;
  38. import java.awt.*;
  39. import java.applet.*;
  40. import java.text.*;
  41.  
  42. public class Clock2 extends Applet implements Runnable {
  43.   Thread timer = null;
  44.   int lastxs=0, lastys=0, lastxm=0, lastym=0, lastxh=0, lastyh=0;
  45.   
  46.   SimpleDateFormat formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault());
  47.   Date dummy = new Date();
  48.   String lastdate = formatter.format(dummy);
  49.   
  50.   Font F = new Font("TimesRoman", Font.PLAIN, 14);
  51.   Date dat = null;
  52.   Color fgcol = Color.blue;
  53.   Color fgcol2 = Color.darkGray;
  54.  
  55. public void init() {
  56.   int x,y;
  57.  
  58.   try {
  59.     setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),16)));
  60.   } catch (Exception E) { }
  61.   try {
  62.     fgcol = new Color(Integer.parseInt(getParameter("fgcolor1"),16));
  63.   } catch (Exception E) { }
  64.   try {
  65.     fgcol2 = new Color(Integer.parseInt(getParameter("fgcolor2"),16));
  66.   } catch (Exception E) { }
  67.   resize(300,300);              // Set clock window size
  68. }
  69.  
  70.   // Plotpoints allows calculation to only cover 45 degrees of the circle,
  71.   // and then mirror
  72.  
  73. public void plotpoints(int x0, int y0, int x, int y, Graphics g) {
  74.  
  75.   g.drawLine(x0+x,y0+y,x0+x,y0+y);
  76.   g.drawLine(x0+y,y0+x,x0+y,y0+x);
  77.   g.drawLine(x0+y,y0-x,x0+y,y0-x);
  78.   g.drawLine(x0+x,y0-y,x0+x,y0-y);
  79.   g.drawLine(x0-x,y0-y,x0-x,y0-y);
  80.   g.drawLine(x0-y,y0-x,x0-y,y0-x);
  81.   g.drawLine(x0-y,y0+x,x0-y,y0+x);
  82.   g.drawLine(x0-x,y0+y,x0-x,y0+y);
  83. }
  84.  
  85.   // Circle is just Bresenham's algorithm for a scan converted circle
  86.  
  87. public void circle(int x0, int y0, int r, Graphics g) {
  88.   int x,y;
  89.   float d;
  90.  
  91.   x=0;
  92.   y=r;
  93.   d=5/4-r;
  94.   plotpoints(x0,y0,x,y,g);
  95.  
  96.   while (y>x){
  97.     if (d<0) {
  98.       d=d+2*x+3;
  99.       x++;
  100.     }
  101.     else {
  102.       d=d+2*(x-y)+5;
  103.       x++;
  104.       y--;
  105.     }
  106.     plotpoints(x0,y0,x,y,g);
  107.   }
  108. }
  109.  
  110.  
  111.   // Paint is the main part of the program
  112.  
  113. public void paint(Graphics g) {
  114.   int xh, yh, xm, ym, xs, ys, s = 0, m = 10, h = 10, xcenter, ycenter;
  115.   String today;
  116.  
  117.   dat = new Date();
  118.   SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault());
  119.   try {
  120.     s = Integer.parseInt(formatter.format(dat));
  121.   } catch (NumberFormatException n) {
  122.     s = 0;
  123.   }
  124.   formatter.applyPattern("m");
  125.   try {
  126.     m = Integer.parseInt(formatter.format(dat));
  127.   } catch (NumberFormatException n) {
  128.     m = 10;
  129.   }  
  130.   formatter.applyPattern("h");
  131.   try {
  132.     h = Integer.parseInt(formatter.format(dat));
  133.   } catch (NumberFormatException n) {
  134.     h = 10;
  135.   }
  136.   formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
  137.   today = formatter.format(dat);
  138.   xcenter=80;
  139.   ycenter=55;
  140.   
  141.   // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00)
  142.   // x = r(cos a) + xcenter, y = r(sin a) + ycenter
  143.   
  144.   xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
  145.   ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
  146.   xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
  147.   ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
  148.   xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + xcenter);
  149.   yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + ycenter);
  150.   
  151.   // Draw the circle and numbers
  152.   
  153.   g.setFont(F);
  154.   g.setColor(fgcol);
  155.   circle(xcenter,ycenter,50,g);
  156.   g.setColor(fgcol2);
  157.   g.drawString("9",xcenter-45,ycenter+3); 
  158.   g.drawString("3",xcenter+40,ycenter+3);
  159.   g.drawString("12",xcenter-5,ycenter-37);
  160.   g.drawString("6",xcenter-3,ycenter+45);
  161.  
  162.   // Erase if necessary, and redraw
  163.   
  164.   g.setColor(getBackground());
  165.   if (xs != lastxs || ys != lastys) {
  166.     g.drawLine(xcenter, ycenter, lastxs, lastys);
  167.     g.drawString(lastdate, 5, 125);
  168.   }
  169.   if (xm != lastxm || ym != lastym) {
  170.     g.drawLine(xcenter, ycenter-1, lastxm, lastym);
  171.     g.drawLine(xcenter-1, ycenter, lastxm, lastym); }
  172.   if (xh != lastxh || yh != lastyh) {
  173.     g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
  174.     g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }
  175.   g.setColor(fgcol2);
  176.   g.drawString("", 5, 125);
  177.   g.drawString(today, 5, 125);  
  178.   g.drawLine(xcenter, ycenter, xs, ys);
  179.   g.setColor(fgcol);
  180.   g.drawLine(xcenter, ycenter-1, xm, ym);
  181.   g.drawLine(xcenter-1, ycenter, xm, ym);
  182.   g.drawLine(xcenter, ycenter-1, xh, yh);
  183.   g.drawLine(xcenter-1, ycenter, xh, yh);
  184.   lastxs=xs; lastys=ys;
  185.   lastxm=xm; lastym=ym;
  186.   lastxh=xh; lastyh=yh;
  187.   lastdate = today;
  188.   dat=null;
  189. }
  190.  
  191. public void start() {
  192.   if(timer == null)
  193.     {
  194.       timer = new Thread(this);
  195.       timer.start();
  196.     }
  197. }
  198.  
  199. public void stop() {
  200.   timer = null;
  201. }
  202.  
  203. public void run() {
  204.   while (timer != null) {
  205.     try {Thread.sleep(100);} catch (InterruptedException e){}
  206.     repaint();
  207.   }
  208.   timer = null;
  209. }
  210.  
  211. public void update(Graphics g) {
  212.   paint(g);
  213. }
  214.  
  215.   public String getAppletInfo() {
  216.     return "Title: A Clock \nAuthor: Rachel Gollub, 1995 \nAn analog clock.";
  217.   }
  218.   
  219.   public String[][] getParameterInfo() {
  220.     String[][] info = {
  221.       {"bgcolor", "hexadecimal RGB number", "The background color.  Default is the color of your browser."},
  222.       {"fgcolor1", "hexadecimal RGB number", "The color of the hands and dial.  Default is blue."},
  223.       {"fgcolor2", "hexadecimal RGB number", "The color of the seconds hand and numbers.  Default is dark gray."}
  224.     };
  225.     return info;
  226.   }
  227. }
  228.