Source of Car.java


  1: import java.awt.*;
  2: import java.awt.geom.*;
  3: import java.io.*;

  5: /**
  6:    A serializable car shape.
  7: */
  8: public class Car implements Serializable
  9: {
 10:    /**
 11:       Constructs a car.
 12:       @param x the left of the bounding rectangle
 13:       @param y the top of the bounding rectangle
 14:       @param width the width of the bounding rectangle
 15:    */
 16:    public Car(int x, int y, int width)
 17:    {
 18:       body = new Rectangle(x, y + width / 6,
 19:          width - 1, width / 6);
 20:       roof = new Rectangle(x + width / 3, y,
 21:          width / 3, width / 6);
 22:       frontTire = new Ellipse2D.Double(x + width / 6, y + width / 3,
 23:          width / 6, width / 6);
 24:       rearTire = new Ellipse2D.Double(x + width * 2 / 3, y + width / 3,
 25:          width / 6, width / 6);
 26:    }

 28:    private void writeObject(ObjectOutputStream out)
 29:       throws IOException
 30:    {
 31:       out.defaultWriteObject();
 32:       writeRectangularShape(out, frontTire);
 33:       writeRectangularShape(out, rearTire);
 34:    }

 36:    /**
 37:       A helper method to write a rectangular shape.
 38:       @param out the stream onto which to write the shape
 39:       @param s the shape to write
 40:    */
 41:    private static void writeRectangularShape(ObjectOutputStream out,
 42:       RectangularShape s)
 43:       throws IOException
 44:    {
 45:       out.writeDouble(s.getX());
 46:       out.writeDouble(s.getY());
 47:       out.writeDouble(s.getWidth());
 48:       out.writeDouble(s.getHeight());
 49:    }

 51:    private void readObject(ObjectInputStream in)
 52:       throws IOException, ClassNotFoundException
 53:    {
 54:       in.defaultReadObject();
 55:       frontTire = new Ellipse2D.Double();
 56:       readRectangularShape(in, frontTire);
 57:       rearTire = new Ellipse2D.Double();
 58:       readRectangularShape(in, rearTire);
 59:    }

 61:    /**
 62:       A helper method to read a rectangular shape.
 63:       @param in the stream from which to read the shape
 64:       @param s the shape to read. The method sets the frame
 65:       of this rectangular shape.
 66:    */
 67:    private static void readRectangularShape(ObjectInputStream in,
 68:       RectangularShape s)
 69:       throws IOException
 70:    {
 71:       double x = in.readDouble();
 72:       double y = in.readDouble();
 73:       double width = in.readDouble();
 74:       double height = in.readDouble();
 75:       s.setFrame(x, y, width, height);
 76:    }

 78:    /**
 79:       Draws the car.
 80:       @param g2 the graphics context
 81:    */
 82:    public void draw(Graphics2D g2)
 83:    {
 84:       g2.draw(body);
 85:       g2.draw(roof);
 86:       g2.draw(frontTire);
 87:       g2.draw(rearTire);
 88:    }

 90:    public String toString()
 91:    {
 92:       return getClass().getName()
 93:          + "[body=" + body
 94:          + ",roof=" + roof
 95:          + ",frontTire=" + formatRectangularShape(frontTire)
 96:          + ",rearTire=" + formatRectangularShape(rearTire)
 97:          + "]";
 98:    }

100:    /**
101:       A helper method to format a rectangular shape.
102:       @param s the shape to format
103:       @return a formatted representation of the given shape
104:    */
105:    private static String formatRectangularShape(RectangularShape s)
106:    {
107:       return RectangularShape.class.getName()
108:          + "[x=" + s.getX()
109:          + ",y=" + s.getY()
110:          + ",width=" + s.getWidth()
111:          + ",height=" + s.getHeight()
112:          + "]";
113:    }


116:    private Rectangle body;
117:    private Rectangle roof;
118:    private transient Ellipse2D.Double frontTire;
119:    private transient Ellipse2D.Double rearTire;
120: }