I'm trying to write a solitaire game.
I would like to call repaint()
when the game starts and paint the full deck
once only the first time repaint is called, but when I add the if-statement
it
no longer paints.
Here is the code with the if-statement:
private void paintInitialDeck(Graphics g, Card card){
card.paintCard(g);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (initialDrawing) {
Card card;
for (int i = 0; i < deck.size(); i++) {
card = deck.get(i);
card.setY((int) (50 + 0.2 * i));
card.setX((int) (250 + 0.2 * i));
paintInitialDeck(g, card);
}
initialDrawing = false;
}
}
It doesn't work, but if I remove the if statement and the initialDrawing = false
then it works.
Problem is, I need the if-statement.
Anyone can help me understand this better?
ps.: initialDrawing is set to true to begin with. Also, it's really the initialDrawing = false
that makes the whole thing not paint.
The straight-forward solution would be
if (initialDrawing) {
Card card;
for (int i = 0; i < deck.size(); i++) {
card = deck.get(i);
card.setY((int) (50 + 0.2 * i));
card.setX((int) (250 + 0.2 * i));
}
initialDrawing = false;
}
for (Card card : deck) {
card.paintCard(g);
}
This however suggests, that the initialisation should be done elsewhere, maybe in the constructor. In general painting code should do just painting and may be called several times.