Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query on use of paint(Graphics g) method

 
Colm Dickson
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Fred for your input and Campbell for the link. I think (and hope) it will become clearer now.

Colm
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:The article that says this link seems very good . . .
Which link? The one I quoted earlier?
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic