Part 1

For this part of the lab you will create a simple game shown below. You should experiment with this demonstration version to make sure you understand exactly what we have in mind.

You will need to define a class that is an extension of the FrameWindowController class. Keep in mind that any class you define must be saved in a file with the exactly same name, including the match upper and lower case letters.

Begin by drawing the balls in a 400x400 pixel window. The picture should look more or less like the one above.

Constants

When a certain constant value is used in more than one places, it makes sense to make up constants (private static final ...) for them. For example, the size of the balls is a fixed constant and is used in multiple places. We can create a constant variable for it as follows:

      private static final double BALL_SIZE = 50;
This will make it easier to change the size of the ball and also make your program easier to read. Constant names are by convention written with all capital letters and underscores, e.g. BALL_SIZE. But be aware that you can NOT have constants whose definition uses canvas.

Assigning a Random Color

After drawing the ball, the program needs to assign it a random color. Since we will also assign new random color to the ball when the they are bumped, this should be accomplished in a healper method such as:
private void setRandomColor( FilledOval ball) {

	...

}

Note that this is slightly different from the way we did it in the lectures, where a helper method is used to generate and return a random color. The goal is to present you with different ways of achieving the same goal. You are free to choose either way or come up with your own way. Whichever way you choose, it should follow the same guidelines, such as do no more work than necessary, avoid repeating code, etc.

A random color can be generated by randomly choosing integers between 0 and 255 for each of the red, blue, and green components of the color. We will use the RandomIntGenerator class in the objectdraw package to generate random numbers. Declare a generator to be an instance variable of type RandomIntGenerator. Create a new random number generator from class RandomIntGenerator, and assign it to generator:

    generator = new RandomIntGenerator(0,255);
Note that you should create only one RandomIntGenerator in your program, as an instance variable.

After this, a random color can be generated by

    new Color(generator.nextValue(), generator.nextValue(), generator.nextValue());
Note that the three calls of generator.nextValue() will generate different values for red, green and blue components of the color.

Dragging and Bumping Balls

Once you have written the code to set up the display, you need to write the code for dragging the balls. This code must check to see if the user is dragging any ball, and if so, which ball is being dragged. That means you need an if-statement. There are several ways to do the test. Here is a simple strategy: use two variables to record the dragged and the other ball. Let's call them draggedBall and otherBall. In the onMousePress method, you'll assign the ball that is being dragged (the one containing point) to draggedBall and assign the other ball to otherBall . Since both draggedBall and otherBall will be used in different methods, they must be instance variables. Their types must be FilledOval since they are referring to FilledOval objects. Write the code for declaring and assigning draggedBall and otherBall now.

When the user drags a ball, onMouseDrag must check to see if the dragged ball is bumping into the other ball, and if so, how to move the other ball. This is the part of the lab that requires some thinking. It is best to write this part of the code in a helper method. Give it a try!

Test your code!

Try out your code before proceeding to the next part of the lab. Does it behave in the same way as the demonstration above?

There's nothing to hand in for this part of the lab. But it would be wise to copy what you have so far to another file, so that you'll have something to hand in for partial credit, if you can't finish the next part.