Source of Bounce.java


  1: /**
  2:    @version 1.32 2004-07-27
  3:    @author Cay Horstmann
  4: */
  5: 
  6: import java.awt.*;
  7: import java.awt.event.*;
  8: import java.awt.geom.*;
  9: import java.util.*;
 10: import javax.swing.*;
 11: 
 12: /**
 13:    Shows an animated bouncing ball.
 14: */
 15: public class Bounce
 16: {
 17:    public static void main(String[] args)
 18:    {
 19:       JFrame frame = new BounceFrame();
 20:       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 21:       frame.setVisible(true);
 22:    }
 23: }
 24: 
 25: /**
 26:    A ball that moves and bounces off the edges of a 
 27:    rectangle
 28: */
 29: class Ball
 30: {
 31:    /**
 32:       Moves the ball to the next position, reversing direction
 33:       if it hits one of the edges
 34:    */
 35:    public void move(Rectangle2D bounds)
 36:    {
 37:       x += dx;
 38:       y += dy;
 39:       if (x < bounds.getMinX())
 40:       { 
 41:          x = bounds.getMinX();
 42:          dx = -dx;
 43:       }
 44:       if (x + XSIZE >= bounds.getMaxX())
 45:       {
 46:          x = bounds.getMaxX() - XSIZE; 
 47:          dx = -dx; 
 48:       }
 49:       if (y < bounds.getMinY())
 50:       {
 51:          y = bounds.getMinY(); 
 52:          dy = -dy;
 53:       }
 54:       if (y + YSIZE >= bounds.getMaxY())
 55:       {
 56:          y = bounds.getMaxY() - YSIZE;
 57:          dy = -dy; 
 58:       }
 59:    }
 60: 
 61:    /**
 62:       Gets the shape of the ball at its current position.
 63:    */
 64:    public Ellipse2D getShape()
 65:    {
 66:       return new Ellipse2D.Double(x, y, XSIZE, YSIZE);
 67:    }
 68: 
 69:    private static final int XSIZE = 15;
 70:    private static final int YSIZE = 15;
 71:    private double x = 0;
 72:    private double y = 0;
 73:    private double dx = 1;
 74:    private double dy = 1;
 75: }
 76: 
 77: /**
 78:    The panel that draws the balls.
 79: */
 80: class BallPanel extends JPanel
 81: {
 82:    /**
 83:       Add a ball to the panel.
 84:       @param b the ball to add
 85:    */
 86:    public void add(Ball b)
 87:    {
 88:       balls.add(b);
 89:    }
 90: 
 91:    public void paintComponent(Graphics g)
 92:    {
 93:       super.paintComponent(g);
 94:       Graphics2D g2 = (Graphics2D) g;
 95:       for (Ball b : balls)
 96:       {
 97:          g2.fill(b.getShape());
 98:       }
 99:    }
100: 
101:    private ArrayList<Ball> balls = new ArrayList<Ball>();
102: }
103: 
104: /**
105:    The frame with panel and buttons.
106: */
107: class BounceFrame extends JFrame
108: {
109:    /**
110:       Constructs the frame with the panel for showing the
111:       bouncing ball and Start and Close buttons
112:    */
113:    public BounceFrame()
114:    {
115:       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
116:       setTitle("Bounce");
117: 
118:       panel = new BallPanel();
119:       add(panel, BorderLayout.CENTER);
120:       JPanel buttonPanel = new JPanel();
121:       addButton(buttonPanel, "Start",
122:          new ActionListener()
123:          {  
124:             public void actionPerformed(ActionEvent event)
125:             {
126:                addBall();
127:             }
128:          });
129:       
130:       addButton(buttonPanel, "Close",
131:          new ActionListener()
132:          {
133:             public void actionPerformed(ActionEvent event)
134:             {
135:                System.exit(0);
136:             }
137:          });
138:       add(buttonPanel, BorderLayout.SOUTH);
139:    }
140: 
141:    /**
142:       Adds a button to a container.
143:       @param c the container
144:       @param title the button title
145:       @param listener the action listener for the button
146:    */
147:    public void addButton(Container c, String title, ActionListener listener)
148:    {
149:       JButton button = new JButton(title);
150:       c.add(button);
151:       button.addActionListener(listener);
152:    }
153: 
154:    /**
155:       Adds a bouncing ball to the panel and makes 
156:       it bounce 1,000 times.
157:    */
158:    public void addBall()
159:    {
160:       try
161:       {
162:          Ball ball = new Ball();
163:          panel.add(ball);
164: 
165:          for (int i = 1; i <= STEPS; i++)
166:          {
167:             ball.move(panel.getBounds());
168:             panel.paint(panel.getGraphics());
169:             Thread.sleep(DELAY);
170:          }
171:       }
172:       catch (InterruptedException e)
173:       {                    
174:       }
175:    }
176: 
177:    private BallPanel panel;
178:    public static final int DEFAULT_WIDTH = 450;
179:    public static final int DEFAULT_HEIGHT = 350;  
180:    public static final int STEPS = 1000;
181:    public static final int DELAY = 3;
182: }
183: 
184: 
185: 
186: 
187: 
188: 
189: 
190: 
191: 
192: 
193: 
194: 
195: 
196: 
197: 
198: 
199: