Copyright © Cay S. Horstmann 2009 
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.
Car,
CarViewer, CarComponent from chapter 3.
Modify the program so that it can draw an arbitrary number of cars:
private ArrayList<Car>
cars in CarComponentpaintComponent method with a loop that draws
all cars in the array list CarComponent constructor, fill cars
with five cars with different locationsRun your program to check that it draws the cars. Attach the source code to your lab report.
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?
JPanel and then adding the
JPanel to the North. Implement this enhancement if you have
time.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?
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?
updatePositions to
CarComponent that moves each car. See code in lecture slides. Then change the
actionPerformed method to call
component.updatePositions();
Compile. What happens?
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.
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).)
t.start().
Right-click on the image and save it in the same directory as your code.
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?
c.move(MoveDirection.FORWARD, 1))
Run the program. What happens?
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.
Submit the code for your CarScene class with your lab
report.