
// Bounce Ball innerhalb eines Thread

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

import javax.swing.*;


public class Bounce2 extends JFrame{

   private Canvas canvas;

   public Bounce2() {
     JToolBar toolBar = new JToolBar();
     JButton BnStart, BnClose;

      setSize(600, 300);
      setTitle("Bounce2");

      this.getContentPane().setLayout ( new BorderLayout() );

      canvas = new Canvas();
      this.getContentPane().add(canvas, BorderLayout.CENTER);

      this.getContentPane().add(toolBar, BorderLayout.NORTH);

      BnStart = new JButton( "Start" );
      BnStart.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(ActionEvent e) {
          BnStart_Click(e);
         }
      });
      toolBar.add(BnStart);


      BnClose = new JButton( "Close" );
      BnClose.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(ActionEvent e) {
          BnClose_Click(e);
         }
      });
      toolBar.add(BnClose);

   }  //  Bounce2()


   void BnStart_Click(ActionEvent e) {
       System.out.println("JButton1");
	//for (int i=0; i<30; i++) {
           Ball b = new Ball(canvas, Color.blue);
           b.start();   // b.bounce
	// }
   }


   void BnClose_Click(ActionEvent e) {
       System.out.println("Close");
       canvas.setVisible(false);
       System.exit(0);
    }



  //Überschreiben, damit das Programm bei Herunterfahren des Systems beendet werden kann
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

   public static void main(String[] args) {
      Frame f = new Bounce2();
      f.show();
   }

}  // Bounce2



class Ball extends Thread
{
   private Canvas box;
   private static final int XSIZE = 10;
   private static final int YSIZE = 10;
   private int x = 0;
   private int y = 0;
   private int dx = 2;
   private int dy = 2;
   private Color color;

   // Konstruktor
   public Ball(Canvas c, Color co) {
     box = c;
     color = co;
   } // Konstruktor


   public void draw(){
      Graphics g = box.getGraphics();
      g.setColor(color);
      g.fillOval(x, y, XSIZE, YSIZE);
      g.dispose();
   }  // draw

   public void move() {
      if (!box.isVisible()) return;
      Graphics g = box.getGraphics();
      g.setColor(color);
      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();
   }  // move


   public void run() {
      draw();
      for (int i = 1; i <= 4000; i++)
      {  
         move();
         try { 
           Thread.sleep(5); 
         }
         catch(InterruptedException e) {}
      }
   } // run

}  // ball




