CS46A Lab

Animations

Copyright © Cay S. Horstmann 2009 Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

In this lab, you will work with the members of your project team.

Put the answers to the questions in each step into the lab report. Copy/paste the programs that you write in the lab.

This icon indicates optional tasks. Do those if you have time.

Part A: Moving Cars

  1. Make a project with the classes Car, CarViewer, CarComponent from chapter 3. Modify the program so that it can draw an arbitrary number of cars:

    Run your program to check that it draws the cars. Attach the source code to your lab report.

  2. In the CarViewer class, add a JButton to the frame, like this:
    JButton button = new JButton("Move");
    frame.add(button, BorderLayout.NORTH);

    The JButton class is in the javax.swing package, and BorderLayout is in java.awt.

    Compile and run your program. Where is the button?

  3. The buttons in the book's sample program look prettier because they don't fill the entire area. That is achieved by putting the button into a JPanel and then adding the JPanel to the North. Implement this enhancement if you have time.
  4. Click the button. What happens?
  5. Of course, nothing happens—we didn't tell the button what to do when it is clicked. Add this class to the CarViewer class, inside the main method (!), just above the call to setVisible(true).
    class MoveListener implements ActionListener
    {
       public void actionPerformed(ActionEvent event)
       {
          System.out.println("move");
       }
    }

    Compile and run the program. What happens when you click the button?

  6. Of course, nothing happens—we didn't tell the button about the MoveListener. In the main method, call
    button.addActionListener(new MoveListener());

    after the definition of the MoveListener. Compile and run the program. What happens when you click the button?

  7. We don't want the program to tell us "move", we want it to move those cars. Add a method updatePositions to CarComponent that moves each car. See code in lecture slides. Then change the actionPerformed method to call
    component.updatePositions();

    Compile. What happens?

  8. That error message is rather unfortunate and the result of some poor decisions in the design of the Java language. To make it go away, simply add the keyword final to the declaration of the component variable.
    final CarComponent component = new CarComponent();

    Now compile and run your program. What happens when you click the button a few times?

    Attach the code for all classes to your lab report.

  9. It is a bit tedious to keep clicking that button. You can do better by using a javax.swing.Timer.

    Add a Timer like this:

    Timer t = new Timer(100, listener); // 100 milliseconds between actions
    t.start();

    Run your program. What happens?

    (To make the car movement prettier, change car.move(5, 0) to car.move(1, 0).)

  10. Change your program so that the timer only starts when the button is clicked. You will need two listeners, one for the button and one for the timer. In the button listener, call t.start().
  11. For a prettier display, you can use pre-made images instead of laboriously drawing everything with lines and circles. See the lecture slides for an example. Make your cars look like this:

    Right-click on the image and save it in the same directory as your code.

Part B: Moving Cars in Alice

  1. Unzip this file and open the project in Netbeans.

    Rename ArrayScene to CarScene. (Right-click on the class name and select Refactor -> Rename.)

    Change VisualArrayList to ArrayList (2x) in the CarScene class.

    Change cars.add(new Car(this, name)); to

    Car c = new Car(this, name);
    cars.add(c);
    c.setLocalPointOfView(new PointOfView(
       new Quaternion(0, 0, 0, 1),
       new Position(-i, 1, 0)));

    There will be a few unknown classes. For each of them, move the cursor just after the last character and hit Ctrl+Space, then accept the import that NetBeans suggests.

    Run the program. What happens?

  2. Now move all cars. (Hint: c.move(MoveDirection.FORWARD, 1))

    Run the program. What happens?

  3. Ok, but what if we want to move some of them together?

    Following the lecture slides, move the first three cars together.

    Hint: Use cars.get(i) to get at the ith car, and remember to use final when the compiler tells you to.

  4. Add a listener to each car so that it says its name when you click on it. Do this when you construct the cars.

    Submit the code for your CarScene class with your lab report.