What does it do?
它打印组件,就像没有覆盖paintComponent方法一样。例如,如果您设置了背景颜色,则通常由您要扩展的类来绘制。
When do we need to use it?
如果不在整个组件上绘画,则可以使用它。你不绘画的部分将“闪耀”,这意味着你应该让超级类绘制这些部分。与背景颜色的示例一样:如果您只是在组件中间绘制一个圆圈,super.paintComponent将确保背景颜色在圆圈周围。
如果您要绘制组件的整个区域,那么您将在任何super.paintComponent绘制之上进行绘制,因此调用super.paintComponent没有任何意义。
What advantage does it gives us by writing it in paintComponent()?
这是唯一合适的地方。当组件应该被绘制时,调用paintComponent,并且如上所述,如果您不自己绘制整个组件,则需要使用super.paintComponent来对所有部件进行绘制。
The documentation的paintComponent说得很好:
[…] 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.