home *** CD-ROM | disk | FTP | other *** search
/ Internet 1996 World Exposition / park.org.s3.amazonaws.com.7z / park.org.s3.amazonaws.com / Japan / NTT / DM- / hotjava / classes / src / Clock.jpp < prev    next >
Encoding:
Text File  |  2017-09-21  |  2.5 KB  |  121 lines

  1. import browser.*;
  2. import browser.audio.*;
  3. import awt.*;
  4. import net.www.html.*;
  5. import java.io.*;
  6. import java.util.*;
  7. import java.lang.*;
  8.  
  9. // analogue clock without no second hand
  10.  
  11. public
  12. class Clock extends Applet implements Runnable {
  13.  
  14. static String    clkfile = "images/tokei.gif";
  15. Graphics    offscreen;
  16. Image        clock;
  17. Image        im;
  18.  
  19. int        width, height;
  20. int        cx, cy;
  21.  
  22. Thread        kicker = null;
  23. int        lasttime[] = null;
  24.  
  25.  
  26. public void init() {
  27.     clock = getImage(new URL(documentURL,clkfile));
  28.     width  = clock.width;
  29.     height = clock.height;
  30.     cx = width / 2;
  31.     cy = width / 2;
  32.  
  33.     resize(width,height);
  34.     start();
  35. }
  36.  
  37. public void start() {
  38.     if (kicker == null) {
  39.     kicker = new Thread(this);
  40.     kicker.setName("clock");
  41.     kicker.setPriority(Thread.MIN_PRIORITY);
  42.     kicker.start();
  43.     }
  44. }
  45.  
  46. public void stop() {
  47.     kicker = null;
  48. }
  49.  
  50. public void run() {
  51.     Thread curThread = Thread.currentThread();
  52.     if (curThread.getName().compareTo("clock") == 0) {    
  53.     while (kicker != null) {
  54.         int nowtime[] = (new GMTTime()).getTime(9);
  55.         if (lasttime != null && 
  56.             nowtime[0] == lasttime[0] && nowtime[1] == lasttime[1]) {
  57.  
  58.         Thread.sleep(1000*(60-nowtime[2]));
  59.  
  60.         // adjust sec.
  61.         nowtime[2] = 0;
  62.         if (++nowtime[1] == 60) {
  63.             nowtime[1] = 0;
  64.             if (++nowtime[0] == 24) {
  65.             nowtime[0] = 0;
  66.             }
  67.         }
  68.         }
  69.         lasttime = nowtime;
  70.         repaint();
  71.     }
  72.     }
  73. }
  74.  
  75. public void paint(Graphics g) {
  76.     update(g);
  77. }
  78.  
  79. public void update(Graphics g) {
  80.     if (lasttime == null) {
  81.     return;
  82.     }
  83.  
  84.     if (im == null) {
  85.     im = createImage(width, height);
  86.     offscreen = new Graphics(im);
  87.     }
  88.     offscreen.setForeground(Color.lightGray);
  89.     offscreen.fillRect(0,0,width,height);
  90.     offscreen.drawImage(clock,0,0);
  91.     offscreen.setForeground(Color.black);
  92.  
  93.     double pi = 3.1415926535;
  94.     int hour = lasttime[0];
  95.     if (hour > 12) hour -= 12;
  96.     int minutes = lasttime[1];
  97.  
  98.     // Hour hand & tip
  99.     double l = width / 2;
  100.     double h = (pi/2.0 - (hour/6.0+minutes/360.0)*pi);
  101.     double x = cx + l * 0.4 * Math.cos(h);
  102.     double y = cy - l * 0.4 * Math.sin(h);
  103.  
  104.     offscreen.drawLine(cx,cy,(int)x,(int)y);
  105.     offscreen.drawLine((int)x-1,(int)y-1,(int)x-1,(int)y+1);
  106.     offscreen.drawLine((int)x-1,(int)y+1,(int)x+1,(int)y+1);
  107.     offscreen.drawLine((int)x+1,(int)y+1,(int)x+1,(int)y-1);
  108.     offscreen.drawLine((int)x+1,(int)y-1,(int)x-1,(int)y-1);
  109.  
  110.     // Minutes hand
  111.     double m = (pi/2.0 - minutes*pi/30.0);
  112.     x = cx + l * 0.7 * Math.cos(m);
  113.     y = cy - l * 0.7 * Math.sin(m);
  114.  
  115.     offscreen.drawLine(cx,cy,(int)x,(int)y);
  116.  
  117.     g.drawImage(im,0,0);
  118. }
  119.  
  120. }
  121.