home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch04 / Clock.java next >
Encoding:
Java Source  |  1998-12-14  |  4.4 KB  |  176 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3. import java.util.*;
  4. import java.text.*;
  5.  
  6. /*
  7.  * class for applet/application
  8.  */
  9. public class Clock extends Applet implements Runnable {
  10.  
  11. /*
  12.  * the instance of Thread for checking the time periodically
  13.  */
  14. Thread thread = null;
  15. SimpleDateFormat formatter;
  16. Date currentDate;
  17.  
  18. /*
  19.  * saved values used to draw only when things have changed
  20.  */
  21. int lastxs=0;
  22. int lastys=0;
  23. int lastxm=0;
  24. int lastym=0;
  25. int lastxh=0;
  26. int lastyh=0;
  27.  
  28. /**
  29.   * sets the background color to light gray
  30.   */
  31. public void init(){
  32.         this.setBackground(Color.lightGray);
  33. }
  34.  
  35. /**
  36.  * draws the clock face
  37.  * @param g - destination graphics object
  38.  */
  39. public void paint (Graphics g) {
  40.  
  41.        int xh, yh, xm, ym, xs, ys, s=0,m=10, h=10, xcenter, ycenter;
  42. String Today;
  43.     currentDate = new Date();
  44.     SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault());
  45. try{
  46.         s=Integer.parseInt(formatter.format(currentDate));
  47.     }catch(NumberFormatException n){
  48.         s=0;
  49.     }
  50.  
  51.     formatter.applyPattern("m");
  52.     try{
  53.         m=Integer.parseInt(formatter.format(currentDate));
  54.     }catch(NumberFormatException n){
  55.         m=10; 
  56.     }
  57.  
  58.     formatter.applyPattern("h");
  59.     try{
  60.         h=Integer.parseInt(formatter.format(currentDate));
  61.     }catch(NumberFormatException n){
  62.         h=10;
  63.     }
  64.  
  65.        xcenter=100;
  66.        ycenter=100;
  67.  
  68.        xs = (int)(Math.cos(s * 3.14f/30 - 3.14f/2) * 45 + xcenter);
  69. ys = (int)(Math.sin(s * 3.14f/30 - 3.14f/2) * 45 + ycenter);
  70. xm = (int)(Math.cos(m * 3.14f/30 - 3.14f/2) * 40 + xcenter);
  71. ym = (int)(Math.sin(m * 3.14f/30 - 3.14f/2) * 40 + ycenter);
  72. xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30
  73. + xcenter);
  74.        yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30
  75. + ycenter);
  76.  
  77. // Draw the circle and numbers
  78.        g.setFont(new Font("TimesRoman", Font.PLAIN, 14));
  79.        g.setColor(Color.blue);
  80.        g.drawOval (xcenter-50, ycenter-50, 100, 100);
  81.        g.setColor(Color.darkGray);
  82.        g.drawString("9",xcenter-45,ycenter+3);
  83.        g.drawString("3",xcenter+40,ycenter+3);
  84.        g.drawString("12",xcenter-5,ycenter-37);
  85.        g.drawString("6",xcenter-3,ycenter+45);
  86.  
  87. // Erase if necessary, and redraw
  88.        g.setColor(Color.lightGray);
  89.        if (xs != lastxs || ys != lastys) {
  90.               g.drawLine(xcenter, ycenter, lastxs, lastys);
  91.        }
  92.        if (xm != lastxm || ym != lastym) {
  93.               g.drawLine(xcenter, ycenter-1, lastxm, lastym);
  94.               g.drawLine(xcenter-1, ycenter, lastxm, lastym);
  95.        }
  96.        if (xh != lastxh || yh != lastyh) {
  97.               g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
  98.               g.drawLine(xcenter-1, ycenter, lastxh, lastyh);
  99.        }
  100.  
  101.        g.setColor(Color.darkGray);
  102.        g.drawLine(xcenter, ycenter, xs, ys);
  103.        g.setColor(Color.red);
  104.        g.drawLine(xcenter, ycenter-1, xm, ym);
  105.        g.drawLine(xcenter-1, ycenter, xm, ym);
  106.        g.drawLine(xcenter, ycenter-1, xh, yh);
  107.        g.drawLine(xcenter-1, ycenter, xh, yh);
  108.        lastxs=xs; lastys=ys;
  109.        lastxm=xm; lastym=ym;
  110.        lastxh=xh; lastyh=yh;
  111. }
  112.  
  113. /*
  114.  * called when the applet is started
  115.  * create a new instance of Thread and start it
  116.  */
  117. public void start() {
  118.  
  119.        if(thread == null) {
  120.               thread = new Thread(this);
  121.               thread.start();
  122.        }
  123. }
  124.  
  125. /*
  126.  * called when the applet is stopped
  127.  * stops the thread
  128.  */
  129. public void stop() {
  130.  
  131.        thread = null;
  132. }
  133.  
  134. /*
  135.  * the thread itself
  136.  * sleeps for 100ms and forces a repaint
  137.  */
  138. public void run() {
  139.  
  140.        while (thread != null) {
  141.               try {
  142.                      Thread.sleep(100);
  143.               } catch (InterruptedException e) { }
  144.               repaint();
  145.        }
  146.        thread = null;
  147. }
  148.  
  149. /**
  150.  * override the default update method to avoid flickering
  151.  * caused by unnecessary erasing of the applet panel
  152.  * @param g - destination graphics object
  153.  */
  154. public void update(Graphics g) {
  155.        paint(g);
  156. }
  157.  
  158. /**
  159.  * application entry point
  160.  * not used when run as an applet
  161.  * create a new window frame and add the applet inside
  162.  * @param args[] - command-line arguments
  163.  */
  164. public static void main (String args[]) {
  165.  
  166.        Frame f = new Frame ("Clock");
  167.        Clock clock = new Clock ();
  168.  
  169.        f.setSize (210, 230);
  170.        f.add ("Center", clock);
  171.        f.show ();
  172.        clock.init ();
  173.        clock.start ();
  174. }
  175. }
  176.