Source code:
import objectdraw.*;
import java.awt.*;
import objectdraw.*;
import java.awt.*;
/**
* Places a stick figure object on the canvas when the program begins.
* When the mouse is pressed, text box says 'Hi' at the point of
* the press.
*
* Lisa Ballesteros adapted from Barbara Lerner
* @version
*/
public class DrawAStickFigure extends WindowController
{
/*Draw a stick figure */
public void begin() {
resize (600,600);
new StickFigure(canvas);
}
// create new text box "Hi" @param point
public void onMousePress (Location point) {
new Text ("Hi!", point, canvas);
}
}
/**
* Demonstrates the use of graphical objects to create a new kind of object.
*
* Lisa Ballesteros adapted from Barbara Lerner
* @version
*/
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;
public StickFigure (DrawingCanvas figureCanvas) {
new FramedOval (HEAD_LEFT, HEAD_TOP,HEAD_SIZE, HEAD_SIZE, figureCanvas);
new Line (BODY_LEFT, BODY_TOP,BODY_LEFT, BODY_TOP + BODY_SIZE, figureCanvas);
new Line (LEFT_ARM_LEFT, ARM_TOP, BODY_LEFT,ARM_BOTTOM,figureCanvas);
new Line (RIGHT_ARM_RIGHT, ARM_TOP,BODY_LEFT,ARM_BOTTOM,figureCanvas);
new Line (BODY_LEFT,LEG_TOP,LEFT_LEG_LEFT,LEG_BOTTOM,figureCanvas);
new Line (BODY_LEFT,LEG_TOP,RIGHT_LEG_RIGHT,LEG_BOTTOM,figureCanvas);
}
}