001package horstmann.ch06_scene2; 002import java.awt.Graphics2D; 003import java.awt.geom.Line2D; 004import java.awt.geom.Point2D; 005import java.awt.geom.Rectangle2D; 006 007/** 008 A house shape. 009 */ 010public class HouseShape extends SelectableShape 011{ 012 /** 013 Constructs a house shape. 014 @param x the left of the bounding rectangle 015 @param y the top of the bounding rectangle 016 @param width the width of the bounding rectangle 017 */ 018 public HouseShape(int x, int y, int width) 019 { 020 this.x = x; 021 this.y = y; 022 this.width = width; 023 } 024 025 public void draw(Graphics2D g2) 026 { 027 Rectangle2D.Double base 028 = new Rectangle2D.Double(x, y + width, width, width); 029 030 // The left bottom of the roof 031 Point2D.Double r1 032 = new Point2D.Double(x, y + width); 033 // The top of the roof 034 Point2D.Double r2 035 = new Point2D.Double(x + width / 2, y); 036 // The right bottom of the roof 037 Point2D.Double r3 038 = new Point2D.Double(x + width, y + width); 039 040 Line2D.Double roofLeft 041 = new Line2D.Double(r1, r2); 042 Line2D.Double roofRight 043 = new Line2D.Double(r2, r3); 044 045 g2.draw(base); 046 g2.draw(roofLeft); 047 g2.draw(roofRight); 048 } 049 050 public boolean contains(Point2D p) 051 { 052 return x <= p.getX() && p.getX() <= x + width 053 && y <= p.getY() && p.getY() <= y + 2 * width; 054 } 055 056 public void translate(int dx, int dy) 057 { 058 x += dx; 059 y += dy; 060 } 061 062 private int x; 063 private int y; 064 private int width; 065}