001package horstmann.ch06_scene1; 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 void drawSelection(Graphics2D g2) 051 { 052 Rectangle2D.Double base 053 = new Rectangle2D.Double(x, y + width, width, width); 054 g2.fill(base); 055 } 056 057 public boolean contains(Point2D p) 058 { 059 return x <= p.getX() && p.getX() <= x + width 060 && y <= p.getY() && p.getY() <= y + 2 * width; 061 } 062 063 public void translate(int dx, int dy) 064 { 065 x += dx; 066 y += dy; 067 } 068 069 private int x; 070 private int y; 071 private int width; 072}