I have method that writes how many guesses there are left and I want it to be repainted every time person misses letter or word, however it keeps repainting itself all the time and that causes whole program constantly flash.
My method looks like this:
public void repaintWord(Graphics g){
g.SetColor(Color.RED);
g.drawString("You have " + getN() + " guesses left", 400, 50);
repaint();
}
How to make it repaint only once when getN() changes its value?
Store the value of getN()
when repaintWord()
last exited and compare with getN()
on
entrance:
private int lastN = ??; /* Value for max guesses. */
public void repaintWord(Graphics g)
{
if (lastN != getN())
{
lastN = getN();
g.SetColor(Color.RED);
g.drawString("You have " + lastN + " guesses left", 400, 50);
repaint();
}
}