1  /*
  2   * Adapted from an example by
  3   * Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM)
  4   * Published By SunSoft Press/Prentice-Hall
  5   *
  6   * An application with multiple threads for the bouncing balls in a frame.
  7   */
  8  
  9  import java.awt.*;
 10  import java.awt.event.*;
 11  
 12  public class BounceThread extends Frame implements ActionListener
 13  {
 14     private Canvas canvas;
 15  
 16     // the constructor initializes the frame to be a borderlayout 
 17     //    with a canvas in the center and
 18     //    two buttons labelled Start and Close in the south
 19  
 20     public BounceThread()
 21     {  setLayout(new BorderLayout());
 22        setTitle("BounceThread");
 23  
 24        canvas = new Canvas();
 25        add(canvas, BorderLayout.CENTER);
 26  
 27        Panel p = new Panel();
 28        Button b1 = new Button("Start");
 29        b1.addActionListener(this);
 30        p.add(b1);
 31        Button b2 = new Button("Close");
 32        b2.addActionListener(this);
 33        p.add(b2);
 34        add(p, BorderLayout.SOUTH);
 35     }
 36  
 37     // responds to the Start button by creating a new instance 
 38     //          of the BallTimes class and starting its thread
 39     // responds to the close button by exiting the application
 40  
 41     public void actionPerformed(ActionEvent evt)
 42     {  if (evt.getActionCommand().equals("Start"))
 43        {  Ball b = new Ball(canvas);
 44           b.start();
 45        }
 46        else if (evt.getActionCommand().equals("Close"))
 47           System.exit(0);
 48     }   
 49  
 50     // the main method creates an instance of this class and sets the
 51     // properties of the frame 
 52   
 53     public static void main(String[] args)
 54     {  Frame f = new BounceThread();
 55        f.setSize(400, 300);
 56        f.setVisible(true);  
 57     }
 58     
 59  }
 60  
 61  /* The Ball class is a subclass of Thread, so each instance inherits
 62   *   thread methods start, stop, sleep, etc.
 63   * The run method of the thread loops to draw a ball in a new location
 64   *   for a fixed number of iterations.
 65   */
 66  
 67  class Ball extends Thread
 68  {  
 69     private Canvas box;
 70     private static final int XSIZE = 10;
 71     private static final int YSIZE = 10;
 72     private int x = 0;
 73     private int y = 0;
 74     private int dx = 2;
 75     private int dy = 2;   
 76  
 77     // the constructor initializes the canvas to that of the frame
 78     public Ball(Canvas c) { box = c; }
 79  
 80     // draws the ball in the current location
 81     public void draw()
 82     {  Graphics g = box.getGraphics();
 83        g.fillOval(x, y, XSIZE, YSIZE);
 84        g.dispose();
 85     }
 86  
 87     // increments the location of the ball, checks the boundary to see
 88     // if it should "bounce", and draws the ball
 89     // (uses XOR graphics to draw over previous location in the background
 90     //     color.)
 91  
 92     public void move()
 93     {  Graphics g = box.getGraphics();
 94        g.setXORMode(box.getBackground());
 95        g.fillOval(x, y, XSIZE, YSIZE);
 96        x += dx;
 97        y += dy;
 98        Dimension d = box.getSize();
 99        if (x < 0) { x = 0; dx = -dx; }
100        if (x + XSIZE >= d.width) { x = d.width - XSIZE; dx = -dx; }
101        if (y < 0) { y = 0; dy = -dy; }
102        if (y + YSIZE >= d.height) { y = d.height - YSIZE; dy = -dy; }
103        g.fillOval(x, y, XSIZE, YSIZE);
104        g.dispose();
105     }
106  
107     // loops for 1000 times and moves the ball   
108     public void run()
109     {  draw();
110        for (int i = 1; i <= 1000; i++)
111        {  move();
112           try { Thread.sleep(5); } catch(InterruptedException e) {}
113        }
114     }
115  }
116  
117