Results 1 to 13 of 13
- 10-03-2013, 01:00 AM #1
Member
- Join Date
- Nov 2012
- Location
- Johannesburg, South Africa
- Posts
- 92
- Rep Power
- 0
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); } }
thanks in advance
- 10-03-2013, 02:39 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-03-2013, 11:35 AM #3
Member
- Join Date
- Nov 2012
- Location
- Johannesburg, South Africa
- Posts
- 92
- Rep Power
- 0
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
- 10-03-2013, 03:35 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-03-2013, 04:49 PM #5
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 10-03-2013, 05:53 PM #6
Member
- Join Date
- Nov 2012
- Location
- Johannesburg, South Africa
- Posts
- 92
- Rep Power
- 0
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 05:59 PM.
- 10-03-2013, 05:59 PM #7
Member
- Join Date
- Nov 2012
- Location
- Johannesburg, South Africa
- Posts
- 92
- Rep Power
- 0
Re: Question about this particular method: paintComponent(Graphics g)
Last edited by Kagiso; 10-03-2013 at 06:02 PM.
- 10-03-2013, 06:35 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimLast edited by jim829; 10-03-2013 at 06:56 PM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-03-2013, 07:03 PM #9
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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); } }
Regards,
JimLast edited by jim829; 10-03-2013 at 07:27 PM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-03-2013, 09:09 PM #10
Re: Question about this particular method: paintComponent(Graphics g)
Burn the book. It's a crime against trees to print crap like that.
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(); } }
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 10-03-2013, 09:44 PM #11
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 10-04-2013, 12:42 AM #12
Member
- Join Date
- Nov 2012
- Location
- Johannesburg, South Africa
- Posts
- 92
- Rep Power
- 0
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 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 ...
- 10-04-2013, 10:11 AM #13
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Question about this particular method: paintComponent(Graphics g)
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
-
[Java 7 SE+MySQL+NetBeans 7.2.1+Graphics (2D)]paint and paintComponent problem
By KernelPanic in forum AWT / SwingReplies: 2Last Post: 11-07-2012, 02:33 PM -
[Java 7 SE+MySQL+NetBeans 7.2.1+Graphics (2D)]paint and paintComponent problem
By KernelPanic in forum Advanced JavaReplies: 0Last Post: 11-07-2012, 12:49 PM -
the graphics won't show up when i use the methord paintComponent of JPanel class.HELP
By mcclain in forum AWT / SwingReplies: 5Last Post: 10-02-2011, 03:55 AM -
Graphics wont show up(paintComponent)
By TheBreadCat in forum New To JavaReplies: 3Last Post: 02-13-2011, 07:00 PM -
paintComponent() Method straitjacket
By oldalistair in forum New To JavaReplies: 5Last Post: 09-11-2010, 01:06 AM
Bookmarks