MoveAStickFigure Applet


Your browser is ignoring the <APPLET> tag!


 

Source Code:

 

import objectdraw.*;

import java.awt.*;

 

/**

 * Creates a StickFigure object when program starts.  When mouse is clicked

 * the object moves to the location of the mouse-click.

 *

 * Lisa Ballesteros adapted from Barbara Lerner's DrawAStickFigure program

 * March 2009

 */

public class MoveAStickFigure extends WindowController

{

 

    private StickFigure figure;

   

    /*Draw a stick figure */

    public void begin() {

        resize (600,600);

        figure = new StickFigure(canvas);

    }

   

    // move figure to location @param

    public void onMousePress (Location point) {

        figure.move(point);

    }

}

 

import objectdraw.*;

import java.awt.*;

 

/**

 *  StickFigure composed of a head, body, 2 arms, and 2 legs.  The only action

 *  the object currently has is a 'move'.

 *

 * @author (your name)

 * @version 3/3/2009

 */

public class StickFigure

{

    /* constants for the head */

    private static final int HEAD_SIZE = 100;

    private static final int HEAD_LEFT = 50;

    private static final int HEAD_TOP = 50;

   

    /* constants for 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;

   

    /* constants for the arms (size and location)*/

    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;

   

    /* constants for location and size of 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;

   

    private FramedOval head;

    private Line body, leftArm,rightArm, leftLeg,rightLeg;

   

    // constructor

    public StickFigure (DrawingCanvas figureCanvas) {

       

        head = new FramedOval (HEAD_LEFT, HEAD_TOP,HEAD_SIZE, HEAD_SIZE, figureCanvas);

        body = new Line (BODY_LEFT, BODY_TOP,BODY_LEFT, BODY_TOP + BODY_SIZE, figureCanvas);

       

        leftArm = new Line (LEFT_ARM_LEFT, ARM_TOP, BODY_LEFT,ARM_BOTTOM,figureCanvas);

        rightArm = new Line (RIGHT_ARM_RIGHT, ARM_TOP,BODY_LEFT,ARM_BOTTOM,figureCanvas);

       

        leftLeg = new Line (BODY_LEFT,LEG_TOP,LEFT_LEG_LEFT,LEG_BOTTOM,figureCanvas);

        rightLeg = new Line (BODY_LEFT,LEG_TOP,RIGHT_LEG_RIGHT,LEG_BOTTOM,figureCanvas);

    }

   

    // move the figure to @param point

    public void move (Location point){

       

        // variables to set the x and y coordinates for the head

        double headLeft, headTop;

       

        //variables to calculate the x and y coordinates for the body

        double bodyLeft, bodyTop;

       

        // variables to calculate the x and y coordinates for the arms

        double leftArmLeft, rightArmRight, armTop;

       

        // variables to calculate the x and y coordinates for the legs

        double legTop, leftLegLeft, rightLegRight;

 

        headLeft = point.getX();

        headTop = point.getY();

        bodyLeft = headLeft+HEAD_SIZE/2;

        bodyTop = headTop+HEAD_SIZE;

        leftArmLeft = bodyLeft-ARM_X_OFFSET;

        armTop = bodyTop;

        rightArmRight = bodyLeft+ ARM_X_OFFSET;

        legTop = bodyTop + BODY_SIZE;

        leftLegLeft = bodyLeft;

        rightLegRight = bodyLeft;

       

        head.moveTo(headLeft,headTop);

        body.moveTo(bodyLeft,bodyTop);

        leftArm.moveTo(leftArmLeft,armTop);

        rightArm.moveTo(rightArmRight,armTop);

       

        leftLeg.moveTo(leftLegLeft, legTop);

      rightLeg.moveTo(rightLegRight,legTop);

    }

}