01: import java.awt.*;
02: import java.awt.geom.*;
03: 
04: /**
05:    A house shape.
06: */
07: public class HouseShape extends SelectableShape
08: {
09:    /**
10:       Constructs a house 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 HouseShape(int x, int y, int width)
16:    {
17:       this.x = x;
18:       this.y = y;
19:       this.width = width;
20:    }
21: 
22:    public void draw(Graphics2D g2)
23:    {
24:       Rectangle2D.Double base 
25:          = new Rectangle2D.Double(x, y + width, width, width);
26: 
27:       // The left bottom of the roof
28:       Point2D.Double r1
29:          = new Point2D.Double(x, y + width);
30:       // The top of the roof
31:       Point2D.Double r2
32:          = new Point2D.Double(x + width / 2, y);
33:       // The right bottom of the roof
34:       Point2D.Double r3
35:          = new Point2D.Double(x + width, y + width);
36: 
37:       Line2D.Double roofLeft
38:          = new Line2D.Double(r1, r2);
39:       Line2D.Double roofRight
40:          = new Line2D.Double(r2, r3);
41: 
42:       g2.draw(base);
43:       g2.draw(roofLeft);
44:       g2.draw(roofRight);
45:    }
46:    
47:    public boolean contains(Point2D p)
48:    {
49:       return x <= p.getX() && p.getX() <= x + width 
50:          && y <= p.getY() && p.getY() <= y + 2 * width;
51:    }
52: 
53:    public void translate(int dx, int dy)
54:    {
55:       x += dx;
56:       y += dy;
57:    }
58: 
59:    private int x;
60:    private int y;
61:    private int width;
62: }