    01: import java.awt.*;
    02: import java.awt.geom.*;
    03:
    04: /**
    05:    A car shape.
    06: */
    07: public class CarShape
    08: {
    09:    /**
    10:       Constructs a car shape.
    11:       @param x the left of the bounding rectangle
    12:       @param y the top of the bounding rectangle
    13:       @param width the width of the bounding rectangle
    14:    */
    15:    public CarShape(int x, int y, int width)
    16:    {
    17:       this.x = x;
    18:       this.y = y;
    19:       this.width = width;
    20:    }
    21:
    22:    public void draw(Graphics2D g2)
    23:    {
    24:       Rectangle2D.Double body
    25:          = new Rectangle2D.Double(x, y + width / 6,
    26:             width - 1, width / 6);
    27:       Ellipse2D.Double frontTire
    28:          = new Ellipse2D.Double(x + width / 6, y + width / 3,
    29:             width / 6, width / 6);
    30:       Ellipse2D.Double rearTire
    31:          = new Ellipse2D.Double(x + width * 2 / 3,
    32:             y + width / 3,
    33:             width / 6, width / 6);
    34:
    35:       // the bottom of the front windshield
    36:       Point2D.Double r1
    37:          = new Point2D.Double(x + width / 6, y + width / 6);
    38:       // the front of the roof
    39:       Point2D.Double r2
    40:          = new Point2D.Double(x + width / 3, y);
    41:       // the rear of the roof
    42:       Point2D.Double r3
    43:          = new Point2D.Double(x + width * 2 / 3, y);
    44:       // the bottom of the rear windshield
    45:       Point2D.Double r4
    46:          = new Point2D.Double(x + width * 5 / 6, y + width / 6);
    47:       Line2D.Double frontWindshield
    48:          = new Line2D.Double(r1, r2);
    49:       Line2D.Double roofTop
    50:          = new Line2D.Double(r2, r3);
    51:       Line2D.Double rearWindshield
    52:          = new Line2D.Double(r3, r4);
    53:
    54:       g2.draw(body);
    55:       g2.draw(frontTire);
    56:       g2.draw(rearTire);
    57:       g2.draw(frontWindshield);
    58:       g2.draw(roofTop);
    59:       g2.draw(rearWindshield);
    60:    }
    61:
    62:    public boolean contains(Point2D p)
    63:    {
    64:       return x <= p.getX() && p.getX() <= x + width
    65:          && y <= p.getY() && p.getY() <= y + width / 2;
    66:    }
    67:
    68:    public void translate(double dx, double dy)
    69:    {
    70:       x += dx;
    71:       y += dy;
    72:    }
    73:
    74:    private int x;
    75:    private int y;
    76:    private int width;
    77: }