CPSC 170 Lab 3: Polygons and Polylines

As usual, create a lab3 subdirectory for today's lab, open this document in Netscape, and start emacs.

A polygon is a multisided closed figure; a polyline is a line with an arbitrary number of segments. Both polygons and polylines are defined by a set of points, and Java provides graphics methods for both that are based on arrays. Carefully read pages 306-309 (just the top of 309) in the book about polygons and polylines, and study the Rocket example.

File DrawPerson.java contains an applet that draws a shirt. Copy it and the associated html file to your directory, compile DrawPerson.java, and run it using the appletviewer to see what it does. Now modify it as follows:

  1. Draw pants to go with the shirt (they should be a different color). You will need to declare pantsX and pantsY arrays like the shirtX and shirtY arrays and figure out what should go in them. Then make the paint method draw the pants as well as the shirt.

  2. Draw a head. This can just be a circle (or oval), so you won't need to use the Polygon methods. Declare variables headX and headY to hold the position of the head (its upper lefthand corner), and use them when you draw the circle.

  3. Draw hair on the head. This will be a polyline, so again you'll need two arrays to hold the points.

  4. Write a method movePerson(int x, int y) that moves the person by the given number of pixels in the x and y direction. This method should just go through the shirt, pants, and hair arrays and the head x and y coords and increment all of the coordinates by the x or y value as appropriate. (This isn't necessarily the cleanest way to do this, but it's very straightforward).

  5. Now put a loop in your paint method that draws the person three times, moving him (her?) 150 or so pixels each time (you decide how far).

HAND IN: