/*
 * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM)
 * Published By SunSoft Press/Prentice-Hall
 * Copyright (C) 1996 Sun Microsystems Inc.
 * All Rights Reserved. ISBN 0-13-565755-5
 *
 * Permission to use, copy, modify, and distribute this 
 * software and its documentation for NON-COMMERCIAL purposes
 * and without fee is hereby granted provided that this 
 * copyright notice appears in all copies. 
 * 
 * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR 
 * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS
 * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED 
 * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING 
 * THIS SOFTWARE OR ITS DERIVATIVES.
 */
 
/**
 * @version 1.00 07 Feb 1996 
 * @author Cay Horstmann
 */

import java.awt.*;
import java.applet.*;

public class PanelTest extends Applet
{  public void init()
   {  
                     // Das Layout des Applets wird erstellt 
      
      Panel p = new Panel();
       
                     // Das Layout der Buttons wird festgelegt
      p.setLayout(new FlowLayout());
      
                     // Die Buttons werden eingefügt
      p.add(new Button("Tick"));
      p.add(new Button("Reset"));
         
         // p.add(new Button("Close"));   Dies waere sinnvoll, wenn
         //                               mann ein JAVA-Programm hätte
                                        
      add("Right", p);
      
                  // Die Uhr wird erzeugt und in das Applet integriert
      clock = new ClockCanvas(size());
      add("Center",clock);

      
   }
                  // Dies ist die EVENT-Behandlung im JAVA Programm,
                  // um das Fenster zu schliessen.
   public boolean handleEvent(Event evt)
   {  if (evt.id == Event.WINDOW_DESTROY) System.exit(0);
      return super.handleEvent(evt);
   }


                  // Event-Behandlungs-Methode
   public boolean action(Event evt, Object arg)
   {  if (arg.equals("Tick")) clock.tick();
      else if (arg.equals("Reset")) clock.reset();
     // else if (arg.equals("Close")) System.exit(0);
      else return false;
      return true;
   }
   
/*   public static void main(String[] args)
   {  Frame f = new PanelTest();
      f.resize(300, 200);
      f.show();  
   }*/
   
   private ClockCanvas clock;
      
}


            // Die Uhr-Klasse 
class ClockCanvas extends Canvas
{      Dimension prefSize;            // varaible fuer die Größe
     
                          
                                      // Speichern der Größe 
     public ClockCanvas (Dimension prefSize)
     {
       this.prefSize = prefSize;
     }  

     public Dimension preferredSize()   // gewünschte Größe
      {
       return prefSize;
      }
     public Dimension minimumSize()     // minimum Größe
      {
       return prefSize;
      }
     

     public void paint(Graphics g)     // Zeichnen der Uhr
   {  g.drawOval(0, 0, 100, 100);
      double hourAngle = 2 * Math.PI * (minutes - 3 * 60)
          / (12 * 60);
      double minuteAngle = 2 * Math.PI * (minutes - 15) 
         / 60;
      g.drawLine(50, 50, 
         50 + (int)(30 * Math.cos(hourAngle)), 
         50 + (int)(30 * Math.sin(hourAngle)));
      g.drawLine(50, 50, 
         50 + (int)(45 * Math.cos(minuteAngle)), 
         50 + (int)(45 * Math.sin(minuteAngle)));
   }
   
   public void reset()                // Rücksetzen & Neuzeichnen der Uhr
   {  minutes = 0;
      repaint();
   }
   
   public void tick()                 // Die Uhr eine Minute weitersetzen
   {  minutes++;  
      repaint();
   }
   
   int minutes = 0;
}
