StickFigure


Your browser is ignoring the <APPLET> tag!

This program draws a stick figure when the program starts. Whenever the user depresses the mouse, the word "Hi" appears where the mouse is.

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

/**
 * Draws a stick figure on the display.  When the user depresses the mouse
 * button, the stick figure says "hi"
 * 
 * @author Barbara Lerner
 * @version March 3, 2008
 */
public class DrawAStickFigure extends WindowController
{
    /**
     * Draw a stick figure
     */
    public void begin() {
        resize (600, 600);
        new StickFigure (canvas);
    }
    
    /**
     * Add the text "hi" to the screen when the user depresses the mouse
     * button.
     */
    public void onMousePress (Location point) {
        new Text ("Hi!", point, canvas);
    }
}

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

/**
 * Draws a stick figure
 * 
 * @author Barbara Lerner
 * @version March 3, 2008
 */
public class StickFigure
{
    // The location and size of the head
    private static final int HEAD_LEFT = 50;
    private static final int HEAD_TOP = 50;
    private static final int HEAD_SIZE = 100;
    
    // The location and size of the body
    private static final int BODY_LEFT = HEAD_LEFT + (HEAD_SIZE / 2);
    private static final int BODY_TOP = HEAD_TOP + HEAD_SIZE;
    private static final int BODY_SIZE = HEAD_SIZE * 2;
    
    // The location and size of the arms
    private static final int ARM_X_OFFSET = HEAD_SIZE / 2;
    private static final int ARM_Y_OFFSET = HEAD_SIZE / 2;
    private static final int ARM_BOTTOM = BODY_TOP + ARM_Y_OFFSET;
    private static final int ARM_TOP = BODY_TOP;
    private static final int LEFT_ARM_LEFT = BODY_LEFT - ARM_X_OFFSET;
    private static final int RIGHT_ARM_RIGHT = BODY_LEFT + ARM_X_OFFSET;
    
    // The location and size of the legs.
    private static final int LEG_X_OFFSET = HEAD_SIZE / 2;
    private static final int LEG_Y_OFFSET = LEG_X_OFFSET * 2;
    private static final int LEG_TOP = BODY_TOP + BODY_SIZE;
    private static final int LEFT_LEG_LEFT = BODY_LEFT - LEG_X_OFFSET;
    private static final int RIGHT_LEG_RIGHT = BODY_LEFT + LEG_X_OFFSET;
    private static final int LEG_BOTTOM = LEG_TOP + LEG_Y_OFFSET;
    
    /**
     * Draws a stick figure
     */
    public StickFigure (DrawingCanvas figureCanvas) {
        // Draw the head
        new FramedOval (HEAD_LEFT, HEAD_TOP, HEAD_SIZE, HEAD_SIZE, figureCanvas);
        
        // Draw the body
        new Line (BODY_LEFT, BODY_TOP, BODY_LEFT, BODY_TOP + BODY_SIZE, figureCanvas);
        
        // Draw the arms
        new Line (LEFT_ARM_LEFT, ARM_TOP, BODY_LEFT, ARM_BOTTOM, figureCanvas);
        new Line (RIGHT_ARM_RIGHT, ARM_TOP, BODY_LEFT, ARM_BOTTOM, figureCanvas);
        
        // Draw the legs
        new Line (BODY_LEFT, LEG_TOP, LEFT_LEG_LEFT, LEG_BOTTOM, figureCanvas);
        new Line (BODY_LEFT, LEG_TOP, RIGHT_LEG_RIGHT, LEG_BOTTOM, figureCanvas);
    }
}