Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the paintComponent method called twice?

Tags:

java

swing

jpanel

I'm working on an app where I draw everything pixel by pixel. During the process, I noticed that the paintComponent() method of a certain JPanel is called twice. So I created the following MCVE to figure out whether it has something to do with the other components being drawn to the screen, or if it's just a standalone issue:

App.java

public class App {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new MainFrame("Paint test"));
    }
}

MainFrame.java

public class MainFrame extends JFrame {
    private Board mBoard;

    public MainFrame(String title) {
        super(title);

        setMinimumSize(new Dimension(400, 400));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());

        mBoard = new Board();
        add(mBoard, BorderLayout.CENTER);

        setVisible(true);
    }
}

Board.java

public class Board extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        System.out.println("CALLED");
    }
}

But the console log shows "CALLED" twice.

I'm specifically interested in paintComponent() since it's the method where all of the app's magic works, so I need to control each draw cycle.

What are the reasons behind this double call to paintComponent() ?

Is there another way I could do my drawings some other way once ? (I mean, not in paintComponent(), if it would be called twice no matter what)

like image 351
Mohammed Aouf Zouag Avatar asked Nov 17 '25 08:11

Mohammed Aouf Zouag


1 Answers

Why is the paintComponent method called twice?

Because it is called whenever the plug-in determines it is needed.

I need to control that behavior.

Better to use an approach that works, rather than chase an end that cannot be achieved.

The logical approach to this conundrum though, is to instead draw everything to a BufferedImage that is displayed in (an ImageIcon that is displayed in) a JLabel. Then no matter how many times the repaint() method of the label is called, the app. still only needs to generate the pixels once.

Examples

  1. Draw in an image inside panel

    enter image description here

  2. Multiple calls of paintComponent()

    enter image description here

  3. Drawing the Cannabis Curve

    enter image description here

like image 118
Andrew Thompson Avatar answered Nov 18 '25 20:11

Andrew Thompson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!