I have th following sources code for a class called MultiThreadedWorld. I need to modifiy it so that when the mouse is clicked, the balls are restarted from the top left hand corrner of the window. I suppose this means modifying the line: ballArray.moveTo(e.getX(), e.getY());
Also, I need to modify this code so that when the mouse is clicked, the speed of the balls doubles from 100 to 50 as in the line: Thread.sleep(100);
Below is the source code for the class MultiThreadedWorld:
//A multi-bouncing ball animation controlled by threads with
//count of number of hits against boundary
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MultiThreadedBallWorld extends JFrame
{
public static void main (String args[])
{
MultiThreadedBallWorld world = new MultiThreadedBallWorld(Color.red);
world.show();
}
public static final int FrameWidth = 600;
public static final int FrameHeight = 400;
//array to hold balls
private Ball [ ] ballArray;
private static final int NoOfBalls = 10;
//array to hold references to threads
private Thread [ ] ballThread;
//a variable to hold count of hits
private int hits;
//a label to display number of hits
private Label hitsLabel;
private MultiThreadedBallWorld(Color ballColor) {
setSize(FrameWidth, FrameHeight);
setTitle("Multi Threaded Ball World");
//add listener for mouse clicks to frame
addMouseListener(new BallListener());
//create arrays for balls and threads
ballArray = new Ball[NoOfBalls];
ballThread = new Thread[NoOfBalls];
//create balls and threads and add to arrays
for (int i = 0; i < NoOfBalls; i++) {
ballArray = new Ball(new Point(100, 15), 5);
ballArray.setColor(ballColor);
ballArray.setMotion(3.0+i, 6.0-i);
// Create a new Thread to look after the ball referenced
// by ballArray
ballThread = new Thread(new BallThread(ballArray));
ballThread.start();
}
}
private class BallThread implements Runnable {
//the action taken by the thread that controls the ball, run
//overrides run in Thread.
Ball aBall;
public BallThread (Ball b) { aBall = b; }
public void run() {
while (true) {
aBall.move();
//check whether ball intersects boundary and,
//if so, change direction of motion
Point pos = aBall.location();
if ((pos.x < aBall.radius()) ||
(pos.x > FrameWidth - aBall.radius()))
aBall.reflectVert();
if ((pos.y < aBall.radius()) ||
(pos.y > FrameHeight - aBall.radius()))
aBall.reflectHorz();
repaint();
// pause the execution of thread so that a mouse event