home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / applets / Clock / Clock.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  8.5 KB  |  228 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)Clock.java    1.11 02/06/13
  38.  */
  39.  
  40. import java.util.*;
  41. import java.awt.*;
  42. import java.applet.*;
  43. import java.text.*;
  44.  
  45. /**
  46.  * Time!
  47.  *
  48.  * @author Rachel Gollub
  49.  * @modified Daniel Peek replaced circle drawing calculation, few more changes
  50.  */
  51. public class Clock extends Applet implements Runnable {
  52.     private volatile Thread timer;       // The thread that displays clock
  53.     private int lastxs, lastys, lastxm,
  54.                 lastym, lastxh, lastyh;  // Dimensions used to draw hands 
  55.     private SimpleDateFormat formatter;  // Formats the date displayed
  56.     private String lastdate;             // String to hold date displayed
  57.     private Font clockFaceFont;          // Font for number display on clock
  58.     private Date currentDate;            // Used to get date to display
  59.     private Color handColor;             // Color of main hands and dial
  60.     private Color numberColor;           // Color of second hand and numbers
  61.     private int xcenter = 80, ycenter = 55; // Center position
  62.  
  63.     public void init() {
  64.         int x,y;
  65.         lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
  66.         formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", 
  67.                                           Locale.getDefault());
  68.         currentDate = new Date();
  69.         lastdate = formatter.format(currentDate);
  70.         clockFaceFont = new Font("Serif", Font.PLAIN, 14);
  71.         handColor = Color.blue;
  72.         numberColor = Color.darkGray;
  73.  
  74.         try {
  75.             setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),
  76.                                                      16)));
  77.         } catch (NullPointerException e) {
  78.         } catch (NumberFormatException e) {
  79.         }
  80.         try {
  81.             handColor = new Color(Integer.parseInt(getParameter("fgcolor1"),
  82.                                                    16));
  83.         } catch (NullPointerException e) {
  84.         } catch (NumberFormatException e) {
  85.         }
  86.         try {
  87.             numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"),
  88.                                                      16));
  89.         } catch (NullPointerException e) {
  90.         } catch (NumberFormatException e) {
  91.         }
  92.         resize(300,300);              // Set clock window size
  93.     }
  94.  
  95.     // Paint is the main part of the program
  96.     public void update(Graphics g) {
  97.         int xh, yh, xm, ym, xs, ys;
  98.         int s = 0, m = 10, h = 10;
  99.         String today;
  100.  
  101.         currentDate = new Date();
  102.         
  103.         formatter.applyPattern("s");
  104.         try {
  105.             s = Integer.parseInt(formatter.format(currentDate));
  106.         } catch (NumberFormatException n) {
  107.             s = 0;
  108.         }
  109.         formatter.applyPattern("m");
  110.         try {
  111.             m = Integer.parseInt(formatter.format(currentDate));
  112.         } catch (NumberFormatException n) {
  113.             m = 10;
  114.         }    
  115.         formatter.applyPattern("h");
  116.         try {
  117.             h = Integer.parseInt(formatter.format(currentDate));
  118.         } catch (NumberFormatException n) {
  119.             h = 10;
  120.         }
  121.     
  122.         // Set position of the ends of the hands
  123.         xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter);
  124.         ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter);
  125.         xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter);
  126.         ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter);
  127.         xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
  128.                    + xcenter);
  129.         yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30
  130.                    + ycenter);
  131.     
  132.         // Get the date to print at the bottom
  133.         formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
  134.         today = formatter.format(currentDate);
  135.  
  136.         g.setFont(clockFaceFont);
  137.         // Erase if necessary
  138.         g.setColor(getBackground());
  139.         if (xs != lastxs || ys != lastys) {
  140.             g.drawLine(xcenter, ycenter, lastxs, lastys);
  141.             g.drawString(lastdate, 5, 125);
  142.         }
  143.         if (xm != lastxm || ym != lastym) {
  144.             g.drawLine(xcenter, ycenter-1, lastxm, lastym);
  145.             g.drawLine(xcenter-1, ycenter, lastxm, lastym); 
  146.         }
  147.         if (xh != lastxh || yh != lastyh) {
  148.             g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
  149.             g.drawLine(xcenter-1, ycenter, lastxh, lastyh); 
  150.         }
  151.  
  152.         // Draw date and hands
  153.         g.setColor(numberColor);
  154.         g.drawString(today, 5, 125);    
  155.         g.drawLine(xcenter, ycenter, xs, ys);
  156.         g.setColor(handColor);
  157.         g.drawLine(xcenter, ycenter-1, xm, ym);
  158.         g.drawLine(xcenter-1, ycenter, xm, ym);
  159.         g.drawLine(xcenter, ycenter-1, xh, yh);
  160.         g.drawLine(xcenter-1, ycenter, xh, yh);
  161.         lastxs = xs; lastys = ys;
  162.         lastxm = xm; lastym = ym;
  163.         lastxh = xh; lastyh = yh;
  164.         lastdate = today;
  165.         currentDate = null;
  166.     }
  167.  
  168.     public void paint(Graphics g) {
  169.         g.setFont(clockFaceFont);
  170.         // Draw the circle and numbers
  171.         g.setColor(handColor);
  172.         g.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360);
  173.         g.setColor(numberColor);
  174.         g.drawString("9", xcenter-45, ycenter+3); 
  175.         g.drawString("3", xcenter+40, ycenter+3);
  176.         g.drawString("12", xcenter-5, ycenter-37);
  177.         g.drawString("6", xcenter-3, ycenter+45);
  178.  
  179.         // Draw date and hands
  180.         g.setColor(numberColor);
  181.         g.drawString(lastdate, 5, 125);    
  182.         g.drawLine(xcenter, ycenter, lastxs, lastys);
  183.         g.setColor(handColor);
  184.         g.drawLine(xcenter, ycenter-1, lastxm, lastym);
  185.         g.drawLine(xcenter-1, ycenter, lastxm, lastym);
  186.         g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
  187.         g.drawLine(xcenter-1, ycenter, lastxh, lastyh); 
  188.     }
  189.  
  190.     public void start() {
  191.         timer = new Thread(this);
  192.         timer.start();
  193.     }
  194.  
  195.     public void stop() {
  196.         timer = null;
  197.     }
  198.  
  199.     public void run() {
  200.         Thread me = Thread.currentThread();
  201.         while (timer == me) {
  202.             try {
  203.                 Thread.currentThread().sleep(100);
  204.             } catch (InterruptedException e) {
  205.             }
  206.             repaint();
  207.         }
  208.     }
  209.  
  210.     public String getAppletInfo() {
  211.         return "Title: A Clock \n"
  212.             + "Author: Rachel Gollub, 1995 \n"
  213.             + "An analog clock.";
  214.     }
  215.   
  216.     public String[][] getParameterInfo() {
  217.         String[][] info = {
  218.             {"bgcolor", "hexadecimal RGB number", 
  219.              "The background color. Default is the color of your browser."},
  220.             {"fgcolor1", "hexadecimal RGB number", 
  221.              "The color of the hands and dial. Default is blue."},
  222.             {"fgcolor2", "hexadecimal RGB number", 
  223.              "The color of the second hand and numbers. Default is dark gray."}
  224.         };
  225.         return info;
  226.     }
  227. }
  228.