The original definition of the move() method in the Ball class, as seen on page 1 of the lecture 29 slides, is as follows:
public void move() { x += xSpeed; y += ySpeed; }However, which this definition, the Ball object moves in a straight line, even through the boundary that is drawn near the edges of the window. In order to have the Ball object rebound correctly of this boundary, we modified the move() method by passing the position of the boundary rectangle to it as a parameter, and updating the xSpeed or ySpeed values if the ball moved too far to the left or right, or too far up or down:
public void move(Rectangle b) { x += xSpeed; y += ySpeed; if ((x - 20 < b.x) || (x + 20 > b.x + b.width)) { xSpeed = -xSpeed; } if ((y - 20 < b.y) || (y + 20 > b.y + b.height)) { ySpeed = -ySpeed; } }Once we had updated this, we then modified the JPanel class to store a collection of Ball objects in an array:
private Ball[] balls;rather than just a single Ball object. This meant that statements such as:
ball.move(boundary);now became:
for (int i = 0; i < balls.length; i++) { balls[i].move(boundary); }