
// Bounce Ball innerhalb eines Thread
// Express-Schalter

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

import javax.swing.*;


public class Bounce5 extends JFrame{

   private Canvas canvas;
   ThreadGroup g1 =  new ThreadGroup("Gruppe1");

   public Bounce5() {
     JToolBar toolBar = new JToolBar();
     JButton BnClear;
     JButton BnStartSingle;
     JButton BnCreateGroup;
     JButton BnStart;
     JButton BnStop;  //   aus dem Speicher entfernen
     JButton BnSuspend;

     JButton BnClose;

      setSize(600, 600);
      setTitle("Bounce5-Thread-Gruppen");

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

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

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

      BnClear = new JButton( "Clear" );
      BnClear.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(ActionEvent e) {
          BnClear_Click(e);
         }
      });
      toolBar.add(BnClear);




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


      BnCreateGroup = new JButton( "Create Group" );
      BnCreateGroup.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(ActionEvent e) {
          BnCreateGroup_Click(e);
         }
      });
      toolBar.add(BnCreateGroup);



      BnSuspend = new JButton( "Suspend Group" );
      BnSuspend.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(ActionEvent e) {
          BnSuspend_Click(e);
         }
      });
      toolBar.add(BnSuspend);

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


      BnStop = new JButton( "Stop Group" );
      BnStop.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(ActionEvent e) {
          BnStopGroup_Click(e);
         }
      });
      toolBar.add(BnStop);


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


   }  //  Bounce5()

   void BnClear_Click(ActionEvent e) {
      Graphics g = canvas.getGraphics();
      g.setColor(Color.white);
      g.fillRect(0, 0, 600, 600);
      g.dispose();
   }

   void BnStartSingle_Click(ActionEvent e) {
       System.out.println("BnStart");
       Ball b = new Ball(canvas, Color.blue);
       b.setPriority(Thread.MIN_PRIORITY);
       b.start();
   }

     // Start Group
   void BnCreateGroup_Click(ActionEvent e) {
      for (int i= 0; i < 5; i++) {
          Ball b = new Ball(g1, canvas, Color.red);
          b.setPriority(Thread.NORM_PRIORITY);
          b.start();
        //  b.suspend();
      }
   //  g1.resume();
   }

   void BnStartGroup_Click(ActionEvent e) {
     g1.resume();
   }


   void BnSuspend_Click(ActionEvent e) {
     g1.suspend();
   } // BnSuspend

   void BnStopGroup_Click(ActionEvent e) {
     g1.stop();
   } // BnStop_Click


   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 Bounce5();
      f.setVisible(true);
   }

}  // Bounce2



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

   // Konstruktor
   public Ball(Canvas c, Color co) {
     number++;
     y = number*30;
     box = c;
     color = co;
   } // Konstruktor

   public Ball(ThreadGroup g, Canvas c, Color co) {
     super(g,"xxxxxxxxxxxx");
     number++;
     y = number*30;
     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.white);
      // g.setXORMode(box.getBackground());
      g.fillOval(x, y, XSIZE, YSIZE);
      x += dx;
      Dimension d = box.getSize();
      if (x < 0) {
       x = 0;
       dx = -dx;
      }

      if (x + XSIZE >= d.width){
        x = d.width - XSIZE;
        dx = -dx;
      }

      g.setColor(color);
      g.fillOval(x, y, XSIZE, YSIZE);
      g.dispose();
   }  // move


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

}  // ball



