1  import java.awt.*;
  2  import java.awt.geom.*;
  3  import java.util.*;
  4  
  5  /**
  6     A car that can be moved around.
  7  */
  8  public class CarShape implements MoveableShape
  9  {
 10     /**
 11        Constructs a car item.
 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 CarShape(int x, int y, int width)
 17     {
 18        this.x = x;
 19        this.y = y;
 20        this.width = width;
 21     }
 22  
 23     public void move()
 24     {
 25        x++;
 26     }
 27  
 28     public void draw(Graphics2D g2)
 29     {
 30        Rectangle2D.Double body
 31              = new Rectangle2D.Double(x, y + width / 6, 
 32                    width - 1, width / 6);
 33        Ellipse2D.Double frontTire
 34              = new Ellipse2D.Double(x + width / 6, y + width / 3, 
 35                    width / 6, width / 6);
 36        Ellipse2D.Double rearTire
 37              = new Ellipse2D.Double(x + width * 2 / 3, y + width / 3,
 38                    width / 6, width / 6);
 39  
 40        // The bottom of the front windshield
 41        Point2D.Double r1
 42              = new Point2D.Double(x + width / 6, y + width / 6);
 43        // The front of the roof
 44        Point2D.Double r2
 45              = new Point2D.Double(x + width / 3, y);
 46        // The rear of the roof
 47        Point2D.Double r3
 48              = new Point2D.Double(x + width * 2 / 3, y);
 49        // The bottom of the rear windshield
 50        Point2D.Double r4
 51              = new Point2D.Double(x + width * 5 / 6, y + width / 6);
 52        Line2D.Double frontWindshield
 53              = new Line2D.Double(r1, r2);
 54        Line2D.Double roofTop
 55              = new Line2D.Double(r2, r3);
 56        Line2D.Double rearWindshield
 57              = new Line2D.Double(r3, r4);
 58        
 59        g2.draw(body);
 60        g2.draw(frontTire);
 61        g2.draw(rearTire);
 62        g2.draw(frontWindshield);
 63        g2.draw(roofTop);
 64        g2.draw(rearWindshield);
 65     }
 66     
 67     private int x;
 68     private int y;
 69     private int width;
 70  }