Updating GUI as while loop runs
807599Mar 8 2007 — edited Mar 9 2007Hello,
Been learning Java and have decided to put my knowledge to the test and write a program.
I'm writing a Tic Tac Toe game, everything has worked till this point. The player can play against another play or the computer, I thought it would be cool to recreate the sceen from the 1983 War Games movie where the hacker teaches the computer to play Tic Tac Toe to stop an all out nuclear war.
The problem is when I pit the CPU against the CPU I do so in a while loop, the second I set the loop going the screen never updates again while the processor maxes out playing head to head at break neck speed.
Eventually an overflow occurs as my program tries to update JButtons with images at thousands of times a second. I'm not sure how to progress, do I synchronise thread? Not that I've programmed this with threads in mind. Do I need to set some kind of timing in the loop? I've told the thread to sleep a while but the GUI still does not update and the overflows just take longer to happen.
Everything was going so well till this point.
FYI the game board if a JPanel with 9 JButtons, normally the player presses where they want to play, the CPU updates it own buttons with the correct image. The CPU works playing against itself if set to a finite amount of moves by replacing the while loop with a for loop, or if I do not let it restart the game each time it finishes. However the GUI only shows the updates at the end.
Any guidence please, I'm good at reading up on what I need to know I just need to know what I need to know.
Thanks,
BushStar