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:
@Override
public void mouseClicked(MouseEvent e) {//User clicks on play button, creates a new Level object. Level extends JPanel.
if(isOnPlayButton(e.getPoint())){
GameState.setState(GameState.INGAME);
Level l = new Level(2);
l.setVisible(true);
Tetris.getWindow().setContentPane(l);
Soundtrack.updateAudio();
System.out.println("Level panel created and content pane set");
}
}
As intended, "Level panel created and content pane set" is printed to console.
@SuppressWarnings("serial")
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();
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(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;
}
@Override
public void actionPerformed(ActionEvent e) {
this.repaint();
System.out.println("Timer repainting");
}
}
"paintComponent - level painted" is never printed. "Timer repainting" prints out every 2 seconds, as expected.
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).