Results 1 to 6 of 6
  1. #1
    orangeweapons is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default 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!

    Java 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);
        }
    }
    Java 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();
        }
     }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default 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.

    I cannot get everything to get drawn 3 times at different coordinates.
    Use a javax.swing.Timer.

    db
    Last edited by DarrylBurke; 02-14-2012 at 08:43 AM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    orangeweapons is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Repainting polygons is not displaying

    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    orangeweapons is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: Repainting polygons is not displaying

    Quote Originally Posted by DarrylBurke View Post
    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...

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Repainting polygons is not displaying

    Quote Originally Posted by orangeweapons View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Algorithm for filling random polygons
    By theraccoon in forum Java 2D
    Replies: 3
    Last Post: 05-18-2011, 06:35 PM
  2. Create an array of polygons
    By jtech55 in forum Java 2D
    Replies: 0
    Last Post: 03-18-2011, 07:49 PM
  3. Polygons... returning and passing objects?
    By asherwolf in forum New To Java
    Replies: 3
    Last Post: 07-09-2010, 04:41 PM
  4. Replies: 0
    Last Post: 07-02-2008, 07:50 PM
  5. Demonstration of drawing polygons
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 09:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •