This might be a very noob question. I'm just starting to learn Java
I don't understand the operation of paintComponent method. I know if I want to draw something, I must override the paintComponent method.
public void paintComponent(Graphics g) { ... }
But when is it called? I never see anything like "object.paintComponent(g)" but still it is drawn when the program is running.
And what is the Graphics parameter? Where is it from? Parameter must be supplied when the method is called. But as I said before, it seems like this method is never be explicitly called. So who provides this parameter? And why do we have to cast it to Graphics2D?
public void paintComponent(Graphics g) { ... Graphics2D g2= (Graphics2D) g; ... }
The paintComponent() method can also be called explicitly by the repaint() method defined in Component class. The effect of calling repaint() is that Swing automatically clears the graphic on the panel and executes the paintComponent method to redraw the graphics on this panel.
The invocation of super. paintComponent(g) passes the graphics context off to the component's UI delegate, which paints the panel's background.
The argument for paintComponent() is a type Graphics which is from java.awt.Graphics: public void paintComponent(Graphics g) {} The parameter g is a Graphics object. Actually, the object referenced by g is an instance of the Graphics2D class.
repaint() calls the paintComponent() method. Every time you wish to change the appearance of your JPanel, you must call repaint().
The (very) short answer to your question is that paintComponent
is called "when it needs to be." Sometimes it's easier to think of the Java Swing GUI system as a "black-box," where much of the internals are handled without too much visibility.
There are a number of factors that determine when a component needs to be re-painted, ranging from moving, re-sizing, changing focus, being hidden by other frames, and so on and so forth. Many of these events are detected auto-magically, and paintComponent
is called internally when it is determined that that operation is necessary.
I've worked with Swing for many years, and I don't think I've ever called paintComponent
directly, or even seen it called directly from something else. The closest I've come is using the repaint()
methods to programmatically trigger a repaint of certain components (which I assume calls the correct paintComponent
methods downstream.
In my experience, paintComponent
is rarely directly overridden. I admit that there are custom rendering tasks that require such granularity, but Java Swing does offer a (fairly) robust set of JComponents and Layouts that can be used to do much of the heavy lifting without having to directly override paintComponent
. I guess my point here is to make sure that you can't do something with native JComponents and Layouts before you go off trying to roll your own custom-rendered components.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With