Repainting polygons is not displaying
This program should draw each polygon, polyline, etc. multiple times. It works perfectly fine when I remove the call to movePerson() and the surrounding for() loop. However, I cannot get everything to get drawn 3 times at different coordinates. Any advice is appreciated, thanks! :(hi):
Code:
package drawperson2;
// *******************************************************************
// DrawPerson.java
//
// An program that uses the Graphics draw methods to draw a person.
// *******************************************************************
import javax.swing.JFrame;
public class DrawPerson2
{
//-----------------------------------------------
// Creates the main frame for the draw program
//-----------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("Draw Person");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawPersonPanel panel = new DrawPersonPanel ();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
Code:
package drawperson2;
// *******************************************************************
// 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};
private int[] pantsX = {50, 50, 80 ,80 ,100,100, 130, 130, 50};
private int[] pantsY = {240,320,320,290,290,320, 320, 240, 240};
private int headX = 66;
private int headY = 60;
private int[] hairX = {95, 115, 105, 95, 75, 65, 95};
private int[] hairY = {50, 85, 85, 60, 85, 80, 50};
private int[] zigzagX = {80, 110, 85, 85, 105};
private int[] zigzagY = {190, 170, 170, 165, 140};
//--------------------------------------
// Constructor: Set up the panel.
//--------------------------------------
public DrawPersonPanel()
{
setPreferredSize(new Dimension(WIDTH_, HEIGHT_));
}
//--------------------------------------
// Draw person
//--------------------------------------
public void paintComponent (Graphics page)
{
for(int i = 0; i < 3; i ++)
{
//shirt
page.setColor(Color.blue);
page.fillPolygon(shirtX, shirtY, shirtX.length);
//zig zag on shirt
page.setColor(Color.cyan);
page.drawPolyline(zigzagX, zigzagY, zigzagX.length);
//pants
page.setColor(Color.red);
page.fillPolygon(pantsX, pantsY, pantsX.length);
//head
page.setColor(Color.pink);
page.fillOval(headX,headY, 50, 60);
//hair
page.setColor (Color.DARK_GRAY);
page.fillPolygon(hairX,hairY,hairX.length);
movePerson(200, 20);
}
}
public void movePerson(int x, int y)
{
//hair
for(int i = 0; i < hairX.length; i++)
{
hairX[i] += x;
hairY[i] += y;
}
//shirt
for (int i = 0; i < shirtX.length; i++)
{
shirtX[i] += x;
shirtY[i] += y;
}
//pants
for (int i = 0; i < pantsX.length; i++)
{
pantsX[i] += x;
pantsY[i] += y;
}
//zigzag
for (int i = 0; i < zigzagX.length; i++)
{
zigzagX[i] += x;
zigzagY[i] += y;
}
//head
headX += x;
headY += y;
repaint();
}
}
Re: Repainting polygons is not displaying
1. Never perform any kind of business logic or change of state in a painting method override. You have absolutely no control over how often and how many times the method is called.
2. Never invoke repaint() from within a painting method override. This includes repaint() calls in methods called from the painting method.
Quote:
I cannot get everything to get drawn 3 times at different coordinates.
Use a javax.swing.Timer.
db
Re: Repainting polygons is not displaying
This is for a school assignment, I just want to fix the issue at hand and any advice to do so would be helpful. Those are the imports I was told to use.
Re: Repainting polygons is not displaying
Quote:
I just want to fix the issue at hand and any advice to do so would be helpful.
So what do you call my previous post if not advice?
If you're looking for someone to spoonfeed you a working code, then you're in the wrong place.
db
Re: Repainting polygons is not displaying
Quote:
Originally Posted by
DarrylBurke
So what do you call my previous post if not advice?
If you're looking for someone to spoonfeed you a working code, then you're in the wrong place.
db
Tips and a suggestion to add new imports to my program. I didn't at any point ask to be spoon-fed code, you are rudely attacking me as a first-time poster, you, a moderator...
Re: Repainting polygons is not displaying
Quote:
Originally Posted by
orangeweapons
Tips and a suggestion to add new imports to my program. I didn't at any point ask to be spoon-fed code, you are rudely attacking me as a first-time poster, you, a moderator...
Darryl's first reply was full of tips and advice but you chose to ignore it can call Darryl rude?
kind regards,
Jos