Hi,
I am working on an assignment that does the following...
When the program starts, it should look to see whether a file is present that contains a serialized array of vehicles. If so, it redisplays them. Otherwise, it generates random vehicles, writes them to a file, and displays them.
I have the program mostly working, but the vehicles aren't displaying. Any help getting the vehicles to display would be greatly appreciated. Here is my code. I am not allowed to change the VehicleComponent or Car Classes.
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
/**
This class represents a car.
*/
public class Car extends Vehicle
{
/**
Draw the car.
@param g2 the graphics context
*/
public void draw(Graphics2D g2)
{
Rectangle body = new Rectangle(getX(), getY() + CAR_HEIGHT / 3,
CAR_WIDTH, CAR_HEIGHT / 3);
Ellipse2D.Double frontTire = new Ellipse2D.Double(getX() + CAR_WIDTH / 6,
getY() + CAR_HEIGHT * 2 / 3, CAR_WIDTH / 6, CAR_WIDTH / 6);
Ellipse2D.Double rearTire = new Ellipse2D.Double(getX() + CAR_WIDTH * 4 / 6,
getY() + CAR_HEIGHT * 2 / 3, CAR_WIDTH / 6, CAR_WIDTH / 6);
Point2D.Double r1 = new Point2D.Double(getX() + CAR_WIDTH / 6,
getY() + CAR_HEIGHT / 3); // the bottom of the front windshield
Point2D.Double r2 = new Point2D.Double(getX() + CAR_WIDTH * 2 / 6, getY());
// the front of the roof
Point2D.Double r3 = new Point2D.Double(getX() + CAR_WIDTH * 4 / 6, getY());
// the rear of the roof
Point2D.Double r4 = new Point2D.Double(getX() + CAR_WIDTH * 5 / 6,
getY() + CAR_HEIGHT / 3); // the bottom of the rear windshield
Line2D.Double frontWindshield = new Line2D.Double(r1, r2);
Line2D.Double roofTop = new Line2D.Double(r2, r3);
Line2D.Double rearWindshield = new Line2D.Double(r3, r4);
g2.draw(body);
g2.draw(frontTire);
g2.draw(rearTire);
g2.draw(frontWindshield);
g2.draw(roofTop);
g2.draw(rearWindshield);
}
public int getHeight()
{
return CAR_HEIGHT;
}
public int getWidth()
{
return CAR_WIDTH;
}
private final int CAR_WIDTH = 60;
private final int CAR_HEIGHT = CAR_WIDTH / 2 ;
}
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
public class Truck extends Vehicle {
public void draw(Graphics2D g2) {
Rectangle body = new Rectangle(getX(), getY() + TRUCK_HEIGHT / 5,
TRUCK_WIDTH / 5, TRUCK_HEIGHT * 3 / 5);
Rectangle cargo = new Rectangle(getX() + TRUCK_WIDTH / 5, getY(),
TRUCK_WIDTH * 4 / 5, TRUCK_HEIGHT * 4 / 5);
int ybottom = getY() + TRUCK_HEIGHT * 4 / 5;
int tireDiameter = TRUCK_WIDTH / 10;
Ellipse2D.Double frontTire = new Ellipse2D.Double(getX(), ybottom, tireDiameter,
tireDiameter);
Ellipse2D.Double midTire1 = new Ellipse2D.Double(getX() + TRUCK_WIDTH * 1 / 5, ybottom,
tireDiameter, tireDiameter);
Ellipse2D.Double midTire2 = new Ellipse2D.Double(getX() + TRUCK_WIDTH * 3 / 10, ybottom,
tireDiameter, tireDiameter);
Ellipse2D.Double rearTire1 = new Ellipse2D.Double(getX() + TRUCK_WIDTH * 4 / 5, ybottom,
tireDiameter, tireDiameter);
Ellipse2D.Double rearTire2 = new Ellipse2D.Double(getX() + TRUCK_WIDTH * 9 / 10, ybottom,
tireDiameter, tireDiameter);
g2.draw(body);
g2.draw(cargo);
g2.draw(frontTire);
g2.draw(midTire1);
g2.draw(midTire2);
g2.draw(rearTire1);
g2.draw(rearTire2);
}
public int getHeight(){
return TRUCK_HEIGHT;
}
public int getWidth(){
return TRUCK_WIDTH;
}
private final int TRUCK_WIDTH = 100;
private final int TRUCK_HEIGHT = TRUCK_WIDTH / 2;
}
import java.awt.Graphics2D;
public abstract class Vehicle
{
public Vehicle()
{
xleft = 0;
ytop = 0;
}
public abstract void draw(Graphics2D g2);
public void setLocation(int x, int y)
{
xleft = x;
ytop = y;
}
public int getX()
{
return xleft;
}
public int getY()
{
return ytop;
}
public abstract int getHeight();
public abstract int getWidth();
private int xleft;
private int ytop;
}
import javax.swing.JComponent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
/**
This component draws a collection of vehicles.
*/
public class VehicleComponent extends JComponent{
public VehicleComponent()
{
vehicles = new ArrayList<Vehicle>();
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
for (Vehicle v : vehicles)
v.draw(g2);
}
public void add(Vehicle v)
{
vehicles.add(v);
repaint();
}
private ArrayList<Vehicle> vehicles;
}
import javax.swing.JComponent;
import javax.swing.JFrame;
public class RandomVehicleViewer extends JComponent {
public static void main(String[] args) {
JFrame frame = new JFrame();
final int FRAME_WIDTH = 600;
final int FRAME_HEIGHT = 600;
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent component = new VehicleComponent();
frame.add(component);
frame.setVisible(true);
}
}