DrawASpeakingStickFigure Applet


Your browser is ignoring the <APPLET> tag!


 

Source Code:

 

import objectdraw.*;

import java.awt.*;

 

/**

 * Creates stick figures that say "Hi" on the drawing canvas.

 *

 * @author Lisa Ballesteros adaptation of Barbara Lerner's DrawAStickFigure

 * March 2009

 */

public class DrawASpeakingStickFigure extends WindowController

{

    /*Draw a stick figure */

    public void begin() {

        resize (600,600);

    }

   

    public void onMouseClick (Location point) {

        new StickFigure (point, canvas);

    }

   

   

    public void onMousePress (Location point) {

        new Text ("Hi!", point, canvas);

    }

}

 

import objectdraw.*;

import java.awt.*;

 

/**

 *  Object has a head, body, 2 arms, and 2 legs.  It has no actions.

 *

 * @author Lisa Ballesteros adaptation of Barbara Lerner's DrawAStickFigure

 * March 2009

 */

public class StickFigure

{

    /* constants for the head */

    private static final int HEAD_SIZE = 100;

   

    /* constants for the body */

    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;

   

    /* 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);

   

    public StickFigure (Location upperLeft, DrawingCanvas figureCanvas) {

        /* location of head */

        double headLeft = upperLeft.getX();

        double headTop = upperLeft.getY();

       

        /* body location */

        double bodyLeft = headLeft + (HEAD_SIZE / 2);

        double bodyTop = headTop + HEAD_SIZE;

       

        /* arm location */

        double armBottom = bodyTop + ARM_Y_OFFSET;

        double armTop = bodyTop;

        double leftArmLeft = bodyLeft - ARM_X_OFFSET;

        double rightArmRight = bodyLeft + ARM_X_OFFSET;

       

        /*leg location */

        double legTop = bodyTop + BODY_SIZE;

        double leftLegLeft = bodyLeft - LEG_X_OFFSET;

        double rightLegRight = bodyLeft + LEG_X_OFFSET;

        double legBottom = legTop + LEG_Y_OFFSET;

       

 

       

        new FramedOval (headLeft,headTop,HEAD_SIZE, HEAD_SIZE, figureCanvas);

        new Line (bodyLeft, bodyTop, bodyLeft, bodyTop+BODY_SIZE, figureCanvas);

        new Line (leftArmLeft, armTop, bodyLeft, armBottom,figureCanvas);

        new Line (rightArmRight, armTop,bodyLeft, armBottom,figureCanvas);

       

        new Line (bodyLeft, legTop, leftLegLeft, legBottom,figureCanvas);

        new Line (bodyLeft,legTop, rightLegRight,legBottom,figureCanvas);

    }

}