home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / Clock2.java < prev    next >
Text File  |  1997-07-30  |  6KB  |  202 lines

  1. // $Header: z:/admin/metro_examples/java/demo/ClockTest/rcs/Clock2.java 1.2 1997/03/30 01:05:40 NPatel Exp $ 
  2. /*
  3.  * @(#)Clock2.java    1.6 96/12/06
  4.  *
  5.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  6.  *
  7.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  8.  * modify and redistribute this software in source and binary code form,
  9.  * provided that i) this copyright notice and license appear on all copies of
  10.  * the software; and ii) Licensee does not utilize the software in a manner
  11.  * which is disparaging to Sun.
  12.  *
  13.  * This software is provided "AS IS," without a warranty of any kind. ALL
  14.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  15.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  16.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  17.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  18.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  19.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  20.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  21.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  22.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  23.  * POSSIBILITY OF SUCH DAMAGES.
  24.  *
  25.  * This software is not designed or intended for use in on-line control of
  26.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  27.  * the design, construction, operation or maintenance of any nuclear
  28.  * facility. Licensee represents and warrants that it will not use or
  29.  * redistribute the Software for such purposes.
  30.  */
  31.  
  32. // author: Rachel Gollub, 1995
  33. // modified 96/04/24 Jim Hagen : use getBackground()
  34. // modified 96/05/29 Rachel Gollub : add garbage collecting
  35. // modified 96/10/22 Rachel Gollub : add bgcolor, fgcolor1, fgcolor2 params
  36. // modified 96/12/05 Rachel Gollub : change copyright and Date methods
  37. // Time!
  38.  
  39. import java.util.*;
  40. import java.awt.*;
  41. import java.applet.*;
  42. import java.text.*;
  43.  
  44. public class Clock2 extends Applet implements Runnable {
  45.   Thread timer = null;
  46.   int lastxs=0, lastys=0, lastxm=0, lastym=0, lastxh=0, lastyh=0;
  47.   Date dummy = new Date();
  48.   GregorianCalendar cal = new GregorianCalendar();
  49.   SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
  50.   String lastdate = df.format(dummy);
  51.   Font F = new Font("TimesRoman", Font.PLAIN, 14);
  52.   Date dat = null;
  53.   Color fgcol = Color.blue;
  54.   Color fgcol2 = Color.darkGray;
  55.  
  56. public void init() {
  57.   int x,y;
  58.  
  59.   try {
  60.     setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),16)));
  61.   } catch (Exception E) { }
  62.   try {
  63.     fgcol = new Color(Integer.parseInt(getParameter("fgcolor1"),16));
  64.   } catch (Exception E) { }
  65.   try {
  66.     fgcol2 = new Color(Integer.parseInt(getParameter("fgcolor2"),16));
  67.   } catch (Exception E) { }
  68.   resize(300,300);              // Set clock window size
  69. }
  70.  
  71.   // Plotpoints allows calculation to only cover 45 degrees of the circle,
  72.   // and then mirror
  73.  
  74. public void plotpoints(int x0, int y0, int x, int y, Graphics g) {
  75.  
  76.   g.drawLine(x0+x,y0+y,x0+x,y0+y);
  77.   g.drawLine(x0+y,y0+x,x0+y,y0+x);
  78.   g.drawLine(x0+y,y0-x,x0+y,y0-x);
  79.   g.drawLine(x0+x,y0-y,x0+x,y0-y);
  80.   g.drawLine(x0-x,y0-y,x0-x,y0-y);
  81.   g.drawLine(x0-y,y0-x,x0-y,y0-x);
  82.   g.drawLine(x0-y,y0+x,x0-y,y0+x);
  83.   g.drawLine(x0-x,y0+y,x0-x,y0+y);
  84. }
  85.  
  86.   // Circle is just Bresenham's algorithm for a scan converted circle
  87.  
  88. public void circle(int x0, int y0, int r, Graphics g) {
  89.   int x,y;
  90.   float d;
  91.  
  92.   x=0;
  93.   y=r;
  94.   d=5/4-r;
  95.   plotpoints(x0,y0,x,y,g);
  96.  
  97.   while (y>x){
  98.     if (d<0) {
  99.       d=d+2*x+3;
  100.       x++;
  101.     }
  102.     else {
  103.       d=d+2*(x-y)+5;
  104.       x++;
  105.       y--;
  106.     }
  107.     plotpoints(x0,y0,x,y,g);
  108.   }
  109. }
  110.  
  111.  
  112.   // Paint is the main part of the program
  113.  
  114. public void paint(Graphics g) {
  115.   int xh, yh, xm, ym, xs, ys, s, m, h, xcenter, ycenter;
  116.   String today;
  117.  
  118.   dat = new Date();
  119.   cal.setTime(dat);
  120. //  cal.computeFields();
  121.   s = (int)cal.get(Calendar.SECOND);
  122.   m = (int)cal.get(Calendar.MINUTE);
  123.   h = (int)cal.get(Calendar.HOUR_OF_DAY);
  124.   today = df.format(dat);
  125.   xcenter=80;
  126.   ycenter=55;
  127.  
  128.   // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00)
  129.   // x = r(cos a) + xcenter, y = r(sin a) + ycenter
  130.   
  131.   xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
  132.   ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
  133.   xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
  134.   ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
  135.   xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + xcenter);
  136.   yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + ycenter);
  137.   
  138.   // Draw the circle and numbers
  139.   
  140.   g.setFont(F);
  141.   g.setColor(fgcol);
  142.   circle(xcenter,ycenter,50,g);
  143.   g.setColor(fgcol2);
  144.   g.drawString("9",xcenter-45,ycenter+3); 
  145.   g.drawString("3",xcenter+40,ycenter+3);
  146.   g.drawString("12",xcenter-5,ycenter-37);
  147.   g.drawString("6",xcenter-3,ycenter+45);
  148.  
  149.   // Erase if necessary, and redraw
  150.   
  151.   g.setColor(getBackground());
  152.   if (xs != lastxs || ys != lastys) {
  153.     g.drawLine(xcenter, ycenter, lastxs, lastys);
  154.     g.drawString(lastdate, 5, 125);
  155.   }
  156.   if (xm != lastxm || ym != lastym) {
  157.     g.drawLine(xcenter, ycenter-1, lastxm, lastym);
  158.     g.drawLine(xcenter-1, ycenter, lastxm, lastym); }
  159.   if (xh != lastxh || yh != lastyh) {
  160.     g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
  161.     g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }
  162.   g.setColor(fgcol2);
  163.   g.drawString(today, 5, 125);  
  164.   g.drawLine(xcenter, ycenter, xs, ys);
  165.   g.setColor(fgcol);
  166.   g.drawLine(xcenter, ycenter-1, xm, ym);
  167.   g.drawLine(xcenter-1, ycenter, xm, ym);
  168.   g.drawLine(xcenter, ycenter-1, xh, yh);
  169.   g.drawLine(xcenter-1, ycenter, xh, yh);
  170.   lastxs=xs; lastys=ys;
  171.   lastxm=xm; lastym=ym;
  172.   lastxh=xh; lastyh=yh;
  173.   lastdate = today;
  174.   dat=null;
  175. }
  176.  
  177. public void start() {
  178.   if(timer == null)
  179.     {
  180.       timer = new Thread(this);
  181.       timer.start();
  182.     }
  183. }
  184.  
  185. public void stop() {
  186.   timer = null;
  187. }
  188.  
  189. public void run() {
  190.   while (timer != null) {
  191.     try {Thread.sleep(100);} catch (InterruptedException e){}
  192.     repaint();
  193.   }
  194.   timer = null;
  195. }
  196.  
  197. public void update(Graphics g) {
  198.   paint(g);
  199. }
  200. }
  201.  
  202.