Assignment 3: Bouncing Balls
Fall 2006, David Matuszek

Purposes of this assignment:

General idea of the assignment:

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.

What you need to know:

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:

     Example: boolean startsWith(String prefix) (in the String class)

  • boolean is the type of value that is returned.
  • startsWith is the name of the method.
  • String is the type of parameter that is expected.
  • prefix is what the parameter is called inside the method.

We specify these types when we write the method, but not when we use it. Since this method is in the String class, we can send a String object a startsWith message like this:

     if (myString.startsWith("non")) { ... }

What you need to do:

To get started:

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 while (true) { ... }.) Inside the loop, do this:

Here's why it's complicated:

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 to -deltaX; if a horizontal (top or bottom) wall, change its deltaY to -deltaY. (This is why you need to know the width and height of the panel. Remember, the top left corner of the panel is always at x=0, y=0.)

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 deltaX and deltaY, 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.

Important style considerations:

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 Ball class to move the ball; you shouldn't go changing the ball's x and 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.

Starter code: is the "main" class (it contains a main method). is a new kind of object, representing a "ball" on the screen.

Due date:

Thursday, September 28, before midnight. Zip up your entire project and submit it via Blackboard. Email submissions will not be accepted.