Search code examples
javaswinguser-interfaceevent-dispatch-thread

Java Swing GUI Freezing When Calling Method


I'm pretty new to Swing, but not to Java. Anyway, I have a chess program. Here's the part of the game loop class that matters:

public class game {
    static Board gameboard = new Board();
    public static boolean waitstatus = true;
    public static char currentfaction = 'w';
    public static boolean done = false;
    public static boolean resigned = false;
    public static boolean wplayern = false;
    public static boolean bplayern = false;
    public static gui boardwindow = new gui();

    public static void main(String[] args) {
        gameboard.init(8, 8);
        gui.startGUI(boardwindow);
        runner();
        while (true) {
            //spin
        }

    }

    public static void runner() {
        done = false;
        resigned = false;
        wplayern = false;
        bplayern = false;
        boolean stalemate = false;

        while (true) {
            //The loop stuff    
        }
    }

    public static void resetGame() {
        if (!done) {
            System.out.println("Game's not finished yet!");
        } else {
            gameboard.restart();
            waitstatus = true;
            currentfaction = 'w';
            done = false;
            resigned = false;
            wplayern = false;
            bplayern = false;
            boardwindow.redraw();
            rerun();
        }
    }

    public static void rerun() {
        runner();
    }
}

So that's the code in question. By the way, boardwindow is a gui object, gameboard is my internal logic gameboard array, redraw() has the gui redraw (to update the positions of the pieces), and restart() restarts the internal logic board to the original starting gamestate. The gui class is a swing gui that is pretty simple, and works pretty well. The problem is, in the game loop, when it's checkmate, the loop breaks and the runner() function ends. The user should then be able to press the new game button in the gui, to have the game restart after the checkmate. The new game button calls resetGame(). The problem is that the gui window freezes when runner() is called again, and I'm not sure why, it wouldn't need multiple threads for this, I thought. I have other buttons which call functions that I have not shown you, and those functions call other functions like restart() and such, and they have no problem, its only when runner() is called.

I know my other code is probably awful in formatting and convention, but please try to keep answers focused on the main problem.

Thanks


Solution

  • You should put a condition to control the loop rather than set it to true. Here the loop keeps running even after resetting the game from resetGame(); or use break;

    public static void runner() {
        done = false;
        resigned = false;
        wplayern = false;
        bplayern = false;
        boolean stalemate = false;
    
        while (true) {
            //The loop stuff 
            if(newGameClicked == true){//just to literally make meaning
                break;
            }
        }
    }
    

    Or as a quick fix you could simply just call boardwindow.dispose(); and then create a new instance of gui() and set it visible() to restart the game when the new game button is clicked