Chapter 8: Arrays 139 A Polygon Person A polygon is a multisided closed figure;
ID: 3833054 • Letter: C
Question
Chapter 8: Arrays
139
A Polygon Person
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. Read section7.8 in the text and study the Rocket example in Listing 7.16 & 7.17.
Files DrawPerson.java and DrawPersonPanel.java contain a program that draws a blue shirt. Copy the programs to your directory, compile DrawPerson.java, and run it 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 is probably best done with a polygon, so again you’ll need two arrays to hold the points.
4. Draw a zigzag across the front of the shirt. Use a polyline.
5. Write a method movePerson(int x, inty) 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, hair and zigzag arrays and the head x and y coords and increment all of the coordinates by the x or y value as appropriate. (Thi s isn’t necessarily the cleanest way to do this, but it’s very straightforward).
6. Now put a loop in your paintComponent method that draws the person three times, moving him (her?) 150 or so pixels each time (you decide how far).
// *******************************************************************
// DrawPerson.java
//
// An program that uses the Graphics draw methods to draw a person.
// *******************************************************************
import.javax.swing.JFrame;
public class DrawPerson
{
//-----------------------------------------------
// Creates the main frame for the draw program
//-----------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Draw Person");
frame.setDefaultCloseOperaton (JFrame.EXIT_ON_CLOSE);
DrawPersonPanel panel = new DrawPersonPanel ();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
// *******************************************************************
// DrawPersonPanel.java
//
// An program that uses the Graphics draw methods to draw a person.
// *******************************************************************
import javax.swing.JPanel;
import java.awt*;
public class DrawPersonPanel extends JPanel
{
private final int WIDTH = 600;
private final int HEIGHT = 400;
private int[] shirtX = {60,0,20,60,50,130,120,160,180,120};
private int[] shirtY = {100,150,180,160,250,250,160,180,150,100};
//--------------------------------------
// Constructor: Set up the panel.
//--------------------------------------
public DrawPersonPanel()
{
setPreferredSize(new Dimension(WIDTH, HEIGHT));
}
//--------------------------------------
// Draw person
//--------------------------------------
public void paintComponent (Graphics page)
{
page.setColor(Color.blue);
page.fillPolygon(shirtX, shirtY, shirtX.length);
}
}