This is a bonus program! It's meant to be challenging.

Write a program that animates two different balls bouncing around the canvas. If a ball hits off of a wall, it should bounce back in the opposite direciton. The two balls can also bounce off of each other. An example is shown in the below gif.

In this run, the width of each ball is 50 pixels, but feel free to use whichever numbers you would like. Note that when creating the balls it is important to give the balls different, random velocities and starting X,Y positions. Refer to the random generator reference linked above for help generating random values.

When computer scientists tackle a large problem, it helps to break it down into smaller steps. One way to break this problem down would be:

  • To start, add two balls to the screen with different, random positions.
  • Get the balls to move with random velocities.
  • Get the balls to start bouncing off the walls correctly. Remember that you can get the X position of a GOval ball by calling ball.getX() (same thing goes for the Y position).
  • Have the balls bounce off of each other. For this part, the getElementAt method, shown below, will be useful.