/*
 * ISO compatible Clock
 */

import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;

/**
 * ISO compatible Clock
 * Created by Nikolai Sandved (webmaster@ProbabilityOf.com)
 * Published at http://www.ProbabilityOf.com/ISOClock.shtml
 */

public class ISOClock extends Applet implements Runnable {
    Thread timer;                // The thread that displays clock
    SimpleDateFormat formatter;  // Formats the date displayed
    String lastdate;             // String to hold date displayed
    Font clockFaceFont;          // Font for number display on clock
    Date currentDate;            // Used to get date to display
    Color numberColor;           // Color of second hand and numbers

    /* dd   - 01-31 day with leading zero
       EEE  -
       MM   - 01-12 Month with leading zero
       MMM  - Jan, Feb, ..., Dec Month abr. three letter
       yyyy - Four digit Year
       HH   - 00-24 Hour
       hh   - 00-12 Hour
       mm   - 00-59 Minute
       ss   - 00-59 Seconds
     */
    String OwnDateFormat;        // Digital Clock Format                             
    int ClockFontSize;           // Size on Font used to display Digital Clock

    /* Serif
       TimesRoman
       Courier
    */
    String ClockFont;            // Font used to display Digital Clock

    public void init() {
        OwnDateFormat = "yyyy-MM-dd HH:mm:ss"; // This is the ISO standard
        ClockFontSize = 12 ; 
        ClockFont = "Serif"; 
        formatter = new SimpleDateFormat (OwnDateFormat, Locale.getDefault());
        currentDate = new Date();
        lastdate = formatter.format(currentDate);
        /*blue, darkGray,...*/
        numberColor = Color.black;

        try {
            setBackground(new Color(Integer.parseInt(getParameter("bgcolor"),16)));
        } catch (Exception E) { }
        try {
            numberColor = new Color(Integer.parseInt(getParameter("fgcolor"),16));
        } catch (Exception E) { }
        try {
            OwnDateFormat = new String((getParameter("ODateFormat")));
        } catch (Exception E) { }
        try {
            ClockFont = new String((getParameter("CFont")));
        } catch (Exception E) { }
        try {
            ClockFontSize = Integer.parseInt(getParameter("CFontSize"));
        } catch (Exception E) { }
        
        clockFaceFont = new Font(ClockFont, Font.PLAIN, ClockFontSize);

        resize(12*ClockFontSize,ClockFontSize+10);              // Set clock window size
    }

    // Paint is the main part of the program
    public void paint(Graphics g) {
        String today;
        currentDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault());

        formatter.applyPattern(OwnDateFormat);
        today = formatter.format(currentDate);

        g.setFont(clockFaceFont);

        // Get background color.
        g.setColor(getBackground()); 
        // Overwrite laste date with background color
        g.drawString(lastdate, 1, ClockFontSize+1); 

        // Set date color
        g.setColor(numberColor);  
        g.drawString("", 1, ClockFontSize+1);
        g.drawString(today, 1, ClockFontSize+1);

        lastdate = today;
        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(100);
            } catch (InterruptedException e) {
            }
            repaint();
        }
    }

    public void update(Graphics g) {
        paint(g);
    }

    public String getAppletInfo() {
       return "Title: A ISO compatible Clock \nAuthor: Nikolai Aasen, 1999";
    }
  
    public String[][] getParameterInfo() {
        String[][] info = {
            {"CFontSize", "Font Size", "Font size"},
            {"bgcolor", "hexadecimal RGB number", "The background color. Default is the color of your browser."},
            {"fgcolor", "hexadecimal RGB number", "The color of the seconds hand and numbers. Default is dark gray."},
            {"ODateFormat", "Date Format", "dd - 01-31 day with leading zero, EEE  -, MM - 01-12 Month with leading zero, MMM - Jan, Feb, ..., Dec Month abr. three letter, yyyy - Four digit Year, HH - 00-24 Hour, hh- 00-12 Hour, mm - 00-59 Minute, ss - 00-59 Seconds"},
            {"CFont", "Font to the Clock", "TimesRoman, Courier, Serif"}
        };
        return info;
    }
}
