
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;
import java.awt.event.*;

public class Klok extends Applet implements Runnable 
{
  Thread timer;                // The thread that displays clock
  int lastxs, lastys, lastxm,
  lastym, lastxh, lastyh;  // Dimensions used to draw hands 
  Date currentDate;            // Used to get date to display
   
  int lasthours;
  int lastminutes;
  int lastseconds;

  Seg7Display shourstens    = new Seg7Display(0,1);
  Seg7Display shoursunits   = new Seg7Display(0,2);
  Seg7Display sminutestens  = new Seg7Display(0,3);
  Seg7Display sminutesunits = new Seg7Display(0,4);
  Seg7Display secondstens   = new Seg7Display(0,5);
  Seg7Display secondsunits  = new Seg7Display(0,6);

  Colondots colon = new Colondots();
  Colondots colon2 = new Colondots();

  // override update(), to not erase the background before painting
  public void update(Graphics g) 
  {
    paint(g);
  }
 
  public void init() 
  {
    setLayout(null);   // look into this option
    int x,y;
    lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
    currentDate = new Date();

    lasthours   = currentDate.getHours();
    lastminutes = currentDate.getMinutes();
    lastseconds = currentDate.getSeconds();
    setBackground(new Color(0,0,100));
    shourstens.setBounds(1,1,27,45);
    shoursunits.setBounds(28,1,27,45);
    sminutestens.setBounds(61,1,27,45);
    sminutesunits.setBounds(88,1,27,45);
    secondstens.setBounds(121,1,27,45);
    secondsunits.setBounds(148,1,27,45);
    colon.setBounds(55,1,6,45);
    colon2.setBounds(115,1,6,45);

    add(shourstens);
    add(shoursunits);
    add(colon);
    add(sminutestens);
    add(sminutesunits);
    add(colon2);
    add(secondstens);
    add(secondsunits);
  }

    // Paint is the main part of the program
    public void paint(Graphics g) 
    {
      currentDate = new Date();
      int hours;
      int minutes;
      int seconds;

      hours   = currentDate.getHours();
      minutes = currentDate.getMinutes();
      seconds = currentDate.getSeconds();

      lasthours   = hours;
      lastminutes = minutes;
      lastseconds = seconds;

      shourstens.NewValue(hours / 10);
      shoursunits.NewValue(hours % 10);
      sminutestens.NewValue(minutes / 10);
      sminutesunits.NewValue(minutes % 10);
      secondstens.NewValue(seconds / 10);
      secondsunits.NewValue(seconds % 10);
      
      currentDate=null;
    }

    public void start() 
    {
      timer = new Thread(this);
      timer.start();
    }

    public void stop() 
    {
      timer = null;
    }

    public void run() 
    {
      Thread me = Thread.currentThread();
      while (timer == me) 
      {
        try 
        {
          Thread.currentThread().sleep(1000);
        } 
        catch (InterruptedException e) { }
        repaint();
      }
    }

    public Dimension getPreferredSize() 
    {
      return new Dimension(178+6-1,47+27-1);
    }
} // end of class Klok

// ** Start of class Colon Dots ***********************************
class Colondots extends Canvas 
{

  Color background;
  Color segmenton;
  Color segmentoff;

  public Dimension getPreferredSize() 
  {  
    return new Dimension(20, 45);
  }

  // contructor
  Colondots() 
  {
    background= new Color(0,88,0);
    setBackground(background);
    segmenton= new Color(0,255,0);
    segmentoff= background.brighter();
  }

  public void paint(Graphics g) 
  {
    g.setColor(segmenton);
    g.fillOval(2,13,3,3);
    g.fillOval(2,30,3,3);
  } // end of paint
  
  // override update() so not to erase the background before painting

  public void update(Graphics g) 
  {
    paint(g);
  }
} // ** end of class Colondots ***********************************

// entered here to avoid multiple classes in directory
class Seg7Display extends Canvas 
{
  Color background;
  Color segmenton;
  Color segmentoff;

  int Occult=0;
  int Digit;

  int X;
  int Y;

  boolean TopOn;
  boolean LeftTopOn;
  boolean RightTopOn;
  boolean MiddleOn;
  boolean LeftBottomOn;
  boolean RightBottomOn;
  boolean BottomOn;

  // override constructor to set values
  Seg7Display(int Status,int Value) 
  {
    background= new Color(0,88,0);
    setBackground(background);
    segmenton= new Color(0,255,0);
    segmentoff= background.brighter();
    X=5;
    Y=5;
    // digit = value between 0..9
    Digit=Value;
    Convert();
  }

  public Dimension getPreferredSize() 
  {
     return new Dimension(28, 45);
  }

  public void paint(Graphics g) 
  {
    if (TopOn) {g.setColor(segmenton);} else {g.setColor(segmentoff);}
    g.drawLine(X+ 1,Y+ 0,X+15,Y+ 0);
    g.drawLine(X+ 2,Y+ 1,X+14,Y+ 1);
    g.drawLine(X+ 3,Y+ 2,X+13,Y+ 2);
  
    if (RightTopOn) {g.setColor(segmenton);} else {g.setColor(segmentoff);}
    g.drawLine(X+16,Y+ 1,X+16,Y+16);
    g.drawLine(X+15,Y+ 3,X+15,Y+15);
    g.drawLine(X+14,Y+ 4,X+14,Y+14);

    if (LeftTopOn) {g.setColor(segmenton);} else {g.setColor(segmentoff);}
    g.drawLine(X+ 0,Y+ 1,X+ 0,Y+16);
    g.drawLine(X+ 1,Y+ 3,X+ 1,Y+15);
    g.drawLine(X+ 2,Y+ 4,X+ 2,Y+14);

    if (RightBottomOn) {g.setColor(segmenton);} else {g.setColor(segmentoff);}
    g.drawLine(X+16,Y+18,X+16,Y+33);
    g.drawLine(X+15,Y+19,X+15,Y+31);
    g.drawLine(X+14,Y+20,X+14,Y+30);

    if (LeftBottomOn) {g.setColor(segmenton);} else {g.setColor(segmentoff);}
    g.drawLine(X+ 0,Y+18,X+ 0,Y+33);
    g.drawLine(X+ 1,Y+19,X+ 1,Y+31);
    g.drawLine(X+ 2,Y+20,X+ 2,Y+30);

    if (BottomOn) {g.setColor(segmenton);} else {g.setColor(segmentoff);}
    g.drawLine(X+ 1,Y+34,X+15,Y+34);
    g.drawLine(X+ 2,Y+33,X+14,Y+33);
    g.drawLine(X+ 3,Y+32,X+13,Y+32);

    if (MiddleOn) {g.setColor(segmenton);} else {g.setColor(segmentoff);}
    g.drawLine(X+ 3,Y+16,X+13,Y+16);
    g.drawLine(X+ 2,Y+17,X+14,Y+17);
    g.drawLine(X+ 3,Y+18,X+13,Y+18);
  } // end of paint

  void Convert() 
  {
    switch (Digit) 
    {
      case 
        0: { TopOn=true; LeftTopOn=true; RightTopOn=true; MiddleOn=false;LeftBottomOn=true; RightBottomOn=true; BottomOn=true;};
           break;
      case 
        1: { TopOn=false;LeftTopOn=false;RightTopOn=true; MiddleOn=false;LeftBottomOn=false;RightBottomOn=true; BottomOn=false;};
           break;
      case 
        2: { TopOn=true; LeftTopOn=false;RightTopOn=true; MiddleOn=true; LeftBottomOn=true; RightBottomOn=false;BottomOn=true;};
           break;
      case 
        3: { TopOn=true; LeftTopOn=false;RightTopOn=true; MiddleOn=true; LeftBottomOn=false;RightBottomOn=true; BottomOn=true;};
           break;
      case 
        4: { TopOn=false;LeftTopOn=true; RightTopOn=true; MiddleOn=true; LeftBottomOn=false;RightBottomOn=true; BottomOn=false;};
       break;
      case 
        5: { TopOn=true; LeftTopOn=true; RightTopOn=false;MiddleOn=true; LeftBottomOn=false;RightBottomOn=true;BottomOn=true;};
           break;
      case 
        6: { TopOn=true; LeftTopOn=true; RightTopOn=false;MiddleOn=true; LeftBottomOn=true; RightBottomOn=true; BottomOn=true;};
           break;
      case 
        7: { TopOn=true; LeftTopOn=false;RightTopOn=true; MiddleOn=false;LeftBottomOn=false;RightBottomOn=true; BottomOn=false;};
           break;
      case 
        8: { TopOn=true; LeftTopOn=true; RightTopOn=true; MiddleOn=true; LeftBottomOn=true; RightBottomOn=true; BottomOn=true;};
           break;
      case 
        9: { TopOn=true; LeftTopOn=true; RightTopOn=true; MiddleOn=true; LeftBottomOn=false;RightBottomOn=true; BottomOn=true;};
           break;
      };
    } // end of convert

    public void NewValue(int v) 
    {
      Digit=v;
      Convert();
      repaint();
    };

    // override update to not erase the background before painting
    public void update(Graphics g) 
    {
      paint(g);
    }
} // end of Seg7Display;
