Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By Kagiso

Thread: Question about this particular method: paintComponent(Graphics g)

  1. #1
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Question about this particular method: paintComponent(Graphics g)

    Java Code:
    	private static class DisplayPanel extends JPanel
    	{
    		public void paintComponent(Graphics g)
    		{
    			super.paintComponent(g);   // why is it that when I remove this line the background color doesn't change
    			setBackground(Color.GRAY);
    			g.drawString("Hello World",50,50);
    		}
    	}
    why can't I just call setBackground() ? what am I missing here?

    thanks in advance

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,424
    Rep Power
    5

    Default Re: Question about this particular method: paintComponent(Graphics g)

    The setBackground method you are calling is inherited by JPanel. Even though you override paintComponent in your subclass, JPanel and its inheritance hierarchy still perform some graphics functions. One of those functions is to paint the background.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Question about this particular method: paintComponent(Graphics g)

    Please correct me if I got it wrong

    super.paintComponent(g) creates the background(a wall where you can paint on )
    then setBackground() paints a color on that wall

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,424
    Rep Power
    5

    Default Re: Question about this particular method: paintComponent(Graphics g)

    paintComponent (at whatever level it is invoked) just does painting. The "wall" so to speak, is created by the JPanel. JFrame is similar in that it has a variety of panes on which to paint.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,776
    Rep Power
    5

    Default Re: Question about this particular method: paintComponent(Graphics g)

    The code makes little sense anyway; what is happening here is

    - paint panel
    - set the color property of the panel

    But that needs to be the other way around; set the color first, then paint. You don't want to set the color WHILE painting, you want to set that once when you create the JPanel, before it is painted for the first time.

    The reason why this works at all is because you'll get the proper color on screen when the panel repaints itself a second time. But the first time it still has the default background color. You may see a little glitch when starting the application perhaps.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Question about this particular method: paintComponent(Graphics g)

    Jimmy, please bear with me if I'm being difficult... but I really need to understand
    super.paintComponent()... what does it actually do?
    Why can't I simply call setBackground() to change the color of DisplayPanel ?
    Last edited by Kagiso; 10-03-2013 at 04:59 PM.

  7. #7
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Question about this particular method: paintComponent(Graphics g)

    Quote Originally Posted by gimbal2 View Post
    The code makes little sense anyway; what is happening here is

    - paint panel
    - set the color property of the panel

    But that needs to be the other way around; set the color first, then paint. You don't want to set the color WHILE painting, you want to set that once when you create the JPanel, before it is painted for the first time.

    The reason why this works at all is because you'll get the proper color on screen when the panel repaints itself a second time. But the first time it still has the default background color. You may see a little glitch when starting the application perhaps.
    What are you talking about? this code is fine(It's taken from a textBook... for your information)... there's no glitch... perhaps you also need to learn a bit more about javax.swing
    Last edited by Kagiso; 10-03-2013 at 05:02 PM.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,424
    Rep Power
    5

    Default Re: Question about this particular method: paintComponent(Graphics g)

    First, Gimbal is correct! The example is wrong. setBackground is pertinent to the JPanel and has nothing to do with the paintComponent at this level. The only way to correct it would be to put the setBackground before the super.paintComponent() invocation. That is because the paintComponent method that paints the background uses the value that you are setting. But if you set it after the fact, it won't work first time around. The setBackground call should be done higher up in your program, probably in the constructor of the DisplayPanel class.

    One more clarification which I didn't mention. By wrong, I didn't mean wouldn't work. It is inefficient. When you do the setBackground in your paintComponent method it will determine if the background needs to be repainted and do so resulting in two paintings upon startup.

    And of course, you have to add this to a JFrame or some other top level container for it to display at all.

    Regards,
    Jim
    Last edited by jim829; 10-03-2013 at 05:56 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,424
    Rep Power
    5

    Default Re: Question about this particular method: paintComponent(Graphics g)

    Not to belabor the point but try this code.

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class ColorCheck extends JPanel {
       JFrame        frame;
       int           width         = 600;
       int           height        = 600;
       public ColorCheck () {
          frame = new JFrame();
          frame.add(this);
          setPreferredSize(new Dimension(width, height));
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
       public static void main(String[] args) {
          new ColorCheck();
       }
       public void paintComponent(Graphics g) {
          System.out.println("painting");
          setBackground(Color.red);
          setBackground(Color.blue);
          super.paintComponent(g);
       }
    }
    Then move one of the setBackgrounds to your constructor and see what happens when you run it. And finally, move the last one to your constructor and see what happens.

    Regards,
    Jim
    Last edited by jim829; 10-03-2013 at 06:27 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Question about this particular method: paintComponent(Graphics g)

    Quote Originally Posted by Kagiso View Post
    What are you talking about? this code is fine(It's taken from a textBook... for your information)... there's no glitch... perhaps you also need to learn a bit more about javax.swing
    Burn the book. It's a crime against trees to print crap like that.

    Quote Originally Posted by jim829 View Post
    The only way to correct it would be to put the setBackground before the super.paintComponent() invocation.
    No. The state of a Component should never be changed inside a painting method override. The only way to correct this is to setBackground(...) elsewhere. Keep painting methods for painting and painting alone.

    Oh, and setBackground(...) (like so many methods that change the state of a Component) internally calls repaint().
    Java Code:
        public void setBackground(Color bg) {
    	Color oldBg = getBackground();
    	super.setBackground(bg);
    	if ((oldBg != null) ? !oldBg.equals(bg) : ((bg != null) && !bg.equals(oldBg))) {
    	    // background already bound in AWT1.2
    	    repaint();
    	}
        }
    The only reason this defective code doesn't result in a CPU-toasting infinite loop is the check that the background really has been changed, and that pending painting events are coalesced.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,424
    Rep Power
    5

    Default Re: Question about this particular method: paintComponent(Graphics g)

    No. The state of a Component should never be changed inside a painting method override. The only way to correct this is to setBackground(...) elsewhere. Keep painting methods for painting and painting alone.
    Yes, I know. I forgot to remove that statement (which should have been somewhat apparent from my other comments).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Question about this particular method: paintComponent(Graphics g)

    OK now I get it
    Java Code:
    private static class DisplayPanel extends JPanel 
    {
    	private DisplayPanel()
    	{
    		setBackground(Color.GRAY); // it's supposed to be here
    	}
    	
    	public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    	//	setBackground(Color.GRAY);       this line isn't supposed to be in this method
    		g.drawString("Hello World",50,50);
    	}
    }
    To Gimbal... I appologise, you were right...
    To Darryl .... to be honest, I'm the one that added the setBackground()
    To Jim.... I've copied the code... I'll play around with it

    Thanx to all of you ...
    gimbal2 likes this.

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,776
    Rep Power
    5

    Default Re: Question about this particular method: paintComponent(Graphics g)

    Quote Originally Posted by Kagiso View Post
    To Gimbal... I appologise, you were right...
    No worries, high five!

    But to answer your earlier question: what I'm talking about? Something drawn from experience and knowledge. Next time you go to a forum to ask for help, give the people answering the benefit of the doubt and you won't have to apologize. See something that doesn't mesh with what you think is right? Look it up, don't flat out reject it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2012, 01:33 PM
  2. Replies: 0
    Last Post: 11-07-2012, 11:49 AM
  3. Replies: 5
    Last Post: 10-02-2011, 02:55 AM
  4. Graphics wont show up(paintComponent)
    By TheBreadCat in forum New To Java
    Replies: 3
    Last Post: 02-13-2011, 06:00 PM
  5. paintComponent() Method straitjacket
    By oldalistair in forum New To Java
    Replies: 5
    Last Post: 09-11-2010, 12:06 AM

Posting Permissions

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