public class CarShape implements MoveableShape
1: import java.awt.*;
2: import java.awt.geom.*;
3: import java.util.*;
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: }
23: public void translate(int dx, int dy)
24: {
25: x += dx;
26: y += dy;
27: }
29: public void draw(Graphics2D g2)
30: {
31: Rectangle2D.Double body
32: = new Rectangle2D.Double(x, y + width / 6,
33: width - 1, width / 6);
34: Ellipse2D.Double frontTire
35: = new Ellipse2D.Double(x + width / 6, y + width / 3,
36: width / 6, width / 6);
37: Ellipse2D.Double rearTire
38: = new Ellipse2D.Double(x + width * 2 / 3, y + width / 3,
39: width / 6, width / 6);
41: // The bottom of the front windshield
42: Point2D.Double r1
43: = new Point2D.Double(x + width / 6, y + width / 6);
44: // The front of the roof
45: Point2D.Double r2
46: = new Point2D.Double(x + width / 3, y);
47: // The rear of the roof
48: Point2D.Double r3
49: = new Point2D.Double(x + width * 2 / 3, y);
50: // The bottom of the rear windshield
51: Point2D.Double r4
52: = new Point2D.Double(x + width * 5 / 6, y + width / 6);
53: Line2D.Double frontWindshield
54: = new Line2D.Double(r1, r2);
55: Line2D.Double roofTop
56: = new Line2D.Double(r2, r3);
57: Line2D.Double rearWindshield
58: = new Line2D.Double(r3, r4);
59:
60: g2.draw(body);
61: g2.draw(frontTire);
62: g2.draw(rearTire);
63: g2.draw(frontWindshield);
64: g2.draw(roofTop);
65: g2.draw(rearWindshield);
66: }
67:
68: private int x;
69: private int y;
70: private int width;
71: }