I know that this question has been asked many times in a similar way on this platform. I've already spent a few hours trying the answers on my code (Threats and much more), but nothing has worked for me so far. So I'll ask the question here again specifically for my situation. I am currently programming a card game and would like to flip a coin before a card is played. I have already animated this as a gif and the coin toss logic works too. Now I want to get the animation into my program. For this I wrote a while loop which runs for 2 seconds. In this I would like to repaint, but the paint method (like many others had the problem before) is not called. This is my code in my public boolean cointoss() method for it:
cointossed = true;
long startTime = System.currentTimeMillis();
long elapsedTime = 0L;
while (elapsedTime < 2*1000) {
//Used this beacuse I thought my PC isnt fast enough
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
paintImmediately();
elapsedTime = (new Date()).getTime() - startTime;
}
cointossed = false;
In my paint() method there is the following Code, to draw the gif:
if(cointossed) {
g.drawImage(gif, 20, 20,100 , 100, null);
}
Somebody has an idea for my specific case?