//-------------House-----------------

import java.awt.Graphics;

import java.awt.Color;

import java.awt.Polygon;

/**

   Describes a house at a location in 2D space

   @author Kathleen O'Brien

*/

public class House

{

   //other code may need these values

   private static final int DEFAULT_HOUSE_WIDTH = 50;

   private int houseWidth = DEFAULT_HOUSE_WIDTH;

   private int houseHeight = houseWidth;

   private int roofHeight = (houseHeight * 2) / 3;

 

   private int xBase; //x coordinate of lower left corner

   private int yBase; //y coordinate of lower left corner

   private Color houseColor;

   private Color windowColor;

 

   /**

      Constructs a House

      @param x the x coordinate of the lower-left corner

      @param y the y coordinate of the lower-left corner

      @param color the color of the house

   */

   public House(int x, int y, Color color)

   {

       xBase = x;

       yBase = y;

       houseColor = color;

       windowColor = Color.YELLOW;

   }

   /**

       Constructs a House

       @param x the x coordinate of the lower-left corner

       @param y the y coordinate of the lower-left corner

       @param color the color of the house

       @param width of the house

   */

   public House(int x, int y, Color color, int width)

   {

         this(x, y, color);

         houseWidth = width;

   }

 

   /**

      Draws the House on the specified graphics context

      @param g the graphics context on which to draw the House

   */

   public void draw(Graphics g)

   {

      //get x, y coordinate of top of house box

      int x = xBase;

      int y = yBase - houseHeight;

 

      //calculate x, y, width and height of various objects

      int doorWidth = houseWidth / 4;

      int doorHeight = houseHeight /2;

      int windowWidth = doorWidth;

      int windowHeight = windowWidth;

 

      int roofTipX = x + houseWidth / 2;

      int roofTipY = y - roofHeight;

      int doorX = x + houseWidth / 8;

      int doorY = y + houseHeight - doorHeight;

      int windowX = x + houseWidth - windowWidth - houseWidth / 8;

      int windowY = doorY;

 

      //define the roof

      int[] xValues = {x, roofTipX, x + houseWidth};

      int[] yValues = {y, roofTipY, y};

      Polygon roof = new Polygon(xValues, yValues, 3);

 

      //save the current color of this Graphics object

      Color saveColor = g.getColor();

 

      //draw house box

      g.setColor(houseColor);

      g.fillRect(x, y, houseWidth, houseHeight);

      g.setColor(Color.BLACK);

      g.drawRect(x, y, houseWidth, houseHeight);

 

      //draw roof

      g.setColor(Color.GRAY);

      g.fillPolygon(roof);

      g.setColor(Color.BLACK);

      g.drawPolygon(roof);

 

      //draw door

      g.drawRect(doorX, doorY, doorWidth, doorHeight);

 

      //draw window

      g.setColor(windowColor);

      g.fillRect(windowX, windowY, windowWidth, windowHeight);

      g.setColor(Color.BLACK);

      g.drawRect(windowX, windowY, windowWidth, windowHeight);

 

      //restore the Graphics object's original color.

      g.setColor(saveColor);

   }

 

   /**

      Sets the color for the window

      @param theColor the window color for this House

   */

   public void setWindowColor(Color theColor)

   {

      windowColor = theColor;

   }

 

   /**

      Gets the color of the window

      @return the color of the window in this House

   */

   public Color getWindowColor()

   {

      return windowColor;

   }

 

   /**

      Gets the total width of the House

      @return the width of the HOuse

   */

   public int getWidth()

   {

       return houseWidth;

   }

 

   /**

      Gets the total height of the house

      @return the total height of the house

   */

   public int getHeight()

   {

      return houseHeight + roofHeight;

   }

 

   /**

      Gets the lower left-hand x coordinate of this House

      @return the lower left-hand x coordinate

   */

   public int getX()

   {

      return xBase;

   }

 

   /**

      Gets the lower left-hand y coordinate of this House

      @return the lower left-hand y coordinate

   */

   public int getY()

   {

      return yBase;

   }

}//end House -----------------------------