vendredi 8 mai 2015

Repaint method isn't working in time

I'm making a game in Java, and the repaint() method is misbehaving in a very odd way. Basically, I need to repaint the board before getting the AI's move. Here is my method where I'm calling it:

public void mouseClicked(MouseEvent e) {
    mouseX = e.getX();
    mouseY = e.getY();

    int button = e.getButton();
    if (button == MouseEvent.BUTTON1 || button == MouseEvent.BUTTON3) {
        int mouseR = (mouseY / SIZE);
        int mouseC = (mouseX / SIZE);

        if (mouseR >= 0 && mouseC >= 0 &&
                mouseR < board.length && mouseC < board[0].length) {
            if (board[mouseR][mouseC] == 3) {
                makeMove(mouseR, mouseC, playerColor);
                getValidMoves((playerColor % 2) + 1);
                repaint();
                getAIMove();
            }
        }

    }
    repaint();
}

getAIMove() is designed so it waits for a couple of seconds before making the move, so the player can see the results of their move before they see those of the AI. Unfortunately, although I call repaint() before getAIMove(), what happens is more like this: the player clicks where they want to move. Nothing appears to happen for a few seconds, then the results of the player's move AND the AI's move are displayed on the screen. What's going on?

Aucun commentaire:

Enregistrer un commentaire