DrawWithRandomColors


Your browser is ignoring the <APPLET> tag!

This program draws a house when the program starts.

import objectdraw.*;

/**
 * Draws a house when the program starts.
 * Puts "Knock! Knock!" on the screeen where the user clicks the mouse.
 * 
 * @author Barbara Lerner
 * @version October 15, 2008
 */
public class DrawAHouse extends WindowController
{
    /**
     * Draw a house when the program starts.
     */
    public void begin () {
        new House(canvas);
    }
    
    /**
     * Type "Knock! Knock!" on the screen where the user clicks the mouse.
     */
    public void onMouseClick (Location point) {
        new Text ("Knock! Knock!", point, canvas);
    }
}

import objectdraw.*;
import java.awt.Color;

/**
 * Makes a simple drawing of a house.
 * 
 * @author Barbara Lerner 
 * @version October 15, 2008
 */
public class House
{
    // Where the top and left sides of the house wall go
    private static final int HOUSE_TOP = 150;
    private static final int HOUSE_LEFT = 50;
    
    // The dimensions of the house wall
    private static final int HOUSE_WIDTH = 150;
    private static final int HOUSE_HEIGHT = 150;
    
    // Dimensions of the door
    private static final int DOOR_WIDTH = HOUSE_WIDTH / 5;
    private static final int DOOR_HEIGHT = HOUSE_HEIGHT / 3;

    // Location of the door.  The door is centered horizontally and aligned on the bottom
    // with the house wall.
    private static final int DOOR_LEFT = HOUSE_LEFT + (HOUSE_WIDTH / 2) - (DOOR_WIDTH / 2);
    private static final int DOOR_TOP = HOUSE_TOP + HOUSE_HEIGHT - DOOR_HEIGHT;
    
    // Dimensions related to the roof
    private static final int ROOF_OVERHANG = 30;
    private static final int PEAK_HEIGHT = 50;
    
    // Corners of the roof
    private static final int ROOF_LEFT = HOUSE_LEFT - ROOF_OVERHANG;
    private static final int ROOF_RIGHT = HOUSE_LEFT + HOUSE_WIDTH + ROOF_OVERHANG;
    private static final int ROOF_CENTER = (ROOF_LEFT + ROOF_RIGHT) / 2;
    private static final int ROOF_PEAK = HOUSE_TOP - PEAK_HEIGHT;
    
    // Colors of the house
    private static final Color DOOR_COLOR = new Color (95, 83, 61);  // dark brown
    
    /**
     * Create a drawing of a house.
     * @param mysteryCanvas where the drawing should go
     */
    public House (DrawingCanvas mysteryCanvas) {
        // Generate a random color for the wall of the house.
        RandomIntGenerator colorGen = new RandomIntGenerator (0, 255);
        int redness = colorGen.nextValue();
        int greenness = colorGen.nextValue();
        int blueness = colorGen.nextValue();
        Color wallColor = new Color (redness, greenness, blueness);
        
        FilledRect wall = new FilledRect (HOUSE_LEFT, HOUSE_TOP, HOUSE_WIDTH, HOUSE_HEIGHT, mysteryCanvas);
        wall.setColor (wallColor);
        FilledRect door = new FilledRect (DOOR_LEFT, DOOR_TOP, DOOR_WIDTH, DOOR_HEIGHT, mysteryCanvas);
        door.setColor (DOOR_COLOR);
        new Line (ROOF_LEFT, HOUSE_TOP, ROOF_RIGHT, HOUSE_TOP, mysteryCanvas);
        new Line (ROOF_LEFT, HOUSE_TOP, ROOF_CENTER, ROOF_PEAK, mysteryCanvas);
        new Line (ROOF_RIGHT, HOUSE_TOP, ROOF_CENTER, ROOF_PEAK, mysteryCanvas);
    }
}