Overview Schedule Announcements Resources Assignments Home

CSC 121: Computer Science I, Spring 2005

Lab 3

Laboratory 3 - Animation and Project 1

Goals:
To understand how while loops can be used to control the behavior of graphics objects; To experiment with the interaction of graphics objects that are controlled by a "driver class"; To learn about keyboard key events; To gain experience using the BlueJ debugger.
Getting started:
The Steps of the Lab:
  1. Open the shapes-1 project
    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:
    1. Have the rocket (triangle) move vertically to the top of the window. You should use a while loop. You will need to compare the yPosition of the rocket with the yCoordinate of the top of the window to know when to exit the while loop. Use the moveDirection method with xDistance = 0 and yDistance = -3. Test this before going to the next step.
    2. Have the ball (circle) move from its current position to the lower-right corner of the window on a 45 degree angle. Use moveDirection with xDistance and yDistance equal to 3. The ball should not go completely out of the window. You can use this information and myCanvas.getWidth(), myCanvas.getHeight() in the while loop condition. Test this before going to the next step.
    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.

  2. Close the shapes-1 project and open the shapes-2 project
    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:

  3. Work through the exercises in sections 3.12 and 3.13 of your textbook. We will discuss the material about debugging in class.
Overview Schedule Announcements Resources Assignments Home

Valid HTML 4.01!Valid CSS!DePauw University, Computer Science Department, Spring 2005
Maintained by Brian Howard (bhoward@depauw.edu). Last updated