Moving a Stick Figure
The file StickFigure.java contains a class that represents a stick figure. Study the file and note that in addition to a method to
draw the stick figure there are methods to manipulate it:
The method move repositions (“moves”) the figure up or down and over (left or right) on the panel by changing the
The file MoveStickMan.java contains a program that draws a single stick figure that can be moved around and modified using
the keyboard. The file MovePanel.java represents the panel on which the stick figure is displayed. It is similar to
DirectionPanel.java in Listing 7.23 of the text. Note that the constructor adds a KeyListener and contains a call to the
setFocusable method. There is a partially defined inner class named MoveListener that implements the KeyListener interface.
Currently the MoveListener listens only for two arrow keys (left and right) and the g key. The arrow keys move the stick
figure left and right on the panel (the number of pixels moved is in the constant JUMP) and when the user presses the letter
g, the figure “grows” (increases in height by 50%). Compile, and run the program. Test out the arrow keys and the g key.
Now add code to MovePanel.java to have the program respond to the following additional key events:
When the up and down arrow keys are pressed the figure should move JUMP pixels up or down, respectively, on the
When the letter m (for middle) is pressed, the stick figure should place its arms horizontally with legs not quite as far out.
To do this the arm position needs to be 0 (0 pixels above horizontal); a value of 20 for the leg position is good.
Compile and run the program. Try out all the keys it implements.
// ***************************************************************
// StickFigure.java
private int headW; // width of the head
private int legLength; // length of the legs
private int legPosition; // # pixels the legs are up from vertical
private int armLength; // horizontal length of the arms