import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

/**
 * Kelloapletti.
 */
public class ClockApplet extends JApplet
{
  private int handWidth, tickWidth;
  private ClockAppletPanel clockPanel;
  private ClockTimerThread timer;
  private Thread thread;

  /**
   * Get a parameter value.
   * @param key parametri
   * @param def oletusarvo
   * @return parametrin arvo
   */
  public String getParameter( String key, String def )
  {
    return getParameter( key ) != null ? getParameter( key ) : def;
  }

  /**
   * Construct the applet.
   */
  public ClockApplet()
  {}

  /**
   * Timer thread for moving the hands.
   */
  public class ClockTimerThread implements Runnable
  {
    boolean running = true;

    public void run()
    {
      while ( running )
      {
        clockPanel.repaint();
        try
        {
          Thread.sleep( 950 );
        }
        catch ( InterruptedException e )
        {}
      }
    }

    public void stopIt()
    {
      running = false;
    }
  }

  /**
   * Initialize the applet.
   */
  public void init()
  {
    try
    {
      handWidth = Integer.parseInt( getParameter( "handWidth", "2" ) );
      tickWidth = Integer.parseInt( getParameter( "tickWidth", "1" ) );
    }
    catch ( Exception e )
    {
      e.printStackTrace();
    }
    clockPanel = new ClockAppletPanel( handWidth, tickWidth );
    getContentPane().add( clockPanel, BorderLayout.CENTER );
  }

  /**
   * Start the applet.
   */
  public void start()
  {
    timer = new ClockTimerThread();
    thread = new Thread( timer );
    thread.setDaemon( true );
    thread.start();
  }

  /**
   * Stop the applet.
   */
  public void stop()
  {
    timer.stopIt();
  }

  /**
   * Destroy the applet.
   */
  public void destroy()
  {
    timer.stopIt();
  }

  /**
   * Get Applet information.
   * @return appletin tiedot
   */
  public String getAppletInfo()
  {
    return "Kelloapletti, (p) 2003 Petteri Hämäläinen";
  }

  /**
   * Get parameter info.
   * @return parametritiedot
   */
  public String[][] getParameterInfo()
  {
    String[][] pinfo = {
        { "handWidth", "int", "Viisarinleveys" },
        { "tickWidth", "int", "Kellotaulun tuntimerkin leveys" } };
    return pinfo;
  }

}
