Source of BounceApplet5.java


  1: //BounceApplet5.java
  2: //Demonstrates bouncing balls, with two balls in separate threads,
  3: //a red ball and a green ball, each with a Start and a Stop button.

  5: import java.awt.*;
  6: import java.awt.event.*;
  7: import java.applet.Applet;


 10: public class BounceApplet5
 11: extends Applet
 12: implements ActionListener
 13: {
 14:     private Button startRed, startGreen;
 15:     private Button stopRed,  stopGreen;
 16:     private Ball5  redBall,  greenBall;
 17:     Graphics g;


 20:     public void init()
 21:     {
 22:         startRed   = new Button("Start Red");
 23:         startGreen = new Button("Start Green");
 24:         stopRed    = new Button("Stop Red");
 25:         stopGreen  = new Button("Stop Green");
 26:         add(startRed);
 27:         add(startGreen);
 28:         add(stopRed);
 29:         add(stopGreen);
 30:         startRed.addActionListener(this);
 31:         startGreen.addActionListener(this);
 32:         stopRed.addActionListener(this);
 33:         stopGreen.addActionListener(this);
 34:     }

 36:     
 37:     public void actionPerformed(ActionEvent event)
 38:     {
 39:         if (event.getSource() == startRed)
 40:         {
 41:             g = getGraphics();
 42:             redBall = new Ball5(g, Color.red);
 43:             redBall.start();
 44:         }
 45:         if (event.getSource() == startGreen)
 46:         {
 47:             g = getGraphics();
 48:             greenBall = new Ball5(g, Color.green);
 49:             greenBall.start();
 50:         }
 51:         if (event.getSource() == stopRed)
 52:             redBall.freeze();
 53:         if (event.getSource() == stopGreen)
 54:             greenBall.freeze();
 55:     }
 56: }

 58: 
 59: class Ball5 extends Thread
 60: {
 61:     private Graphics g;
 62:     private Color ballColor;

 64:     private boolean keepGoing;

 66:     // Set rectangle and ball sizes
 67:     private int x = 150, xChange = 7;
 68:     private int y = 90, yChange = 3;
 69:     private int diameter = 10;

 71:     // Set initial ball position
 72:     private int leftX = 100, rightX = 200;
 73:     private int topY  = 40, bottomY = 140;


 76:     public Ball5(Graphics graphics, Color ballColor)
 77:     {
 78:         g = graphics;
 79:         this.ballColor = ballColor;
 80:         keepGoing = true;
 81:     }

 83:     public void freeze()
 84:     {
 85:         keepGoing = false;
 86:     }

 88:     public void run()
 89:     {
 90:         g.setColor(Color.white);
 91:         g.fillRect(leftX, topY, rightX-leftX, bottomY-topY);
 92:         Color backgroundColor = g.getColor();
 93:         while (keepGoing)
 94:         {
 95:             g.setColor(backgroundColor);
 96:             g.fillOval(x, y, diameter, diameter);

 98:             if (x<=leftX || x>=rightX)  xChange = -xChange;
 99:             if (y<=topY  || y>=bottomY) yChange = -yChange;
100:             x = x + xChange;
101:             y = y + yChange;

103:             g.setColor(ballColor);
104:             g.fillOval(x, y, diameter, diameter);
105:             try
106:             {
107:                 sleep(30);
108:             }
109:             catch (InterruptedException e)
110:             {
111:                 System.err.println("Exception: " + e.toString());
112:             }
113:         }
114:     }
115: }