1  import java.awt.*;
  2  import java.awt.geom.*;
  3  
  4  /**
  5     A house shape.
  6  */
  7  public class HouseShape extends SelectableShape
  8  {
  9     /**
 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 void drawSelection(Graphics2D g2)
 48     {
 49        Rectangle2D.Double base 
 50           = new Rectangle2D.Double(x, y + width, width, width);
 51        g2.fill(base);
 52     }
 53  
 54     public boolean contains(Point2D p)
 55     {
 56        return x <= p.getX() && p.getX() <= x + width 
 57           && y <= p.getY() && p.getY() <= y + 2 * width;
 58     }
 59  
 60     public void translate(int dx, int dy)
 61     {
 62        x += dx;
 63        y += dy;
 64     }
 65  
 66     private int x;
 67     private int y;
 68     private int width;
 69  }