1

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.

HelpMe
  • 13
  • 4
  • can you just remove the timer try it, and remove that repaint() from the Level Constructor – whyn0t Aug 26 '16 at 22:24
  • I suggest Swingworkers: http://stackoverflow.com/questions/782265/how-do-i-use-swingworker-in-java – David Pérez Cabrera Aug 26 '16 at 22:27
  • 3
    I recommend posting an [mcve]. As it is currently, one cannot reproduce what you are experiencing and there doesn't seem to be anything obvious (at least that I see) from the posted code. – copeg Aug 26 '16 at 22:27
  • "remove the timer try it, and remove that repaint()." No, that did not work. – HelpMe Aug 26 '16 at 22:31
  • "I recommend posting an Minimal, Complete, and Verifiable example." It's hard to post a minimal **and** verifiable example. Do you want me to post all the classes? All the code? – HelpMe Aug 26 '16 at 22:33
  • "I suggest Swingworkers." I don't completely understand this. Where and how would I use it? Are you sure that this is the problem? – HelpMe Aug 26 '16 at 22:35
  • 2
    `Do you want me to post all the classes? All the code?` No - that's not 'minimal'. The point is 2 fold: 1. You are asking volunteers for help - posting excessive code will most likely require too much time for any volunteer to take 2. Often the problem is revealed during the process of creating an mcve (and that's what debugging is all about) – copeg Aug 26 '16 at 22:36
  • To continue on copeg's suggestion, create a single frame with a panel and a button. Override the panel's `paintComponent` method and have the button call repaint on the panel. Do this from scratch (takes ~1 min). Compare the results and the code. Once you find the thing that causes the problem, if you didn't solve it yourself by now, post the modified MCVE and we'll help you. – user1803551 Aug 26 '16 at 22:41
  • @copeg, I agree . HelpMe no offence but just be more precise about the problem, review your code, debug till finding what's wrong or at least limit the area of suspicious code. www.youtube.com/watch?v=XmlXU4uK5rA – whyn0t Aug 26 '16 at 22:50

1 Answers1

4

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).

lnyng
  • 925
  • 6
  • 18
  • If you have a question ask it in the comments. Don't post questions as an answer. – user1803551 Aug 26 '16 at 22:50
  • I think you're right. This might be the problem. Near my call to repaint(), I did "System.out.println(this.getSize().getWidth() + " : " + this.getSize().getHeight());" And both the width and height came out to 0.0 Unfortunately, setPrefferedSize() doesn't do anything to fix that. – HelpMe Aug 26 '16 at 23:03
  • I used setSize() and it worked! THANK YOU SO MUCH =) – HelpMe Aug 26 '16 at 23:05
  • I haven't use swing stuff for a while, but I remember that `setPreferredSize()` is usually a better option. It would take a while to figure out why it does not work for you though. – lnyng Aug 26 '16 at 23:11