3: Bouncing Balls
Fall 2006, David Matuszek
Display a number of randomly-sized colored balls moving around within a window. These balls move in straight lines, in random directions and random speeds, but bounce off the "walls" (edges) of the window and off of each other.
Here are the various objects you will need to talk to, and the things you can say to them (that is, the messages and the methods).
Here's how to read the method descriptions:
We specify these types when we write the method, but not when
we use it. Since this method is in the
JPanel panel-- This is the part of the "window" that you draw on. My code (below) creates the window, creates the panel (named
panel), and puts the panel in the window.
Graphics getGraphics()-- Asks the panel for its
Graphicsobject. My code (below) uses this; you probably don't have to.
int getWidth()-- Asks the panel for its width in pixels.
int getHeight()-- Asks the panel for its height in pixels.
Graphics g-- This is the thing that represents the "drawable" surface of the panel. It's an old friend--you used it in the very first assignment.
void setColor(Color color)-- Tells the graphics what color to use henceforth.
void fillOval(int x, int y, int width, int height)-- Tells the graphics to draw a "ball" at the given location, with the given height and width.
Ball ball-- This is an object that represents a "ball" (colored circle). It isn't a Sun-supplied class, but one of ours. I will provide some starter code for it, and you will add any methods you need.
new Ball(int x, int y, Color color)-- Constructs a new ball. I've written this code for you. The newly created ball has these characteristics (variables):
yare the coordinates of the top left corner of the "bounding box" (the imaginary rectangle enclosing the ball).
colormay be any Color object you choose. You can use one of the named colors, or create your own color with
new Color(int red, int green, int blue)
diameterof the ball is randomly chosen to be between 10 and 40 pixels. Since we are using exclusively round balls, the ball's height and width are both equal to its diameter.
deltaXis how much the ball's x-coordinate changes each time the ball is moved. This is initially a random
intbetween -4 and +4.
deltaYis how much the ball's y-coordinate changes each time the ball is moved. This is initially a random
intbetween -4 and +4. Together,
deltaYdefine both the ball's speed and its direction.
void draw(Graphics g)-- A ball isn't drawn automatically--it's just an abstract object. This tells the ball to draw itself on the Graphics object
int getDiameter()-- Returns the diameter of the ball. (This is called a "getter method"--you will need to write a few more getter methods.)
ArrayList<Ball>-- This object is a collection of balls (
Ballobjects). For now, don't worry about the strange syntax! Here are some of its methods:
boolean add(Ball myBall)-- Adds
myBallto itself (itself being a list of balls). Always returns
int size()-- Asks the list for the number of balls in it.
Ball get(index)-- Asks for the ball at position
indexin the list;
indexmust be a number between zero and the size of the list minus one. So, for example, if there are five balls in the list, they are numbered 0, 1, 2, 3, and 4.
BouncingBalls-- This is the class containing the
mainmethod. My code creates an object of this type, then calls the object's
runmethod should then do everything that needs to be done. This is a bit of magic to avoid having to make everything
void pause()-- Tells the computer not to do anything for the next 1/20 of a second.
ArrayListso you can find them again. I suggest you do this in a separate method.
Once you have the balls, you need to set up an infinite loop to display them.
(Yes, I really mean an infinite loop; when you want to stop it, use Eclipse's
"red square." The easiest way to create an infinite loop is with
.) Inside the loop, do this:
heightof the panel, because the user might have changed the window size since the last time you went through this loop.
x = x + deltaX
, unless there is a collision.)
y = y + deltaY
The balls should "bounce" off the edges of the window. If a ball
"hits" a vertical (left or right) edge, you should change its
-deltaX; if a horizontal (top or bottom) wall, change its
-deltaY. (This is why you need to know the
height of the panel. Remember, the top left corner of the panel
is always at
The balls should not pass through one another. If a ball gets close enough
to touch another ball, both should "bounce" off. The cheap and easy
way to do this is to change the sign of both
for both balls. Do it this way first, and if you have time, fix it to get a
better looking bounce (caution: requires some math). Notice that this
means you will need to compare the location of each ball with that of
every other ball--use a loop!
Potential bug: If your balls overlap initially, you may find that they are "stuck" together.
The user might change the window size so quickly that some balls are completely outside the panel. (It's not an error to draw outside the panel, but it doesn't do anything, either). When this happens, you should make sure the balls find their way back into the panel, either immediately or just by going in the right direction.
When a ball bounces off a wall or another ball, it's okay if it penetrates a little bit, or bounces before it quite touches. You don't have to be "pixel perfect." However, it shouldn't be glaringly obvious that this is happening, either.
Use lots of methods. Try to give them good names. You need to decide, with your partner, what methods you need, where they go, and who writes them.
Try to figure out which class is the best one to put each method in. For example,
a ball is a thing that can move, so there should be a method in the
class to move the ball; you shouldn't go changing the ball's
y from outside the
Ball class. On the other hand,
telling all the balls to move isn't something that a single ball should be doing;
that probably belongs in the
BouncingBalls class, which is the
"main controller" class.
Ball.java is a new kind of object, representing a "ball" on the screen.
Thursday, September 28, before midnight. Zip up your entire project and submit it via Blackboard. Email submissions will not be accepted.