Overview | Schedule | Announcements | Resources | Assignments | Home |
Create | Create a Driver object and invoke the start method. Observe the results. |
Read | Open the editor for the Driver class. To understand the initial triangle and circle display, read Driver's field definitions and the class constructor. Next verify what happened when you invoked the start method by studying the statements in that method. |
Add | Add the following actions to the start method,
each of which can be modeled after the first loop in that method:
|
Write |
Create a new method, bounceBall, that moves the ball to the "floor" of the window and then moves it back up to its original height. The signature of bounceBall is: public void bounceBall() To do this you will need to write two consecutive while loops. The first loop will move the ball down and to the right until it reaches the lower window boundary. Use an xDistance of 1 and a yDistance of 3. The second loop will move the ball up and to the right until it reaches its original height or the right side of the window. Use an xDistance of 1 and a yDistance of -3. Test bounceBall before going to the next step. |
Create | Create a Driver object and invoke the start method. Click anywhere in the graphics window to make the window active ("have the focus"). Then press the 'h' key on the keyboard several times. Then press the 's' key once to terminate the simulation. |
Read | Open the editor for the Driver class. Read the Driver field definitions and the class constructor. Next, verify what happened when you invoked the start method by studying the statements in method start and the method keyTyped(KeyEvent ke) found near the bottom of Driver code. |
Add |
Add an "if" statement to the keyTyped(KeyEvent ke) method that will cause the rocket to move vertically to the top of the screen when the 'v' key is typed. Note that the necessary code in the start method is already in place and the boolean vMove is defined and initialized to false. Test before moving to the next step. |
Add |
Note that each time the start method is invoked, the ball changes color from blue to red. Write the code necessary to have the color change to red only when the 'r' key is typed. Note, you should define a boolean field (cColor, for example) and modify the constructor, the start method, and the keyTyped(KeyEvent ke) method. Test your code. If you finish early and want more practice, try adding the bounceBall method to the while loop in start so that it is executed only if the 'b' key is typed. |
Homework:
Overview | Schedule | Announcements | Resources | Assignments | Home |
DePauw University, Computer Science
Department, Spring 2005
Maintained by Brian
Howard (bhoward@depauw.edu
).
Last updated