HelpMe HelpMe - 1 year ago 94
Java Question

repaint() not calling paintComponent()

First of all, please don't burn me at the stake for creating a question while others with similar names and content exist. I looked through them all, but found no solution.

Calling repaint() absolutely does not call paintComponent(), no matter what I seem to try. Here's all the code related to the problem:

public void mouseClicked(MouseEvent e) {//User clicks on play button, creates a new Level object. Level extends JPanel.
Level l = new Level(2);
System.out.println("Level panel created and content pane set");

As intended, "Level panel created and content pane set" is printed to console.

public class Level extends JPanel implements ActionListener{

private final int levelNum;

public Level(int levelNum){
this.levelNum = levelNum;

this.repaint();//Although this should work in the constructor, how about a Timer that repaints for confirmation?
new Timer(2*1000, this).start();

public void paintComponent(Graphics g){
g.drawImage(Tetris.getTexture("LevelBackdrop.png"), 0, 0, null);
System.out.println("paintComponent - level painted");
g.setColor(new Color(255, 255, 0));
g.drawString("2", 355, 55);

public int getLevelNum() {
return levelNum;

public double getGravity(){
return levelNum/4;

public void actionPerformed(ActionEvent e) {
System.out.println("Timer repainting");


"paintComponent - level painted" is never printed. "Timer repainting" prints out every 2 seconds, as expected.

Answer Source

what is the actual size of the level object when the repaint is called? If it is zero height and width, the paintComponent method will not be called. Since you never call setPreferredSize(...), I think this might be the reason.

If this does not solve your problem, overwrite the repaint() method in Level and break on it to see what it actually does (requires jdk installation).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download