home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / WDESAMPL.BIN / Clock2.java < prev    next >
Text File  |  1997-02-21  |  6KB  |  201 lines

  1. /*
  2.  * @(#)Clock2.java    1.8 97/01/24
  3.  *
  4.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. // author: Rachel Gollub, 1995
  32. // modified 96/04/24 Jim Hagen : use getBackground()
  33. // modified 96/05/29 Rachel Gollub : add garbage collecting
  34. // modified 96/10/22 Rachel Gollub : add bgcolor, fgcolor1, fgcolor2 params
  35. // modified 96/12/05 Rachel Gollub : change copyright and Date methods
  36. // Time!
  37.  
  38. import java.util.*;
  39. import java.awt.*;
  40. import java.applet.*;
  41. import java.text.*;
  42.  
  43. public class Clock2 extends Applet implements Runnable {
  44.   Thread timer = null;
  45.   int lastxs=0, lastys=0, lastxm=0, lastym=0, lastxh=0, lastyh=0;
  46.   Date dummy = new Date();
  47.   GregorianCalendar cal = new GregorianCalendar();
  48.   SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
  49.   String lastdate = df.format(dummy);
  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, m, h, xcenter, ycenter;
  115.   String today;
  116.  
  117.   dat = new Date();
  118.   cal.setTime(dat);
  119.   //  cal.computeFields(); Not needed anymore
  120.   s = (int)cal.get(Calendar.SECOND);
  121.   m = (int)cal.get(Calendar.MINUTE);
  122.   h = (int)cal.get(Calendar.HOUR_OF_DAY);
  123.   today = df.format(dat);
  124.   xcenter=80;
  125.   ycenter=55;
  126.   
  127.   // a= s* pi/2 - pi/2 (to switch 0,0 from 3:00 to 12:00)
  128.   // x = r(cos a) + xcenter, y = r(sin a) + ycenter
  129.   
  130.   xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
  131.   ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
  132.   xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
  133.   ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
  134.   xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + xcenter);
  135.   yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 + ycenter);
  136.   
  137.   // Draw the circle and numbers
  138.   
  139.   g.setFont(F);
  140.   g.setColor(fgcol);
  141.   circle(xcenter,ycenter,50,g);
  142.   g.setColor(fgcol2);
  143.   g.drawString("9",xcenter-45,ycenter+3); 
  144.   g.drawString("3",xcenter+40,ycenter+3);
  145.   g.drawString("12",xcenter-5,ycenter-37);
  146.   g.drawString("6",xcenter-3,ycenter+45);
  147.  
  148.   // Erase if necessary, and redraw
  149.   
  150.   g.setColor(getBackground());
  151.   if (xs != lastxs || ys != lastys) {
  152.     g.drawLine(xcenter, ycenter, lastxs, lastys);
  153.     g.drawString(lastdate, 5, 125);
  154.   }
  155.   if (xm != lastxm || ym != lastym) {
  156.     g.drawLine(xcenter, ycenter-1, lastxm, lastym);
  157.     g.drawLine(xcenter-1, ycenter, lastxm, lastym); }
  158.   if (xh != lastxh || yh != lastyh) {
  159.     g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
  160.     g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }
  161.   g.setColor(fgcol2);
  162.   g.drawString(today, 5, 125);  
  163.   g.drawLine(xcenter, ycenter, xs, ys);
  164.   g.setColor(fgcol);
  165.   g.drawLine(xcenter, ycenter-1, xm, ym);
  166.   g.drawLine(xcenter-1, ycenter, xm, ym);
  167.   g.drawLine(xcenter, ycenter-1, xh, yh);
  168.   g.drawLine(xcenter-1, ycenter, xh, yh);
  169.   lastxs=xs; lastys=ys;
  170.   lastxm=xm; lastym=ym;
  171.   lastxh=xh; lastyh=yh;
  172.   lastdate = today;
  173.   dat=null;
  174. }
  175.  
  176. public void start() {
  177.   if(timer == null)
  178.     {
  179.       timer = new Thread(this);
  180.       timer.start();
  181.     }
  182. }
  183.  
  184. public void stop() {
  185.   timer = null;
  186. }
  187.  
  188. public void run() {
  189.   while (timer != null) {
  190.     try {Thread.sleep(100);} catch (InterruptedException e){}
  191.     repaint();
  192.   }
  193.   timer = null;
  194. }
  195.  
  196. public void update(Graphics g) {
  197.   paint(g);
  198. }
  199. }
  200.  
  201.