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