Who is calling the paintComponent () method in my class?

I have a simple class that draws graphics in JPanel. This is my class:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

class Drawing_panel extends JPanel {
    public void paintComponent(Graphics g) {
    super.paintComponent(g);       
    this.setBackground(Color.white);
    g.setColor(Color.red);
    g.fillRect(150, 80, 20, 20);
}  

public Dimension getPreferredSize(){
    return new Dimension(500,500);
}

}

I have another class that creates this instance:

Drawing_panel dp = new Drawing_panel();

There is Drawing_panelno constructor in the class and / or there is no explicit call to the paintComponent()or methods getPreferredSize(). I assume that the method is called in the parent constructor JPanel, but I also did not see the calls there.

+5
source share
2 answers

paintComponentcalled from several different places. The call from JComponent.paintis probably the one you are looking for.

, paintComponent . paintComponent " " , , . ( , , .) : ", ", ", ".

- , , Swing Event Dispatch Thread.

+6

JComponent JPanel , paintComponent(). , - , , .    ,    ( ) , "" paintComponent() , .

+3

All Articles