import java.awt.*;
import java.awt.event.*;

public class Bounce
{
  // Hier startet das Programm
  public static void main( String[] s )
  {
    // erzeugen und anzeigen des Fensters
    new BounceFrame().show();
  }
}

class BounceFrame extends Frame
{
  // Bereich, in dem der Ball springt
  private Panel p1 = new Panel();
  // breich mit den Buttons
  private Panel p2 = new Panel();
  
  // Konstruktor
  public BounceFrame()
  {
    setSize(300,200);
    setTitle("Bouncer");
    
    addWindowListener( 
      new WindowAdapter(){
        public void windowClosing( WindowEvent wEvt )
        {
          dispose();
          System.exit(0);
        }
      });
    
    
    add(  p2, "start", 
          new ActionListener(){
            public void actionPerformed( ActionEvent aEvt )
            {
              new Ball(p1).start();
            }
          });
    add(  p2, "stop", 
          new ActionListener(){
            public void actionPerformed( ActionEvent aEvt )
            {
              System.exit(0);
            }
          });
    add(  p1, "Center");
    add(  p2, "South");    
  }
  
  // Methode zum erzeugen und einfügen von Buttons
  public void add( Panel p, String text, ActionListener a )
  {
    Button b = new Button(text);
    p.add( b );
    b.addActionListener( a );
  }
}

// Klasse zum zeichnen eines Balles und zur Berechnung der Koordinaten
class Ball extends Thread
{
  private Panel box = null;
  private static final int XSIZE = 10;
  private static final int YSIZE = 10;
  private int x,y;
  private int dx = 2;
  private int dy = 2;
  
  public Ball( Panel p )
  {
    box = p;
  }
  
  // zeichnen
  public void draw()
  {
    Graphics g = box.getGraphics();
    g.fillOval(x,y,XSIZE, YSIZE );
    g.dispose();
  }
  // neue Koordinaten ermitteln
  public void move()
  {
    Graphics g = box.getGraphics();
    g.setXORMode( box.getBackground());
    g.fillOval(x,y,XSIZE, YSIZE );
    
    x += dx;
    y += dy;
    
    Dimension d = box.getSize();
    
    if ( x<0 ){ x=0; dx = -dx; }
    if ( x+XSIZE>=d.width ){ x=d.width-XSIZE; dx=-dx; }
    if ( y<0 ){ y=0; dy=-dy; }
    if ( y+YSIZE>=d.height ){ y=d.height-YSIZE; dy=-dy; }
    
    g.fillOval(x,y,XSIZE,YSIZE);
    g.dispose();
  }
  
  // ball anstoßen
  public void run()
  {
    draw();
    for( int i=0 ; i<1000 ; i++ )
    {
      move();
      try{ Thread.sleep(5); }
      catch( InterruptedException ie ){}
    }
  }
}


