1  import java.awt.Graphics2D;
  2  import java.awt.Rectangle;
  3  import java.awt.geom.Ellipse2D;
  4  import java.awt.geom.Line2D;
  5  import java.awt.geom.Point2D;
  6  
  7  /**
  8     A car shape that can be positioned anywhere on the screen.
  9  */
 10  public class Car
 11  {
 12     private int xLeft;
 13     private int yTop;
 14  
 15     /**
 16        Constructs a car with a given top left corner.
 17        @param x the x coordinate of the top left corner
 18        @param y the y coordinate of the top left corner
 19     */
 20     public Car(int x, int y)
 21     {
 22        xLeft = x;
 23        yTop = y;
 24     }
 25  
 26     /**
 27        Draws the car.
 28        @param g2 the graphics context
 29     */
 30     public void draw(Graphics2D g2)
 31     {
 32        Rectangle body = new Rectangle(xLeft, yTop + 10, 60, 10);      
 33        Ellipse2D.Double frontTire 
 34           = new Ellipse2D.Double(xLeft + 10, yTop + 20, 10, 10);
 35        Ellipse2D.Double rearTire 
 36           = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10);
 37  
 38        // The bottom of the front windshield
 39        Point2D.Double r1 = new Point2D.Double(xLeft + 10, yTop + 10);
 40        // The front of the roof
 41        Point2D.Double r2 = new Point2D.Double(xLeft + 20, yTop);
 42        // The rear of the roof
 43        Point2D.Double r3 = new Point2D.Double(xLeft + 40, yTop);
 44        // The bottom of the rear windshield
 45        Point2D.Double r4 = new Point2D.Double(xLeft + 50, yTop + 10);
 46  
 47        Line2D.Double frontWindshield = new Line2D.Double(r1, r2);
 48        Line2D.Double roofTop = new Line2D.Double(r2, r3);
 49        Line2D.Double rearWindshield = new Line2D.Double(r3, r4);
 50     
 51        g2.draw(body);
 52        g2.draw(frontTire);
 53        g2.draw(rearTire);
 54        g2.draw(frontWindshield);      
 55        g2.draw(roofTop);      
 56        g2.draw(rearWindshield);      
 57     }
 58  }