CSC 224: Program 6

Due: Tenth Week

Objective of this Program: To learn about using threads in Java, and to get some experience with animation in GUI's.

The Program Overview

This program is an extension of the example of the program which had a thread controlling the movement of a ball. In the example we have developed in class, we have a ball, which is created by FilledOval.java. Each time we create a ball, we send it a number, which will appear inside the ball. (Actually, this works for the first 10 number, after which it puts a letter inside the ball.) In class we developed a program which creates a GUI interface that has a thread controlling the ball.

This assignment is to create separate threads (up to 10 of them), each of which will control the movement of a separte ball. When each of the balls gets to an edge of the GUI, it will bounce off. Furthermore, if a ball meets the button in the GUI, it will also bounce off the button. The bouncing off of the button will be the hardest part of the assignment.

Extending ThreadMoveInterface

To extend the class to handle several balls, you will need an extra variable, maxNumber (which in this example will be 10), which will be the upper limit of the number of independent threads. You should assign the value of maxNumber in the constructor.

We have to change the variables which control the ball to be arrays:

  1. x, the x-coordinate of the ball
  2. y, the y-coordinate of the ball
  3. dx, the amount of x-movement of the ball
  4. dy, the amount of y-movement of the ball
  5. direction_x, the x-direction of the ball, positive to the right, negative to the left
  6. direction_y, the y-direction of the ball, positive down, negative up
  7. newOval, the actual ball, an instance of FilledOval

Each of the above, must be replaced by an array. It should be declared as an array as a member, e.g.,

int [] x;

It should be created as an array in the constructor

x = new int[maxNumber];

Inside the actionPerformed method, the variable threadNumber should be incremented, and used as the index to create an instance of each member of the arrays:

newOval[threadNumber] = new FilledOval(x[threadNumber],y[threadNumber],20,20, threadNumber);

This is done for each of the lines in the method which has a reference to one of the 5 variables above.

Note: Remember to make sure that the new thread and ball is created only when threadNumber < maxNumber, else you will get either a NullPointerException or an ArrayIndexOutOfBounds exception.

To be safe, you should catch the exceptions inside the method actionPerformed with a try....catch construct.

Note: You do not need an array of threads. Just the one Thread t declared as a member of the class will suffice. Each is given via a

t = new Thread(this);

as given in the example.

The method run

You must modify the method to make sure each ball is controlled by the independent thread. The most important thing is to have the first line of the method stating

int j = threadNumber;

This is because after the thread is created corresponding to newOval[threadNumber], it calls the start method. This method will immediately call the run method, so the j will correspond to the appropriate thread.

Then in the method, replace all references to x, y, direction_x, and direction_y, to the array index of value j:

x becomes x[j]

etc.

Note:Since sleep is a static method, you do not need to change the code for it. It remains Thread.sleep(100).

Modifications to be Done

Rather than having all of the balls being red, you should choose the colors at random. You do this by having variables

int red,green,blue;

and randoming assigning them the values of 0 to 255 inclusive, i.e., one has 0<= red <= 255. You can do this with either Math.random or use the Random class. See the documentation on api of Java for details on how to use them.

You assign a color by having

Color c;
c = new Color(red,green,blue);
and using c instead of Color.red in setting of the color of the FilledOval via the setForeground(c), as done in the example. Note that although the probability is very small, it is possible to have the color chosen to be the same color as the background color as the GUI. If this is the case, you will just see the number associated to the ball moving.

Furthermore, rather than having all of the balls starting with the value of direction_x = direction_y = 1, so that it starts out going down at a 45 degree angle, you should choose the values of direction_x[threadNumber] and direction_y[threadNumber] to be +1 or -1 equally likely. This will give you 4 possible directions it starts out. One does this using Math.random to choose them.

Another modification is changing where the balls should start. Rather than having them all starting at the same place, you should choose a different start place for each of the balls. Again you should use Math.random to choose a start place. One chooses the start place by choosing a different value for the x[threadNumber] and y[threadNumber]. You should make sure it doesn't s start inside the button.

You should also modify the amount of movement of each ball so that they all are different. It could be done by a random number or by a pre-chosen array of values.

The most important modification is to have the balls bounce off the button in the GUI. As it is now, the balls just pass behind the button (as if it were not there, or if the button was above the plane they are moving in). You should have the ball bounce off it as it bounces off the edge. If a ball meets the bottom of the button, it should bounce down (y increases), while it should bounce up if it meets the top of the button. Similarly, if it meets the sides of the button the direction_x should change.

Hint: Find the the coordinates of the area occupied by the button. Then if a ball enters this area, have it react as it does when it meets a wall.

Extra Credit

Extend it so that if two balls meet each other, they bounce off each other as they bounce off the wall. That is to say, each of the balls should move in the "opposite" direction after they meet. Be sure to make it so that they obey the usual laws of Newtonian physics.

What to Hand-in

You should post your project as a 'zip' file on the COL website. It should be posted under the appropriate assignment. Also indicate how many hours you worked on the program in the Readme.txt file.


Homepage of CTI School back to 211 homepage