import javax.swing.UIManager;
import java.awt.*;

/**
 * Varsinainen ohjelma.
 */
public class ClockApp
{
  private ClockFrame frame;

  /**
   * Luo ja rakentaa ohjelman.
   */
  public ClockApp()
  {
    frame = new ClockFrame();
    frame.validate();

    // keskittää ohjelman ruudun keskelle
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if ( frameSize.height > screenSize.height )
      frameSize.height = screenSize.height;

    if ( frameSize.width > screenSize.width )
      frameSize.width = screenSize.width;

    frame.setLocation( ( screenSize.width - frameSize.width ) / 2, ( screenSize.height - frameSize.height ) / 2 );

    // näyttää ohjelman
    frame.setVisible( true );

    // luo ajastinsäikeen ja käynnistää sen
    ClockTimerThread r = new ClockTimerThread();
    Thread t = new Thread( r );
    t.setDaemon( true );
    t.start();
  }

  /**
   * Main-metodi.
   * @param args käynnistysparametrit
   */
  public static void main( String[] args )
  {
    try
    {
      UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
    }
    catch ( Exception e )
    {
      e.printStackTrace();
    }
    new ClockApp();
  }

  /**
   * Ajastin säie.
   */
  private class ClockTimerThread implements Runnable
  {
    boolean running = true;

    /**
     * Säikeen ajo ja olemassaolo.
     */
    public void run()
    {
      while ( running )
      {
        frame.repaint();
        try
        {
          // nukuttaa säikeen noin sekunnin ajaksi
          Thread.sleep( 950 );
        }
        catch ( InterruptedException e )
        {}
      }
    }

    /**
     * Pysäyttää säikeen ja aiheuttaa sen kuoleman.
     */
    public void stopIt()
    {
      running = false;
    }
  }

}
