previous | start | next

File Car.java

1 import java.awt.Graphics2D;
2 import java.awt.geom.Ellipse2D;
3 import java.awt.geom.Line2D;
4 import java.awt.geom.Point2D;
5 import java.awt.geom.Rectangle2D;
6
7 /**
8    A car shape that can be positioned anywhere on the screen.
9 */
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(double x, double 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       Rectangle2D.Double body 
30          = new Rectangle2D.Double(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    private double xLeft;
65    private double yTop;
66 }


previous | start | next