import java.awt.*;
import java.applet.*;
import java.util.Date;


public class RTime extends Applet implements Runnable {
  int      lastHour, lastMinute, lastSecond;
  Thread   tr;
  int      speed = 1000;  //  1 sec
  int      lastTenHour, lastTenMinute, lastTenSecond;
  Color    LEDColour;
  Color    BackGround;
  Color    FrameColour;

  
  public void init() {
    String  param;

    lastHour = 0;
    lastMinute=0;
    lastSecond=0;
    lastTenHour=0;
    lastTenMinute=0;
    lastTenSecond=0;

    param = getParameter("backcolor");
    BackGround = (param == null) ? Color.black : GetColourFromString(param);
    param = getParameter("ledcolor");
    LEDColour = (param == null) ? Color.green : GetColourFromString(param);
    param = getParameter("framecolor");
    FrameColour = (param == null) ? Color.green : GetColourFromString(param);
  }

 
  public Color GetColourFromString(String theParam) {

    if (theParam.equalsIgnoreCase("BLACK")) {
      return Color.black;
    } else if (theParam.equalsIgnoreCase("BLUE")) {
      return Color.blue;
    } else if (theParam.equalsIgnoreCase("CYAN")) {
      return Color.cyan;
    } else if (theParam.equalsIgnoreCase("DARKGRAY")) {
      return Color.darkGray;
    } else if (theParam.equalsIgnoreCase("GRAY")) {
      return Color.gray;
    } else if (theParam.equalsIgnoreCase("GREEN")) {
      return Color.green;
    } else if (theParam.equalsIgnoreCase("LIGHTGRAY")) {
      return Color.lightGray;
    } else if (theParam.equalsIgnoreCase("MAGENTA")) {
      return Color.magenta;
    } else if (theParam.equalsIgnoreCase("ORANGE")) {
      return Color.orange;
    } else if (theParam.equalsIgnoreCase("PINK")) {
      return Color.pink;
    } else if (theParam.equalsIgnoreCase("RED")) {
      return Color.red;
    } else if (theParam.equalsIgnoreCase("WHITE")) {
      return Color.white;
    } else if (theParam.equalsIgnoreCase("YELLOW")) {
      return Color.yellow;
    }
 
    return Color.black;   

  }
  public void paint(Graphics g) {
    Date  theDate = new Date();
     g.setColor(FrameColour);
     g.fillRect(0, 0, bounds().width, bounds().height);
     g.setColor(BackGround);
     g.fillRect(2, 2, bounds().width-4, bounds().height-4); 
                 // <TABLE BORDER="2">
     DrawClock(g, theDate);		//piirrä 00:..:.. 
   }

  
  public final synchronized void update (Graphics g) {
    int theHour, theMinute, theSecond;
    int theTenHour, theTenMinute, theTenSecond;
    Date  aDate = new Date();

    theSecond = aDate.getSeconds();
    theMinute = aDate.getMinutes();
    theHour = aDate.getHours();
    theTenHour = theHour / 10;
    theHour = theHour - theTenHour * 10;
    theTenMinute = theMinute / 10;
    theMinute = theMinute - theTenMinute * 10;
    theTenSecond = theSecond / 10;
    theSecond = theSecond - theTenSecond * 10;

    if (theSecond != lastSecond)
      {
      DrawSecond(theSecond);
      lastSecond = theSecond;
      }
    if (theTenSecond != lastTenSecond)
      {
      DrawTenSecond(theTenSecond);
      lastTenSecond = theTenSecond;
      }
    if (theMinute != lastMinute)
      {
      DrawMinute(theMinute);
      lastMinute = theMinute;
      }
    if (theTenMinute != lastTenMinute)
      {
      DrawTenMinute(theTenMinute);
      lastTenMinute = theTenMinute;
      }
    if (theHour != lastHour)
      {
      DrawHour(theHour);
      lastHour = theHour;
      }
    if (theTenHour != lastTenHour)
      {
      DrawTenHour(theTenHour);
      lastTenHour = theTenHour;
      }

    DrawColons();		//kaksoispisteet : 00:00:00

    
  }

 
  public synchronized void DrawClock (Graphics g, Date aDate) {
    int theHour, theMinute, theSecond;
    int theTenHour, theTenMinute, theTenSecond;

    theSecond = aDate.getSeconds();
    theMinute = aDate.getMinutes();
    theHour = aDate.getHours();
    theTenHour = theHour / 10;
    theHour = theHour - theTenHour * 10;
    theTenMinute = theMinute / 10;
    theMinute = theMinute - theTenMinute * 10;
    theTenSecond = theSecond / 10;
    theSecond = theSecond - theTenSecond * 10;

    
    DrawSecond(theSecond);
    DrawTenSecond(theTenSecond);
    DrawMinute(theMinute);
    DrawTenMinute(theTenMinute);
    DrawHour(theHour);
    DrawTenHour(theTenHour);
    DrawColons();

  }

    // piirrä numerot
  public synchronized void DrawSecond(int theValue) {
    DrawSevenSegments(getGraphics(), theValue, 99, 5); 
         
  }

  public synchronized void DrawTenSecond(int theValue) {
    DrawSevenSegments(getGraphics(), theValue, 81, 5);
  }

  public synchronized void DrawMinute(int theValue) {
    DrawSevenSegments(getGraphics(), theValue, 61, 5);
  }

  public synchronized void DrawTenMinute(int theValue) {
    DrawSevenSegments(getGraphics(), theValue, 43, 5);
  }

  public synchronized void DrawHour(int theValue) {
    DrawSevenSegments(getGraphics(), theValue, 23, 5);
  }

  public synchronized void DrawTenHour(int theValue) {
    DrawSevenSegments(getGraphics(), theValue, 5, 5);
  }

 
  public synchronized void DrawColons() { 
    Graphics g = getGraphics();

    g.setColor(LEDColour);
    g.fillRect(39, 13, 2, 2);		//piirrä :
    g.fillRect(39, 22, 2, 2);
    g.fillRect(77, 13, 2, 2);
    g.fillRect(77, 22, 2, 2);

  }

  private synchronized void DrawSevenSegments(Graphics g,
            int theValue, int x, int y) {
 
    
    switch (theValue) {
      case 0:
        DrawSegment0On(g, x, y);
        DrawSegment1On(g, x, y);
        DrawSegment2On(g, x, y);
        DrawSegment3Off(g, x, y);
        DrawSegment4On(g, x, y);
        DrawSegment5On(g, x, y);
        DrawSegment6On(g, x, y);
        break;
      case 1:
        DrawSegment0Off(g, x, y);
        DrawSegment1Off(g, x, y);
        DrawSegment2On(g, x, y);
        DrawSegment3Off(g, x, y);
        DrawSegment4Off(g, x, y);
        DrawSegment5On(g, x, y);
        DrawSegment6Off(g, x, y);
        break;
      case 2:
        DrawSegment0On(g, x, y);
        DrawSegment1Off(g, x, y);
        DrawSegment2On(g, x, y);
        DrawSegment3On(g, x, y);
        DrawSegment4On(g, x, y);
        DrawSegment5Off(g, x, y);
        DrawSegment6On(g, x, y);
        break;
      case 3:
        DrawSegment0On(g, x, y);
        DrawSegment1Off(g, x, y);
        DrawSegment2On(g, x, y);
        DrawSegment3On(g, x, y);
        DrawSegment4Off(g, x, y);
        DrawSegment5On(g, x, y);
        DrawSegment6On(g, x, y);
        break;
      case 4:
        DrawSegment0Off(g, x, y);
        DrawSegment1On(g, x, y);
        DrawSegment2On(g, x, y);
        DrawSegment3On(g, x, y);
        DrawSegment4Off(g, x, y);
        DrawSegment5On(g, x, y);
        DrawSegment6Off(g, x, y);
        break;
      case 5:
        DrawSegment0On(g, x, y);
        DrawSegment1On(g, x, y);
        DrawSegment2Off(g, x, y);
        DrawSegment3On(g, x, y);
        DrawSegment4Off(g, x, y);
        DrawSegment5On(g, x, y);
        DrawSegment6On(g, x, y);
        break;
      case 6:
        DrawSegment0On(g, x, y);
        DrawSegment1On(g, x, y);
        DrawSegment2Off(g, x, y);
        DrawSegment3On(g, x, y);
        DrawSegment4On(g, x, y);
        DrawSegment5On(g, x, y);
        DrawSegment6On(g, x, y);
        break;
      case 7:
        DrawSegment0On(g, x, y);
        DrawSegment1Off(g, x, y);
        DrawSegment2On(g, x, y);
        DrawSegment3Off(g, x, y);
        DrawSegment4Off(g, x, y);
        DrawSegment5On(g, x, y);
        DrawSegment6Off(g, x, y);
        break;
      case 8:
        DrawSegment0On(g, x, y);
        DrawSegment1On(g, x, y);
        DrawSegment2On(g, x, y);
        DrawSegment3On(g, x, y);
        DrawSegment4On(g, x, y);
        DrawSegment5On(g, x, y);
        DrawSegment6On(g, x, y);
        break;
      case 9:
        DrawSegment0On(g, x, y);
        DrawSegment1On(g, x, y);
        DrawSegment2On(g, x, y);
        DrawSegment3On(g, x, y);
        DrawSegment4Off(g, x, y);
        DrawSegment5On(g, x, y);
        DrawSegment6Off(g, x, y);
        break;
    }
      
  }

 
  private synchronized void DrawSegment0On(Graphics g, int x, int y) {
    g.setColor(LEDColour);
    DrawHorizontalLine(g, x+3, y+1, 9);
  }
  private synchronized void DrawSegment0Off(Graphics g, int x, int y) {
    g.setColor(BackGround);
    DrawHorizontalLine(g, x+3, y+1, 9);
  }
  private synchronized void DrawSegment3On(Graphics g, int x, int y) {
    g.setColor(LEDColour); 
    DrawHorizontalLine(g, x+3, y+12, 9); 
  }
  private synchronized void DrawSegment3Off(Graphics g, int x, int y) {
    g.setColor(BackGround);
    DrawHorizontalLine(g, x+3, y+12, 9);
  }
  private synchronized void DrawSegment6On(Graphics g, int x, int y) {
    g.setColor(LEDColour); 
    DrawHorizontalLine(g, x+3, y+23, 9); 
  }
  private synchronized void DrawSegment6Off(Graphics g, int x, int y) {
    g.setColor(BackGround);
    DrawHorizontalLine(g, x+3, y+23, 9);
  }

  private synchronized void DrawSegment1On(Graphics g, int x, int y) { 
    g.setColor(LEDColour);  
    DrawVerticalLine(g, x+1, y+3, 9);  
  }
  private synchronized void DrawSegment1Off(Graphics g, int x, int y) { 
    g.setColor(BackGround);
    DrawVerticalLine(g, x+1, y+3, 9); 
  }
  private synchronized void DrawSegment2On(Graphics g, int x, int y) { 
    g.setColor(LEDColour);  
    DrawVerticalLine(g, x+12, y+3, 9);  
  }
  private synchronized void DrawSegment2Off(Graphics g, int x, int y) { 
    g.setColor(BackGround);
    DrawVerticalLine(g, x+12, y+3, 9); 
  }
  private synchronized void DrawSegment4On(Graphics g, int x, int y) { 
    g.setColor(LEDColour);  
    DrawVerticalLine(g, x+1, y+14, 9);  
  }
  private synchronized void DrawSegment4Off(Graphics g, int x, int y) { 
    g.setColor(BackGround);
    DrawVerticalLine(g, x+1, y+14, 9); 
  } 
  private synchronized void DrawSegment5On(Graphics g, int x, int y) { 
    g.setColor(LEDColour);  
    DrawVerticalLine(g, x+12, y+14, 9);  
  }
  private synchronized void DrawSegment5Off(Graphics g, int x, int y) { 
    g.setColor(BackGround);
    DrawVerticalLine(g, x+12, y+14, 9); 
  } 

  private synchronized void DrawHorizontalLine(Graphics g, int x,
        int y, int width) {
    g.drawLine(x+1, y-1, x+width-2, y-1);
    g.drawLine(x, y, x+width-1, y);
    g.drawLine(x+1, y+1, x+width-2, y+1);
  }

  private synchronized void DrawVerticalLine(Graphics g, int x,
       int y, int height) {
    g.drawLine(x-1, y+1, x-1, y+height-2);
    g.drawLine(x, y, x, y+height-1);
    g.drawLine(x+1, y+1, x+1, y+height-2);
  }


  
  public void start() {
    if (tr == null) {
      tr = new Thread(this);
      tr.start();
    }
  }
 
  public void stop() {
    if (tr != null) {
      tr.stop();
      tr = null;
    }
  }
 
  public void run() {

    while (true) {
      try {
        Thread.currentThread().sleep(speed);
      }  
      catch (InterruptedException e) {
      }  
      super.repaint();
    }
  }

}
