CS 161 Lab G - Bouncing Balls
Due Fri Oct 25 at 9:00am
Overview
In this lab, you will modify an existing program in order to create an animation of a number of colored balls bouncing around in a box, changing properties (e.g., color, size, speed) when they hit the walls. Because there are too many balls to keep track of individually, you'll need to use an array
to track them all.
This lab will be completed in pairs. Be sure to review the pair programming guidelines. You also must work with a different partner than you have before!
Objectives
- To practice working with arrays: declaring them, instantiating them, and accessing them
- To review using for loops (particularly when working with arrays)
- To working with graphics
Necessary Files
You will need to download and extract the BlueJ project from the LabG.zip file. This project will supply you with the following classes:
-
Ball
This class represents a Ball bouncing in the box. You will need to make a couple of small changes to this class. -
BallCourt
This class represents the box in which the Balls are bouncing. It handles all the moving and drawing of the Balls. You will need to make significant changes to this class.This class is a graphics class (a JComponent), similar to the ones you've created and worked with in the past. Recall that the
public void paintComponent(Graphics g)
method controls how the Component appears on the screen.-
You might also notice that the class uses built-in
fillRect()
andfillOval()
methods to draw shapes, rather than instantiating newRectangle
orEllipse2D.Double
objects. This is mostly for convenience; however, either method is equally effective.
-
You might also notice that the class uses built-in
-
BallCourtFrame
This class creates a frame to hold the BallCourt (similar to how you would create JFrames in your main method). In addition, it also controls the animation of the balls by telling the BallCourt to repaint itself over and over again. You do not need to modify this class. Note that the class has startAnimation() and pauseAnimation() methods that do exactly what they sound like. -
BallCourtFrameTester
A tester for the program. You can use this to test your code, or you can simply create a new instance ofBallCourtFrame
by right-clicking on the class in BlueJ.
Assignment Details
In this lab, you will not need to write any new classes! However, you will need to modify some existing classes in order to add in extra functionality.
-
If you run the program without modifying anything, you should see a single red ball bouncing around on the screen:
Your task is three-fold: (1) replace this singleBall with a collection of 25 balls, (2) add the ability for the balls to have a random color, and (3) have the balls change when they hit a wall.
-
The first thing you should do is declare and instantiate a new array of length 25 to hold your collection of balls. You should do this in the
BallCourt
class.- You may notice that there are "TODO" comments scattered around the class. These will tell you where to put your code. Make sure to delete the TODO comment once you've done the task!
-
The next thing you should do is fill your array with 25 balls! You should use a loop to go through each spot in the array (each room in the hotel) and make a
new Ball()
to put in that room. -
Next, use a loop to draw ALL of the balls on the BallCourt, rather than just the singleBall. (Remember to stop drawing the singleBall!).
- At this point, you should be able to run the program and see a large number of red circles sitting on the screen.
- Next, use a loop to go through your array and tell each Ball in the array to take a step. After you do this, you should be able to see all your red Balls moving and bouncing around the BallCourt!
-
This is great and all, but it will look better if we can change the color of the Balls. Modify the code so that each Ball is created with a random color.
- If you go to the Ball class, you can see that they are all given a specific color in the constructor. This color is drawn from a constant called
COLORS
that is an array of Color objects (aColor[]
). You can choose a random Color from this array by chosing a random number to use an as index into that array--it's like picking a random room number and going to that room to get the Color! - Remember that you can get a random number by using the Math.random() function (to get a number between 0 and 1) or the nextInt() method of a
Random
object (to get a number between 0 and the parameter). Think about what should be the lower and upper bounds of the random number (Hint: does it matter how many items are in the array?)
Once this works, you should be able to have lots of multi-colored balls bouncing around your BallCourt! How festive!
- If you go to the Ball class, you can see that they are all given a specific color in the constructor. This color is drawn from a constant called
-
There is one more step: in order to make things more interesting, modify the code further so that each Ball changes when it bounces off of a wall. Bouncing off one wall (your choice) should have the Ball change Color:
- Inside the
move()
method you should see logic for bouncing off a wall. Take a moment to look over the code and make sure you understand it. Add a few inline comments to note down what is going on! (This can help you out later). In addition to the current functionality, add code so that the Ball changes color. This will involve picking a new color from the COLORS array and assigning that to the Ball's attribute. - Rather than picking a Color randomly, instead make it so the Balls change to the next Color in the array. You will need to keep track of the index of the current Color (hey, is there already an instance variable for this?). Then you can pick the next color by increasing the index by 1.
- What happens when you get to the end of the list? Can you make it so that you start over at the beginning? So that you cycle (that's a hint) through the colors?
- Be sure to check that your color changing works. HINT: try temporarily changing the size of your array to 1 ball, so that it is easier to track and check that it is changing Colors appropriately!
- Inside the
-
Each other wall should make some change to the Ball. For example, maybe
one wall makes it larger and one wall makes it small.
Or maybe one wall speeds up the Ball, and another slows it down. You are welcome to mix and match effects or come up with your own---but at least one wall needs to change the Ball's color!
- If you've got more ideas, you could even make it so that different halves of the wall produce difference changes! How might you add in this functionality?
- It is also possible to make different regions change the Ball--maybe the ball slows down when it nears the center of the BallCourt. Make this as interesting as you want!
- Make sure that you've added your names to the top of the classes you modified, that your code is readable and properly formatted, that you've included detailed inline comments explaining your code, and that everything works smoothly (whew).
- As always, be sure to fill out the lab partner evaluation survey on Moodle after you turn in your work!
Submitting
Make sure both your names are at the top of each file, and upload the entire project directory to the LabG submission folder on hedwig. Only one partner needs to upload the code. Make sure you upload your work to the correct folder! The lab is due at the start of class the morning after lab.
After you have submitted your solution, log onto Moodle and submit the Lab G Partner Evaluation. Both partners need to submit evaluations!
Extensions
Here are some further options you might enjoy. Be sure to finish the main part of the program first!
- You might think about adding a bit more variety to the program. Maybe have the Balls be initialized with a random diameter (maybe between 20 and 50 pixels). Maybe have the balls be more like ovals, with different widths and heights (this would involve changing your drawing code as well as your movement code, since you bounce off the walls at different places!)
- Similarly, you might try "perturbing" the speed as you bounce off the walls, so that things aren't so uniform. Try adding a very small random value (that could be negative!) to the speed after you bounce. This should make it look like your balls have some "spin"
-
Challenging! I have provided an
isTouching()
method that you can use to determine whether one Ball is touching another. Can you modify your code so that the balls bounce off each other as well as the walls?- In your BallCourt class, instead of just moving each Ball, you'll also need to use another loop to run through the Balls again and have your first ball check if it is touching every other ball (though be careful that you're not bouncing off yourself!). If two Balls are touching, you'll want to use the
bounceOff()
method. This should provide basic bouncing (though it is untested).
- In your BallCourt class, instead of just moving each Ball, you'll also need to use another loop to run through the Balls again and have your first ball check if it is touching every other ball (though be careful that you're not bouncing off yourself!). If two Balls are touching, you'll want to use the
Grading
This assignment will be graded on approximately the following criteria:
- Declared and instantiated an array of Balls [10%]
- Have filled the array with Ball objects [10%]
- You draw all of the Balls in the array on the BallCourt [15%]
- All of the Balls in the array move around the BallCourt [15%]
- The Balls are created with random colors [10%]
- The Balls change color in order when they bounce off a wall [15%]
- The Balls change in some way when the bounce of the other walls [10%]
- You have used good programming style and documentation [10%]
- You completed your lab partner evaluation [5%]