Prev Up Next
Go backward to Variable Declarations
Go up to Top
Go forward to Drawing the Ball

Setting the Ball's Initial Position

The animate method will depend on the variables you just declared to decide where to draw the ball at each step and how much to move it between steps. Therefore, before "animate" is invoked by the browser for the first time, you must tell the computer what values to associate with each variable name. The browser always invokes "begin" before any other method in your applet, so this is a good place to make these assignments.

In the "begin" method's body, add assignment statements to make the ball's original y component a small number (like 5), and its initial x component something between 0 and 200 (the width of the applet's default screen area). Also, you will need to assign values to "xspeed" and "yspeed" to specify the rate at which the x and y coordinates should change. Make these values relatively small (in the 2 to 4 range) and make the change per step in the y coordinate a bit bigger than the change in the x coordinate.


Prev Up Next