Help: Drawing a Stick Figure


Help is available for each task, or you can go straight to the solution source code.

Task 1

Create a new class called StickyBoy, StickyGirl or ( StickyPerson, if you prefer). Make it a subclass of java.awt.Canvas, select "Write code for this class", and have it import java.awt.*.
  1. Create a new class by clicking on the Create New Class or Interface button in the toolbar.
  2. Set the class name to StickyBoy
  3. Set the superclass to java.awt.Canvas
  4. Check the "Write source code for this class" box.
  5. Press "next".
  6. Press "Add Package".
  7. Select java.awt from the list of packages.
  8. Press "finish".

Task 2

Draw the following elements:
filled rectangle	52,52,46,74
line			76,126,43,190
line			76,126,107,190
line			99,58,134,30
line			52,58,35,115
oval			26,114,12,12
oval			134,20,12,12
oval			63,27,27,25
rectangle		36,190,16,8
rectangle		99,190,16,9
string			"Stick Boy" @ 55,218
Draw the filled rectangle in blue via setColor(Color.blue); and draw everything else in black; see Color. The oval and rectangle coordinates are upper-left corner followed by width, height.
Add a method void paint(Graphics g) to your StickyBoy class by selecting the Stickyboy class in the workbench and pressing the "Create New Method or Constructor" button on the toolbar. Then add the drawing commands to this method.

All of the draw commands are done using methods of java.awt.Graphics.html>Graphics such as drawLine. You must set the color of the graphics context before a draw command to set the color.

If you get stuck, look at the solution and simply paste in the drawing code for this method.

Task 3

Create a new Applet (visually) called Drawing.
From the Workspace window, click on the "A" button.

Task 4

Add two StickyBoy beans to the new applet.
In the Visual Composition Editor select the "Options->Add Bean..." menu item. Select StickyBoy from the list. Click on the left half of the Applet (the dotted rectangle) to place a stickyboy. Repeat to place another, or hold the Control key and drag the current StickyBoy, creating a new copy of him.

Task 5

Save your applet and run it!

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.