// Applet Clock v1.0 - clock.java;

import java.applet.*;
import java.util.*;
import java.awt.*;

public class clock extends Applet implements Runnable
{
   Date date;
   Font font;
   Color fcolor, bcolor;
   boolean ampm, second, blink;
   char seperator;
   Image offscreen;
   Dimension d;
   
   private Thread clock_thread = null;
   
   public void init()
   {
      font = new Font(getParameter("font"), font.BOLD, getIntegerParameter("fontsize"));
      fcolor = getColorParameter("fcolor");
      bcolor = getColorParameter("bcolor");
      ampm = getBooleanParameter("ampm");
      second = getBooleanParameter("second");
      seperator = getCharParameter("seperator");
      d = this.size();
      offscreen = this.createImage(d.width, d.height);
   
      this.setForeground(fcolor);
      this.setBackground(bcolor);

      System.out.println("Clock v1.0 by Horace Chan\n");
   }

   public void start()
   {
      if (clock_thread == null)
      {
         clock_thread = new Thread(this);
         clock_thread.start();
      }
   }

   public void stop()
   {
      if ((clock_thread != null) && clock_thread.isAlive())
         clock_thread.stop();
      clock_thread = null;
   }

   public void destroy()
   {
      offscreen.flush();
   }

   public void paint(Graphics g)
   {
      String sztime;
      int tempint, hour;
      char tempsep;
      
      g.setColor(bcolor);
      g.fillRect(0,0,d.width,d.height);
      g.setColor(fcolor);

      g.setFont(font);

      if (blink)
         tempsep = seperator;
      else
         tempsep = ' ';

      sztime = new String("");
      
      hour = date.getHours();
      if (ampm)
      {
         tempint = hour%12;
         if (tempint == 0) tempint = 12;
      } 
      else
         tempint = hour;
      
      if (tempint < 10) sztime = sztime + '0';
      sztime = sztime + Integer.toString(tempint);
      sztime = sztime + seperator;

      tempint = date.getMinutes();
      if (tempint < 10) sztime = sztime + '0';
      sztime = sztime + Integer.toString(tempint);
      
      if (second)
      {
         sztime = sztime + tempsep;
         tempint = date.getSeconds();
         if (tempint < 10) sztime = sztime + '0';
         sztime = sztime + Integer.toString(tempint);
      }

      if (ampm)
      {
         if (hour < 12)
            sztime = sztime + " a.m.";
         else
            sztime = sztime + " p.m.";
      }

      g.drawString(sztime,5,(font.getSize()+5));
      blink = !blink;
   }
   
   public void run()
   {
      long delay = 500;
      Graphics g;

      while (true)
      {
         date = new Date();
         g = offscreen.getGraphics();
         paint(g);
         g = this.getGraphics();
         g.drawImage(offscreen, 0, 0, this);
         try { Thread.sleep(delay); }
         catch (InterruptedException e) {};
      }
   }

   protected Color getColorParameter(String name)
   {
      String value = this.getParameter(name);
      int intvalue;
      try { intvalue = Integer.parseInt(value, 16); }
      catch (NumberFormatException e)
      { return null; }
      return new Color(intvalue);
   }

   protected boolean getBooleanParameter(String name)
   {
      Boolean tempbool;
      String value = this.getParameter(name);
      
      tempbool = new Boolean(value);   
      return tempbool.booleanValue();
   }

   protected int getIntegerParameter(String name)
   {
      String value = this.getParameter(name);
      int intvalue;
      try  { intvalue = Integer.parseInt(value, 10); }
      catch (NumberFormatException e)
      { return 12; }
      return intvalue;
   }

   protected char getCharParameter(String name)
   {
      String value = this.getParameter(name);
      char charvalue;
      try { charvalue = value.charAt(0); }
      catch (StringIndexOutOfBoundsException e)
      { return ':'; }
      return charvalue;
   }

   public String[][] getParameterInfo()
   {
      String[][] info =
      {  {"font", "font name", "font"},
         {"fcolor", "hex value", "foreground color"},
         {"bcolor", "hex value", "background color"},
         {"ampm", "true/false", "display 24hrs or am/pm"},
         {"second", "true/false", "display second"},
         {"seperator", "char", "seperator"} };
      
      return info;
   }

   public String getAppletInfo()
   {
      return "Clock v1.0 - by Horace Chan";      
   }
}
