public class CarShape extends SelectableShape
1: import java.awt.*;
2: import java.awt.geom.*;
4: /**
5: A car shape.
6: */
7: public class CarShape extends SelectableShape
8: {
9: /**
10: Constructs a car shape.
11: @param x the left of the bounding rectangle
12: @param y the top of the bounding rectangle
13: @param width the width of the bounding rectangle
14: */
15: public CarShape(int x, int y, int width)
16: {
17: this.x = x;
18: this.y = y;
19: this.width = width;
20: }
22: public void draw(Graphics2D g2)
23: {
24: Rectangle2D.Double body
25: = new Rectangle2D.Double(x, y + width / 6,
26: width - 1, width / 6);
27: Ellipse2D.Double frontTire
28: = new Ellipse2D.Double(x + width / 6, y + width / 3,
29: width / 6, width / 6);
30: Ellipse2D.Double rearTire
31: = new Ellipse2D.Double(x + width * 2 / 3,
32: y + width / 3,
33: width / 6, width / 6);
35: // The bottom of the front windshield
36: Point2D.Double r1
37: = new Point2D.Double(x + width / 6, y + width / 6);
38: // The front of the roof
39: Point2D.Double r2
40: = new Point2D.Double(x + width / 3, y);
41: // The rear of the roof
42: Point2D.Double r3
43: = new Point2D.Double(x + width * 2 / 3, y);
44: // The bottom of the rear windshield
45: Point2D.Double r4
46: = new Point2D.Double(x + width * 5 / 6, y + width / 6);
47: Line2D.Double frontWindshield
48: = new Line2D.Double(r1, r2);
49: Line2D.Double roofTop
50: = new Line2D.Double(r2, r3);
51: Line2D.Double rearWindshield
52: = new Line2D.Double(r3, r4);
54: g2.draw(body);
55: g2.draw(frontTire);
56: g2.draw(rearTire);
57: g2.draw(frontWindshield);
58: g2.draw(roofTop);
59: g2.draw(rearWindshield);
60: }
61:
62: public void drawSelection(Graphics2D g2)
63: {
64: Rectangle2D.Double body
65: = new Rectangle2D.Double(x, y + width / 6,
66: width - 1, width / 6);
67: g2.fill(body);
68: }
70: public boolean contains(Point2D p)
71: {
72: return x <= p.getX() && p.getX() <= x + width
73: && y <= p.getY() && p.getY() <= y + width / 2;
74: }
76: public void translate(int dx, int dy)
77: {
78: x += dx;
79: y += dy;
80: }
82: private int x;
83: private int y;
84: private int width;
85: }