HideAStickFigure


Your browser is ignoring the <APPLET> tag!

This program draws two stick figures. One stick figure is hidden behind a large black rectangle. The user can drag the rectangle around to hide the other stick figure.

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

/**
 * Draws two stick figures and a large black rectangle on the screen.
 * Allows the user to drag the rectangle.
 * 
 * @author Barbara Lerner
 * @version March 3, 2008
 */
public class HideAStickFigure extends WindowController
{
    // Locations for stick figures and the hider rectangle.
    private static final int FIGURE_TOP = 50;
    private static final int FIGURE1_LEFT = 50;
    private static final int FIGURE2_LEFT = 200;
    private static final int HIDER_LEFT = FIGURE1_LEFT - 10;
    private static final int HIDER_TOP = FIGURE_TOP - 10;
    private static final int HIDER_WIDTH = 120;
    private static final int HIDER_HEIGHT = 420;
    
    // A rectangle that the user can drag around to change what is visible.
    private FilledRect hider;
    
    // Remember if the user is dragging the rectangle
    private boolean hiderGrabbed = false;
    
    // Remember the last mouse location so that we can calculate how far to move
    // the rectangle
    private Location lastPoint;

    /**
     * Draw 2 stick figures with one of them hidden behind a black rectangle
     */
    public void begin() {
        resize (600, 600);
        
        // Draw 2 stick figures
        new StickFigure (FIGURE1_LEFT, FIGURE_TOP, canvas);
        new StickFigure (FIGURE2_LEFT, FIGURE_TOP, canvas);
        
        // Cover one of the stick figures with a rectangle
        hider = new FilledRect (HIDER_LEFT, HIDER_TOP, HIDER_WIDTH, HIDER_HEIGHT, canvas);
    }
    
    /**
     * When the user presses the mouse button, determine if the user is clicking
     * on the black rectangle.  If so, remember that the user is about to drag
     * the rectangle.  Also, remember the current mouse location.
     */
    public void onMousePress (Location point) {
        if (hider.contains (point)) {
            hiderGrabbed = true;
            lastPoint = point;
        }
    }
    
    /**
     * If the user is dragging the rectangle, move it the same distance and 
     * direction as the mouse moved.
     */
    public void onMouseDrag (Location point) {
        if (hiderGrabbed) {
            double dx = point.getX() - lastPoint.getX();
            double dy = point.getY() - lastPoint.getY();
            hider.move (dx, dy);
            lastPoint = point;
        }
    }
    
    /**
     * Stop the dragging.
     */
    public void onMouseRelease (Location point) {
        hiderGrabbed = false;
    }
}

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

/**
 * Draws a stick figure
 * 
 * @author Barbara Lerner
 * @version October 9, 2008
 */
public class StickFigure
{
    // The size of the head
    private static final int HEAD_SIZE = 100;
    
    // The size of the body
    private static final int BODY_SIZE = HEAD_SIZE * 2;
    
    // The relative location of the hand to the shoulder
    private static final int ARM_X_OFFSET = HEAD_SIZE / 2;
    private static final int ARM_Y_OFFSET = HEAD_SIZE / 2;
    
    // The relative location of the foot from the shoulder
    private static final int LEG_X_OFFSET = HEAD_SIZE / 2;
    private static final int LEG_Y_OFFSET = LEG_X_OFFSET * 2;
    
    /**
     * Draws a stick figure
     * 
     * @param headLeft the left edge of the head
     * @param headTop the top edge of the head
     * @param figureCanvas the canvas to draw on
     */
    public StickFigure (int headLeft, int headTop, DrawingCanvas figureCanvas) {
        // The location of the body
        int bodyLeft = headLeft + (HEAD_SIZE / 2);
        int bodyTop = headTop + HEAD_SIZE;
        
        // The location of the arms
        int armBottom = bodyTop + ARM_Y_OFFSET;
        int armTop = bodyTop;
        int leftArmLeft = bodyLeft - ARM_X_OFFSET;
        int rightArmRight = bodyLeft + ARM_X_OFFSET;
        
        // The location of the legs.
        int legTop = bodyTop + BODY_SIZE;
        int leftLegLeft = bodyLeft - LEG_X_OFFSET;
        int rightLegRight = bodyLeft + LEG_X_OFFSET;
        int legBottom = legTop + LEG_Y_OFFSET;

        // Draw the head
        new FramedOval (headLeft, headTop, HEAD_SIZE, HEAD_SIZE, figureCanvas);
        
        // Draw the body
        new Line (bodyLeft, bodyTop, bodyLeft, bodyTop + BODY_SIZE, figureCanvas);
        
        // Draw the arms
        new Line (leftArmLeft, armTop, bodyLeft, armBottom, figureCanvas);
        new Line (rightArmRight, armTop, bodyLeft, armBottom, figureCanvas);
        
        // Draw the legs
        new Line (bodyLeft, legTop, leftLegLeft, legBottom, figureCanvas);
        new Line (bodyLeft, legTop, rightLegRight, legBottom, figureCanvas);
    }
}