17

What does super.paintComponent(g) do (especially when we place it inside paintComponent() method)? I am surprised I don't see anyone asking this in SO before.

I dig out my school notes on Java Graphics, the only thing it mentioned on this line of code is "do not delete".

However I have been practicing and tying out on Java paintComponent() method these few weeks. So far I have not included that line into my codes, and everything seems to work well (so far). So..

Questions:

  1. What does it do?
  2. When do we need to use it?
  3. What advantage does it gives us by writing it in paintComponent()?
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • _.." and everything seems to work well"_ - Try and do some [animation](http://stackoverflow.com/a/22123304/2587435) without calling it and see what happens. The result should give you a good understanding of why we should call it. See [result of not calling it](http://stackoverflow.com/q/26477521/2587435) – Paul Samsotha Feb 25 '15 at 16:48
  • 1
    @peeskillet I just voted up your solution in http://stackoverflow.com/questions/22123266/drawing-multiple-jcomponents-to-a-frame . That is a nice solution. – user3437460 Feb 25 '15 at 17:33

2 Answers2

14
  1. What does it do?

It prints the component as if you hadn't overridden the paintComponent method. If you have a background color set for instance, this is typically painted by the class you're extending.

  1. When do we need to use it?

You use it if you don't paint on the entire component. The parts that you don't paint will "shine through" which means that you should let the super class paint those parts. As with the example of the background color for instance: If you just paint a circle in the middle of the component, super.paintComponent will make sure the background color is painted around the circle.

If you do paint the entire area of your component, then you will paint on top of whatever super.paintComponent paints and thus there's no point in calling super.paintComponent.

  1. What advantage does it gives us by writing it in paintComponent()?

That's the only logical place to put it. paintComponent is called when the component should be painted, and, as mentioned above, if you don't paint the entire component yourself, you need super.paintComponent to paint on the parts that shine through.

The documentation of paintComponent says it pretty well:

[...] if you do not invoker super's implementation you must honor the opaque property, that is if this component is opaque, you must completely fill in the background in a non-opaque color. If you do not honor the opaque property you will likely see visual artifacts.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Thanks for your reply. It looks like I should always try to include that line of code. However, is there any situation where I should not put that line of code into `paintComponent()` ? – user3437460 Feb 25 '15 at 16:57
  • Yes. If you paint on your entire component (say, you paint some rainbow background that covers the entire component for instance) then it's completely pointless to first do super.paintComponent. – aioobe Feb 25 '15 at 16:58
  • @aiobee I see, thanks a lot for your concise and precise reply. – user3437460 Feb 25 '15 at 17:00
3

From the Java Painting Tutorial:

Most of the standard Swing components have their look and feel implemented by separate "UI Delegate" objects. The invocation of super.paintComponent(g) passes the graphics context off to the component's UI delegate, which paints the panel's background. For a closer look at this process, see the section entitled "Painting and the UI Delegate" in the aforementioned SDN article.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107