/****************************/ /* Digital Clock V1.0 1997 */ /* for Java API 1.0.2 */ /* Ju Li */ /****************************/ import java.applet.*; import java.awt.*; import java.net.*; import java.util.*; import LED; public class Clock extends Canvas implements Runnable { final int delay = 1000/5; final double colon_fraction = 0.5; int minutes, seconds, height, width; int last_minutes, last_seconds, now_minutes, now_seconds; int digit_width, digit_height; long program_start; public Thread clockthread; Countdown_action myaction; public Clock (int Clock_start_minutes, int Clock_start_seconds, int Clock_width, int Clock_height, Countdown_action action) { super(); minutes = Clock_start_minutes; seconds = Clock_start_seconds; now_minutes = minutes; now_seconds = seconds; width = Clock_width; height = Clock_height; digit_height = height; digit_width = (int) Math.floor(width/(4+colon_fraction)); myaction = action; program_start = System.currentTimeMillis(); repaint(); clockthread = new Thread(this); } public synchronized void run() { try { for(;;) { Thread.sleep (delay); calculate_time(); if ( (now_minutes != last_minutes) || (now_seconds != last_seconds) ) { repaint(); last_minutes = now_minutes; last_seconds = now_seconds; } } } catch(InterruptedException e) { } } public void calculate_time() { long seconds_spent, minutes_spent; seconds_spent = (System.currentTimeMillis()-program_start)/1000; minutes_spent = seconds_spent/60; seconds_spent -= minutes_spent*60; now_minutes = minutes - (int) minutes_spent; now_seconds = seconds - (int) seconds_spent; while (now_seconds<0) { now_seconds += 60; now_minutes --; } if (now_minutes < 0) { now_minutes = 0; now_seconds = 0; myaction.countdown_action(); } } public void update(Graphics g) { // update(), if not overridden, will // 1) clear g 2) set colors 3) call paint(). // The "clear g" part is the reason screen flashes. // We can avoid the problem using double buffering. paint(g); } public void paint(Graphics g) { g.setColor (Color.black); g.fillRect (0,0,width,height); g.setColor (Color.green); LED.Draw(g,now_minutes/10,0,0,digit_width,digit_height); LED.Draw(g,now_minutes%10, digit_width,0,digit_width,digit_height); drawFilledCircle (g, (int) Math.floor(digit_width*(2+colon_fraction/2)), digit_height/3, (int) Math.floor(digit_width*colon_fraction/6)); drawFilledCircle (g, (int) Math.floor(digit_width*(2+colon_fraction/2)), digit_height*2/3, (int) Math.floor(digit_width*colon_fraction/6)); LED.Draw(g,now_seconds/10, (int) Math.floor(digit_width*(2+colon_fraction)), 0,digit_width,digit_height); LED.Draw(g,now_seconds%10, (int) Math.floor(digit_width*(3+colon_fraction)), 0,digit_width,digit_height); } public void drawFilledCircle (Graphics g, int x, int y, int radius) { int a,b; for (a=0,b=1; a