wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Query on use of paint(Graphics g) method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Query on use of paint(Graphics g) method" Watch "Query on use of paint(Graphics g) method" New topic
Author

Query on use of paint(Graphics g) method

Colm Dickson
Ranch Hand

Joined: Apr 04, 2009
Posts: 89
Hello.


I'm a bit puzzled with the rules concerning the use of the paint() method inherited from the Container class. The rules state that you cannot call this directly?
whay exactly does this mean? If you implement it in a class of type JFrame for example then this paint() method is automatically called when the screen is displayed...is this the correct train of thought?

Why is it that it can only be implemented in the example above (meaning that is called automatically) but then we can use it in a subclass that requires the graphics from the parent class that we can then call it using super.paint()?


Thanks.

Colm
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37957
    
  22
Is it any help if you look at the documentation for paint, or what you are actually supposed to use for Swing, paintComponent, or this link from the paint documentation?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Colm, I struggled with the very questions you are asking. It's not easy to explain in as simple answer.

For me it help to look at examples and explanations of the paint method in action. That is a needed supplement to the API documentation.

I use paint when I am doing animation, or my chess game. Often I will write a class that extends JPanel, and this class will include a paint method. When the application is first started. this paint method gets called automatically once, about the time when the contructor is called, or shortly after. If there are any changes in the position after this first time. Then repaint() is used, and even though the name is different, it still uses the paint() method in my class.

If my class does not include a paint method, then I suppose somewhere up the chain a paint() method is being called once, to display the GUI, but you don't really need to understand that for your program to work.

with specific regard to the container class that you mentioned, your statement "f you implement it in a class of type JFrame for example then this paint() method is automatically called when the screen is displayed...is this the correct train of thought? "

is not too far off the mark. I would say essentially yes this is true.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Colm Dickson wrote:Hello.


I'm a bit puzzled with the rules concerning the use of the paint() method inherited from the Container class. The rules state that you cannot call this directly?
whay exactly does this mean? If you implement it in a class of type JFrame for example then this paint() method is automatically called when the screen is displayed...is this the correct train of thought?

Why is it that it can only be implemented in the example above (meaning that is called automatically) but then we can use it in a subclass that requires the graphics from the parent class that we can then call it using super.paint()?


Thanks.

Colm


OK I didn't really address this specifically. If you implement a JFrame without specifying a paint() then any paint functionality is inherited automatically from the parent. If you do specify a paint method(), maybe by writing a class that extends JFrame and writing your own paint method, then a call to super.paint() allows you to add your own paint functionality without overriding that which is provided by the parent.

I'll qualify this by saying I have never had to use super.paint() myself, nor have I ever specified paint() with a JFrame. Only with JPanels. That doesn't mean I don't think it has a purpose, I see it more as a question of programming style, or else a question of how sophisticated your program needs to be.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37957
    
  22
You don't usually use the paintComponent() method of the JFrame. You add some sort of Component to that frame, commonly a JPanel, and you override the painting methods of that. The method you are really interested in is paintComponent. Don't confuse it with paintComponents.

As far as I can remember, you are supposed to use paintComponent() and its first line should read super.paintComponent(g); That means you call the superclass version which paints a plain Component, then you call your own code which paints whatever you tell it to.
You don't actually call paint() and I don't think you call paintComponent() either. You can get paint() by calling repaint(). What usually happens is that when some event occurs, eg Timer, Component becomes visible, the JVM calls the paint method of whatever, and sends it a Graphics object (well, it's actually some sort of Graphics2D so you can cast it), then the paint method passes the Graphics object to the paintComponent method.

Have you seen the links I quoted earlier? Did they help?

Don't say "parent" to mean "superclass" or "child" to mean "subclass". In GUIs "parent" means what appears outside and "child" means what appears inside, so you can get confused if you aren't strict with those terms.
I don't think you pass Graphics from the superclass to the subclass, but vice versa. You have a subclass (public class MyPanel extends JPanel) and (I think) the subclass paintComponent method passes its Graphics object to the superclass paintComponent method.

Rob: Maybe you can explain it better
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
The article that says this link seems very good, a different way of describing things that the API or the tutorial. Duly noted on using paintComponent() . I'll have to try it. I've never really understood the difference between paint and paintComponent(), or when one might be better than the other, but I'd like to find out.
Colm Dickson
Ranch Hand

Joined: Apr 04, 2009
Posts: 89
Thanks Fred for your input and Campbell for the link. I think (and hope) it will become clearer now.

Colm
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37957
    
  22
Fred Hamilton wrote:The article that says this link seems very good . . .
Which link? The one I quoted earlier?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Campbell Ritchie wrote:
Fred Hamilton wrote:The article that says this link seems very good . . .
Which link? The one I quoted earlier?


sorry I meant the third link of your first post in this thread. The one that says "this link". I had intended to refer to the link, not create one, which I think was probably confusing.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37957
    
  22
Thank you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Query on use of paint(Graphics g) method
 
Similar Threads
paint() method
I'm new to Java, you see...
Graphics g
Need help on Graphics and repaint
method calls