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

/**
 * Kellopaneli.
 */
public class ClockAppletPanel extends JComponent
{
  private BasicStroke handStroke, tickStroke;

  public ClockAppletPanel()
  {}

  /**
   * Konstruktori.
   * @param handWidth viisarin leveys
   * @param tickWidth kellotaulun tuntimerkin leveys
   */
  public ClockAppletPanel( int handWidth, int tickWidth )
  {
    handStroke = new BasicStroke( handWidth );
    tickStroke = new BasicStroke( tickWidth );
  }

  /**
   * Piirtometodi.
   * @param grfx piirtokonteksti
   */
  public void paintComponent( Graphics grfx )
  {
    Graphics2D g = ( Graphics2D ) grfx;
    g.setStroke( tickStroke );
    g.setColor( Color.black );
    Dimension size = this.getSize();

    for ( double x = 0; x < 2 * Math.PI; x = x + 0.5236 ) // sin() ja cos() käyttävät radiaaneja, 6.2832 == 2*PI
    {
      g.drawLine( ( int ) ( size.width / 2 + Math.sin( x ) * size.width * 0.45 ),
                  ( int ) ( size.height / 2 - Math.cos( x ) * size.height * 0.45 ),
                  ( int ) ( size.width / 2 + Math.sin( x ) * size.width * 0.4 ),
                  ( int ) ( size.height / 2 - Math.cos( x ) * size.height * 0.4 ) ); // piirtää kellotaulun
    }

    Calendar cal = Calendar.getInstance();
    int second = cal.get( Calendar.SECOND );
    int minute = cal.get( Calendar.MINUTE );
    int hour = cal.get( Calendar.HOUR );

    g.setStroke( handStroke );
    g.setColor( Color.blue );
    g.drawLine( ( int ) size.width / 2, ( int ) size.height / 2,
                ( int ) size.width / 2 + ( int ) ( Math.sin( 2 * Math.PI * second / 60 ) * size.width * 0.34 ),
                ( int ) size.height / 2 - ( int ) ( Math.cos( 2 * Math.PI * second / 60 ) * size.height * 0.34 ) ); // sekunnit

    g.drawLine( ( int ) size.width / 2, ( int ) size.height / 2,
                ( int ) size.width / 2 + ( int ) ( Math.sin( 2 * Math.PI * minute / 60 ) * size.width * 0.3 ),
                ( int ) size.height / 2 - ( int ) ( Math.cos( 2 * Math.PI * minute / 60 ) * size.height * 0.3 ) ); // minuutit

    g.drawLine( ( int ) size.width / 2, ( int ) size.height / 2,
                ( int ) size.width / 2 + ( int ) ( Math.sin( 2 * Math.PI * hour / 12 ) * size.width * 0.25 ),
                ( int ) size.height / 2 - ( int ) ( Math.cos( 2 * Math.PI * hour / 12 ) * size.height * 0.25 ) ); // tunnit
  }

}
