This is the problematic piece of code:
while(playgame == true)
{
if(board.isVisible() == false)
{
board.setVisible(true);
}
}
When called from the action listener, you're in the event dispatch thread. That code runs in a loop when playgame
is true, never letting control back to the EDT so that it could do the drawing.
You main should look something like this instead:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Create board here, if needed
createBoard();
board.setVisible(true);
// start a new game here
startGame();
}
});
}
invokeLater()
pushes the code to the event dispath thread, which is required for thread safe access and creation of swing components, so don't skip that even if your code appears to work without it.
Also, it might be cleaner if you simply did not call main()
from the action listener at all, but just the method that starts a new game.