// --- NeXTClock, written by Rene K. Mueller <kiwi@the-labs.com>
//     V0.20: Jul 20, 1996

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.util.Date;

public class NeXTClock extends java.applet.Applet 
        implements Runnable {

   Thread clockThread;  // let's do it with threads
   Font dateFont, dayFont, timeFont, monthFont;
   Image baseImg; int size = 5; int italic = 1;
   Date now; Color darkGreen; int blink = 0; int last_min = -1;
   String weekday[] = { "SUN", "MON","TUE","WED","THU","FRI","SAT" };
   String month[] = { "JAN","FEB","MAR","APR","MAY","JUN","JUL",
      "AUG","SEP","OCT","NOV","DEC" };
   public void init() {
      baseImg = getImage(getCodeBase(),"default.gif");
      timeFont = new Font("TimesRoman",Font.BOLD,16);
      dayFont = new Font("Helvetica",Font.PLAIN,6);
      dateFont = new Font("TimesRoman",Font.BOLD,18);
      monthFont = new Font("Helvetica",Font.ITALIC,8);
      darkGreen = new Color(0,200,0);
   }
   int doItalic(int x, int y) {
      if(italic==1) return (int)(x-y*.2);
      return x;
   }
   // one segment 0-6
   void drawSegment(Graphics g, int x, int y, int seg) {
      int coord[] = {
         0,0,1,0, // 0
         1,0,1,1, // 1
         1,1,1,2, // 2
         1,2,0,2, // 3
         0,2,0,1, // 4
         0,1,0,0, // 5
         0,1,1,1 }; // 6
      g.drawLine(
         doItalic(x+coord[seg*4+0]*size,coord[seg*4+1]*size),
         y+coord[seg*4+1]*size,
         doItalic(x+coord[seg*4+2]*size,coord[seg*4+3]*size),
         y+coord[seg*4+3]*size);
   }
   // one digit
   void drawDigit(Graphics g, int digit, int x, int y) {
      int digits[] = {
         1+2+4+8+16+32+00,  // 0 
         0+2+4+0+00+00+00,  // 1
         1+2+0+8+16+00+64,  // 2 
         1+2+4+8+00+00+64,  // 3 
         0+2+4+0+00+32+64,  // 4 
         1+0+4+8+00+32+64,  // 5 
         1+0+4+8+16+32+64,  // 6 
         1+2+4+0+00+00+00,  // 7 
         1+2+4+8+16+32+64,  // 8 
         1+2+4+8+00+32+64,  // 9 
      };
      for(int i=0; i<7; i++) {
         if((digits[digit]&(1<<i))!=0)
            drawSegment(g,x,y,i);
      }
   }
   void drawDigitString(Graphics g, String s, int x, int y) {
      for(int i=0; i<s.length(); i++) {
         int digit = s.charAt(i);
         if(digit>='0'&&digit<='9')
            drawDigit(g,digit-'0',x+=(int)(size*1.8),y);
         else
            x += (int)(size*.8);
      }
   }
   void drawBlink(Graphics g) {
      if(blink==0) { 
         g.setColor(darkGreen);
         blink = 1;
      } else {
         g.setColor(Color.black);
         blink = 0;
      }
      g.drawLine(30,9,30,10);
      g.drawLine(29,12,29,13);
   }
   void drawTime(Graphics g) {
      FontMetrics fm = getFontMetrics(timeFont);
      String s = 
      ""+(int)(now.getHours()/10)+(now.getHours()%10)+":"+
      ""+(int)(now.getMinutes()/10)+(now.getMinutes()%10);
      g.setFont(timeFont);
      g.setColor(darkGreen);
//      g.drawString(s,(size().width-fm.stringWidth(s)-2)/2,18);
      drawDigitString(g,s,4,6);
   }
   void drawDate(Graphics g) {
      FontMetrics fm; String s;
      g.setColor(Color.black);
      fm = getFontMetrics(dayFont);  s = weekday[now.getDay()]; g.setFont(dayFont);
      g.drawString(s,(size().width-fm.stringWidth(s))/2-2,31);
      fm = getFontMetrics(dateFont); s = ""+now.getDate();  g.setFont(dateFont);
      g.drawString(s,(size().width-fm.stringWidth(s))/2-2,46);
      fm = getFontMetrics(monthFont);  s = month[now.getMonth()]; g.setFont(monthFont);
      g.drawString(s,(size().width-fm.stringWidth(s))/2-2,54);
   }
   public void update(Graphics g) {
      paint(g);
   }
   public void paint(Graphics g) {
      g.drawImage(baseImg,0,0,this);
      drawDate(g);
      drawTime(g);
   }
   // code was taken from other 'clock' implementations
   public void start() {
      if (clockThread == null) {
         clockThread = new Thread(this, "Clock");
         clockThread.start();
      }
   }
   public void run() {
      while (clockThread != null) {
         now = new Date(); 
         if(last_min!=now.getMinutes()) {
            last_min = now.getMinutes();
            this.getGraphics().drawImage(baseImg,0,0,this);
            drawDate(this.getGraphics());
            drawTime(this.getGraphics());
         } 
         drawBlink(this.getGraphics());
         this.getToolkit().sync();
         pause(1000);
      }
   }
   public void pause(int time) {
      try { clockThread.sleep(time);
      } catch (InterruptedException e) {}
   }
   public void stop() {
      clockThread.stop();
      clockThread = null;
   }
}

