import java.awt.*;
import java.applet.*;
import java.util.Date;


public class RDate extends Applet implements Runnable {
  int lastDate, lastMonth, lastYear;
  Thread tr;
  int speed = 1000;  //  1 sec
  int lastTenDate, lastTenMonth, lastTenYear;
  Color LEDColour;
  Color BackGround;
  Color FrameColour;
  Font fonti;
  
  public void init() {
       
    String  param;

    lastDate=0;
    lastMonth=0;
    lastYear=0;
    lastTenDate=0;
    lastTenMonth=0;
    lastTenYear=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  tDate = 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, tDate); 
   }

  public final synchronized void update (Graphics g) {
    int theDate, theMonth, theYear;
    int theTenDate, theTenMonth, theTenYear;
    Date  aDate = new Date();

    theDate = aDate.getDate();
    theMonth = aDate.getMonth();
    theYear = aDate.getYear();
    theTenDate = theDate / 10;
    theDate = theDate - theTenDate * 10;
    theTenMonth = theMonth / 9;
    theMonth = (theMonth - (theTenMonth * 10))+1;
    theTenYear = theYear / 10;
    theYear = theYear - theTenYear * 10;

    
   if (theDate != lastDate)
      {
      DrawDate(theDate);
      lastDate = theDate;
      }
    if (theTenDate != lastTenDate)
      {
      DrawTenDate(theTenDate);
      lastTenDate = theTenDate;
      }
   if (theMonth != lastMonth)
      {
      DrawMonth(theMonth);
      lastMonth = theMonth;
      }
    if (theTenMonth != lastTenMonth)
      {
      DrawTenMonth(theTenMonth);
      lastTenMonth = theTenMonth;
      }
   if (theYear != lastYear)
      {
      DrawYear(theYear);
      lastYear = theYear;
      }
    if (theTenYear != lastTenYear)
      {
      DrawTenYear(theTenYear);
      lastTenYear = theTenYear;
      }

    DrawPoints();		//pisteet 	  00.00.00
    
  }

 
  public synchronized void DrawClock (Graphics g, Date aDate) {
    int theDate, theMonth, theYear;
    int theTenDate, theTenMonth, theTenYear;

    theDate = aDate.getDate();
    theMonth = aDate.getMonth();
    theYear = aDate.getYear();
    theTenDate = theDate / 10;
    theDate = theDate - theTenDate * 10;
    theTenMonth = theMonth / 9;
    theMonth = (theMonth - (theTenMonth * 10))+1;
    theTenYear = theYear / 10;
    theYear = theYear - theTenYear * 10;
    
    DrawDate(theDate);
    DrawTenDate(theTenDate);
    DrawMonth(theMonth);
    DrawTenMonth(theTenMonth);
    DrawYear(theYear);
    DrawTenYear(theTenYear);
    DrawPoints();
  }

 
  public synchronized void DrawTenDate(int theValue) {
    DrawSevenSegments(getGraphics(), theValue, 5, 5);
  }

  public synchronized void DrawDate(int theValue) {
    DrawSevenSegments(getGraphics(), theValue, 23, 5);
  }
  public synchronized void DrawTenMonth(int theValue) {
    DrawSevenSegments(getGraphics(), theValue, 43, 5);
  }

  public synchronized void DrawMonth(int theValue) {
    DrawSevenSegments(getGraphics(), theValue, 61, 5);
  }
  public synchronized void DrawTenYear(int theValue) {
    DrawSevenSegments(getGraphics(), theValue, 81, 5);
  }

  public synchronized void DrawYear(int theValue) {
    DrawSevenSegments(getGraphics(), theValue, 99, 5);
  }
 
 
  
 public synchronized void DrawPoints() { 
    Graphics g = getGraphics();

   g.setColor(LEDColour);
   g.fillRect(39, 28, 2, 2);
   g.fillRect(77, 28, 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();
    }
  }

}
