home *** CD-ROM | disk | FTP | other *** search
- import browser.*;
- import browser.audio.*;
- import awt.*;
- import net.www.html.*;
- import java.io.*;
- import java.util.*;
- import java.lang.*;
-
- // analogue clock without no second hand
-
- public
- class Clock extends Applet implements Runnable {
-
- static String clkfile = "images/tokei.gif";
- Graphics offscreen;
- Image clock;
- Image im;
-
- int width, height;
- int cx, cy;
-
- Thread kicker = null;
- int lasttime[] = null;
-
-
- public void init() {
- clock = getImage(new URL(documentURL,clkfile));
- width = clock.width;
- height = clock.height;
- cx = width / 2;
- cy = width / 2;
-
- resize(width,height);
- start();
- }
-
- public void start() {
- if (kicker == null) {
- kicker = new Thread(this);
- kicker.setName("clock");
- kicker.setPriority(Thread.MIN_PRIORITY);
- kicker.start();
- }
- }
-
- public void stop() {
- kicker = null;
- }
-
- public void run() {
- Thread curThread = Thread.currentThread();
- if (curThread.getName().compareTo("clock") == 0) {
- while (kicker != null) {
- int nowtime[] = (new GMTTime()).getTime(9);
- if (lasttime != null &&
- nowtime[0] == lasttime[0] && nowtime[1] == lasttime[1]) {
-
- Thread.sleep(1000*(60-nowtime[2]));
-
- // adjust sec.
- nowtime[2] = 0;
- if (++nowtime[1] == 60) {
- nowtime[1] = 0;
- if (++nowtime[0] == 24) {
- nowtime[0] = 0;
- }
- }
- }
- lasttime = nowtime;
- repaint();
- }
- }
- }
-
- public void paint(Graphics g) {
- update(g);
- }
-
- public void update(Graphics g) {
- if (lasttime == null) {
- return;
- }
-
- if (im == null) {
- im = createImage(width, height);
- offscreen = new Graphics(im);
- }
- offscreen.setForeground(Color.lightGray);
- offscreen.fillRect(0,0,width,height);
- offscreen.drawImage(clock,0,0);
- offscreen.setForeground(Color.black);
-
- double pi = 3.1415926535;
- int hour = lasttime[0];
- if (hour > 12) hour -= 12;
- int minutes = lasttime[1];
-
- // Hour hand & tip
- double l = width / 2;
- double h = (pi/2.0 - (hour/6.0+minutes/360.0)*pi);
- double x = cx + l * 0.4 * Math.cos(h);
- double y = cy - l * 0.4 * Math.sin(h);
-
- offscreen.drawLine(cx,cy,(int)x,(int)y);
- offscreen.drawLine((int)x-1,(int)y-1,(int)x-1,(int)y+1);
- offscreen.drawLine((int)x-1,(int)y+1,(int)x+1,(int)y+1);
- offscreen.drawLine((int)x+1,(int)y+1,(int)x+1,(int)y-1);
- offscreen.drawLine((int)x+1,(int)y-1,(int)x-1,(int)y-1);
-
- // Minutes hand
- double m = (pi/2.0 - minutes*pi/30.0);
- x = cx + l * 0.7 * Math.cos(m);
- y = cy - l * 0.7 * Math.sin(m);
-
- offscreen.drawLine(cx,cy,(int)x,(int)y);
-
- g.drawImage(im,0,0);
- }
-
- }
-