      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

     33              = new Rectangle(xLeft, yTop + 10, 60, 10);

     34        Ellipse2D.Double frontTire

     35              = new Ellipse2D.Double(xLeft + 10, yTop + 20, 10, 10);

     36        Ellipse2D.Double rearTire

     37              = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10);

     38

     39        // The bottom of the front windshield

     40        Point2D.Double r1

     41              = new Point2D.Double(xLeft + 10, yTop + 10);

     42        // The front of the roof

     43        Point2D.Double r2

     44              = new Point2D.Double(xLeft + 20, yTop);

     45        // The rear of the roof

     46        Point2D.Double r3

     47              = new Point2D.Double(xLeft + 40, yTop);

     48        // The bottom of the rear windshield

     49        Point2D.Double r4

     50              = new Point2D.Double(xLeft + 50, yTop + 10);

     51

     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  }