Shepard
Shepard

Reputation: 1111

Java Repaints only every other guess

I have a function that prints how many guesses there are for a player.
Here is the code:

public void drawString(Graphics g){
    g.setColor(Color.RED);
    g.drawString("You have " + getN() + " guesses left", 400, 50);
    if (lastN != getN()){
        repaint();
        lastN = getN();
    }
}

My problem is that it repaints every other time. I mean:

First it prints: You have 10 guesses left
then it stays 10 when only 9 guesses left

Then it prints: You have 8 guesses left
ect. until there are 0...

how to make it change every time?

Upvotes: 2

Views: 122

Answers (1)

Harmeet Singh
Harmeet Singh

Reputation: 2616

So you need to do :

if (lastN != getN()){        
    lastN = getN();
}
repaint();

repaint outside of if condition, now in this case paint will be called every time

Upvotes: 4

Related Questions