Hello. Just started to learn Java. I do not understand how the paintComponent method works from the program:

import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class prog3 { public static void main(String[] args) { myFrame okno = new myFrame(); } } class myFrame extends JFrame { myFrame() { myPanel pn = new myPanel(); Container cont = getContentPane(); cont.add(pn); setBounds(10, 10, 300, 600); setVisible(true); } } class myPanel extends JPanel { public void paintComponent(Graphics gr) { gr.setColor(Color.BLUE); gr.drawRect(10, 10, 200, 300); } } 

The method is not a constructor, however it starts automatically when an object is created - pn

  • It is called by myFrame because you added it to the list: cont.add (pn); - Alex Krass

2 answers 2

This method is in the JComponent class, from where it is called when the component is drawn. JComponent is the ancestor of the JPanel class from which you inherited. In your case, you redefined it (Override), but your class remained the successor of JComponent, respectively, your method is now called, and not the internal one. I advise in such cases to look at the source code of classes from which you inherit, helps to understand a lot.

** UPD **

 public JPanel(LayoutManager layout, boolean isDoubleBuffered) { setLayout(layout); setDoubleBuffered(isDoubleBuffered); setUIProperty("opaque", Boolean.TRUE); updateUI(); } public JPanel(LayoutManager layout) { this(layout, true); } public JPanel(boolean isDoubleBuffered) { this(new FlowLayout(), isDoubleBuffered); } public JPanel() { this(true); } 

These are constructors JPanel. Since your myPanel is a successor of JPanel, when you call myPanel constructor

 myPanel pn = new myPanel(); 

You have implicitly called the JPanel constructor, the one without parameters. If you look at the chain that will be traversed by JPanel constructors, there is an updateUI () method at the very top, I assume that there is a paintComponent twitching somewhere inside, but you can hardly find it, because there is a lot of reflection, I think this call is hiding somewhere- then there

** UPD UPD **

http://www.oracle.com/technetwork/java/painting-140037.html

Here it is written about all frauds.

  • Thanks for the answer. JComponent is the ancestor of the class JPanel, and my class myPanel is also the ancestor of JPanel, it turns out, I override the method from JPanel, and not from JComponent. Or I do not understand something. And at what moment this method is called, which line of code? When do I put a panel into a container or create an object of class myPanel? I can not understand why the method works immediately, without addressing through a dot. - EvgenS 1:59
  • Your class is a descendant, not an ancestor. - iksuy
  • I figured out the inheritance, but I can't understand why the method starts automatically. Guys, if not hard, send it to a resource where you can read about it or an appropriate topic. - EvgenS
  • @EvgenS, updated a bit - iksuy
  • iksuy, forgot to thank. Thank you so much for the advice. - EvgenS

Tutorial Head First Java, Kathy Sierra, Bert Bates. p. 394

I quote:

At any time when the JVM determines that the screen needs to be updated, your paintComponent() method will be called. Consider that this method is called by the system.