Computer Science 120
Introduction to Programming

Spring 2012, Siena College

House Demo

A working demo of House will appear below. Click inside the applet to interact with it.



House BlueJ Project

Click here to download a BlueJ project for House.


House Source Code

The Java source code for House is below. Click on a file name to download it.


HouseWindow.java

import objectdraw.*;

/**

 * WindowController for a House Application.  When you click on the screen, you

 * create a house with a roof and door.  If you click on a door, it (opens/closes).

 * 

 * @author Darren Lim

 * @version 1.0

 */

public class HouseWindow extends WindowController

{

    House houseReference;



    /**

     * begin overriden method for our HouseWindow application.  Initializes our house reference

     * to null

     */

    public void begin()

    {

        houseReference = null;

    }



    /**

     * onMouseClick overridden method for our HouseWindow application.  Checks whether we

     * should create a new house if we click on a non-door location on the screen.

     * 

     * @param point  Location of mouse click

     */

    public void onMouseClick(Location point)

    {

       

        if (houseReference == null)

        {

            //First House to be drawn

            houseReference = new House(point, canvas);

        }

        else if (houseReference.onDoorClick(point))

        {

            //Check to see if the active house's door has been clicked on

            houseReference.changeDoor();

        }

        else

        {

            houseReference.removeFromCanvas();

            houseReference = new House(point, canvas);

        }



    }

}


House.java

import objectdraw.*;

import java.awt.*;



/**

 * Draws a Wire-frame house on the screen.

 * 

 * @author Darren Lim 

 * @version 1.0

 */

public class House

{

    // instance variables - replace the example below with your own

    private Line leftRoofEdge, rightRoofEdge, topOfHouse;

    private FramedRect bodyOfHouse, closedDoor;

    private FramedOval doorKnob;

    private FilledRect openDoor;

    private DrawingCanvas canvas;



    /**

     * Constructor for objects of class House.  The point is assumed to be the top

     * of the house (roof point).  The house is 50 x 50 with the roof 30 pixels high.

     * The roof extends from the body of the house by 10 pixels.  An open door is a FilledRect,

     * and a closed door is a FramedRect with a doorknob

     * 

     * @param point Location of the top point of the roof

     * @param acanvas Drawing canvas for our house 

     */

    public House(Location point, DrawingCanvas acanvas)

    {

        canvas = acanvas;

        //Coordinates of the top of the roof

        double x = point.getX(), y = point.getY();

        //Draw Roof

        leftRoofEdge = new Line(x, y, x - 30, y + 30, canvas);

        rightRoofEdge = new Line(x, y, x + 30, y + 30, canvas);

        topOfHouse = new Line(x + 30, y + 30, x - 30, y + 30, canvas);

        bodyOfHouse = new FramedRect(x - 25, y + 30, 50, 50, canvas);

        closedDoor = new FramedRect(x - 10, y + 50, 20, 30, canvas);

        doorKnob = new FramedOval(x - 8, y + 50 + 15 - 2, 4, 4, canvas);

        openDoor = new FilledRect(x - 10, y + 50, 20, 30, canvas);

        openDoor.setColor(Color.BLACK);

        openDoor.hide();

        

        

    }

    

    /**

     * Returns true if the point parameter is contained by the open door.

     *

     * @param  point Location of a click

     * @return     whether the door was clicked on or not

     */

    public boolean onDoorClick(Location point)

    {

        if (openDoor.contains(point))

            return true;

        return false;

    }

    

    /**

     * Removes all objects from the canvas

     *

     */

    public void removeFromCanvas()

    {

        leftRoofEdge.removeFromCanvas();

        rightRoofEdge.removeFromCanvas();

        topOfHouse.removeFromCanvas();

        bodyOfHouse.removeFromCanvas();

        openDoor.removeFromCanvas();

        doorKnob.removeFromCanvas();

        closedDoor.removeFromCanvas();

        

    }

    

    /**

     * Reverts between an open door (black rectangle) and a closed door (framed rect with

     * a door knob)

     *

     */

    public void changeDoor()

    {

        if (openDoor.isHidden())

        {

            openDoor.show();

            doorKnob.hide();

            closedDoor.hide();            

        }

        else

        {

            openDoor.hide();

            doorKnob.show();

            closedDoor.show();

        }

    }



}