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