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);
}
}
Explanation / Answer
import java.awt.*; import java.awt.event.*; import java.applet.*; public class SimplePolygons extends Applet implements MouseListener { /* Variables for implementing polygon input. */ private int[] xCoord, yCoord; // Arrays containing the points of // the polygon. Up to 500 points // are allowed. private int pointCt; // The number of points that have been input. private final static Color polygonColor = Color.red; // Color that is used to draw the polygons. public void init() { // Initialize the applet. The applet listens for mouse events. // Arrays are created to hold the points. setBackground(Color.white); addMouseListener(this); xCoord = new int[500]; yCoord = new int[500]; pointCt = 0; } public void paint(Graphics g) { // The paint() routine does nothing but draw a 1-pixel black // border around the applet. Polygons drawn on the applet // are not permanent. g.setColor(Color.black); g.drawRect(0, 0, getSize().width - 1, getSize().height - 1); } // end paint() private void putLine(int x1, int y1, int x2, int y2) { // Draw a line from (x1,y1) to (x2,y2) directly onto the // applet, without going through the paint() method. Graphics g = getGraphics(); g.drawLine(x1,y1,x2,y2); g.dispose(); } private void putPolygon() { // Draw the polygon described by the arrays xCoord and yCoord // and the integer pointCt. A filled polygon with a black // outline is drawn. If pointCt is 0 or 1, nothing is drawn. // If pointCt is 2, only a black line is drawn. if (pointCt < 2) return; Graphics g = getGraphics(); if (pointCt == 2) { g.drawLine(xCoord[0], yCoord[0], xCoord[1], yCoord[1]); } else { g.setColor(Color.red); g.fillPolygon(xCoord, yCoord, pointCt); g.setColor(Color.black); g.drawPolygon(xCoord, yCoord, pointCt); } g.dispose(); } public void mousePressed(MouseEvent evt) { // Process a user mouse-click. if (evt.isShiftDown()) { // Clear the applet. (This only requires a repaint.) // Also, set pointCt to zero to start a new polygon. pointCt = 0; repaint(); } else if ( pointCt > 0 && (Math.abs(xCoord[0] - evt.getX())Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.