CS 161 Lab F - Bouncing Balls
Due Thurs Mar 07 at 11:59pm
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 colors 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)
Necessary Files
You will need to download and extract the BlueJ project from the LabF.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 not an Applet, though it functions similar to one. Like an Applet, this class has a
paint(Graphics g)
method that acts like the "main" method of your drawing: it defines what happens whenever the window is drawn on the screen. Note that this class calls the methodpaintComponent()
rather thanpaint()
(Java wouldn't want to make things easy on you by using the same name now, would it?) We are not using an Applet for this lab because you can't easily print to the console from them; thus we're graduating to bigger and better graphical systems! -
BallCourtFrame
This class creates a frame to hold the BallCourt. 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 of BallCourtFrame by right-clicking on the class in BlueJ. If you do this, you can use the startAnimation() and stopAnimation() methods easily.
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 two-fold: (1) replace this singleBall with a collection of 25 balls, and (2) add the ability for the balls to have a random color, as well as change color 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 apartment) and make a new Ball to put 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, let's modify the code further so that the Balls change color when they bounce off of a wall.
- 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. Question: do the inline comments help make the code easier to follow? 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
- Make sure that you've added your names to the top of the classes you modified, that you've included some inline comments explaining your code, and that everything works smoothly
- As always, be sure to fill out the lab partner evaluation survey on Moodle after you turn in your work!
Submitting
- Once you are sure that your program works, make sure that both your and your partner's names are in a class comment at the top of the file. Then submit your program to the submissions folder.
- Upload the your modified
Ball.java
class andBallCourt.java
class to the LabF folder on the submission folder on hedwig. Make sure you upload your work to the correct folder! The lab is due at midnight on the day of the lab. - Again, please fill out the lab partner evaluation on Moodle! Pretty please with sugar on top?
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 also have 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 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 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 [15%]
- Have filled the array with Ball objects [15%]
- 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%]
- You have used good programming style and documentation [10%]
- You completed your lab partner evaluation [5%]