Source of HouseShape.java


  1: import java.awt.*;
  2: import java.awt.geom.*;

  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:    }

 22:    public void draw(Graphics2D g2)
 23:    {
 24:       Rectangle2D.Double base 
 25:          = new Rectangle2D.Double(x, y + width, width, width);

 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);

 37:       Line2D.Double roofLeft
 38:          = new Line2D.Double(r1, r2);
 39:       Line2D.Double roofRight
 40:          = new Line2D.Double(r2, r3);

 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:    }

 53:    public void translate(int dx, int dy)
 54:    {
 55:       x += dx;
 56:       y += dy;
 57:    }

 59:    private int x;
 60:    private int y;
 61:    private int width;
 62: }