How to update Image Icons for Java buttons

Does anyone know how to update the Image Icons button?

The problem is this:

I make checkers and create a graphical interface. Everything works, including AI, except that I named my AI after I placed the move, as a result, the buttons did not have a check on it.

  • Press button
  • Click on a specific button where there is a checker
  • Click on the next place where the checker should go
  • Call AI immediately to make a move.

My buttons have ImageIcons of checkers pictures, when I click on the next place where I need to check the checker, JButton on JPanel is not updated at this moment, but waits for the AI ​​to make a move and finish its movement, as a result of which I can not see where my checker is gone.

When 3-4 calls one after another, I see only the result that AI did, but not mine, because everything is updated after it leaves the actionListener inventory.

I tried calling: repaint (); double-check (); void (); On a JPanel that contains buttons.

to step 4 so that the user can see what he / she posted before the AI ​​makes a move.

else
{
     //This is where the code starts
     if ("White".equals(Red_Or_White.getText()))
     {
          //Meaning that it is white turn, then
          playerPlaysAMove(x, y, goingToGo_x, goingToGo_y);
     }
     if ("AI".equals(AI_Enabled.getText()))
     {
          //AI is enabled
          AIMoves(board, "Red");
          //the AI needs to play the position as if the AI was a red player, 
          //because the player must be white
     }
}

Both AIs play in the same round, but AI calculations take about 1 minute, and at that time the player’s turn is not displayed until the AI ​​plays, because it updates all the buttons after exiting the Listener action (after else).

Board is an 8x8 button array that fits on a JPanel

static void playerPlaysAMove(int save_x, int save_y, int moveTo_x, int moveTo_y)
{
    if(save_x - moveTo_x == 1 || save_x - moveTo_x == -1)
    {
        board[moveTo_x][moveTo_y].setIcon(board[save_x][save_y].getIcon());
        board[save_x][save_y].setIcon(null);
    }
    else if (save_x - moveTo_x == 2 && save_y - moveTo_y == 2)
    {
        board[save_x-1][save_y-1].setIcon(null);
        board[moveTo_x][moveTo_y].setIcon(board[save_x][save_y].getIcon());
        board[save_x][save_y].setIcon(null);
    }
    else if (save_x - moveTo_x == 2 && save_y - moveTo_y == -2)
    {
        board[save_x-1][save_y+1].setIcon(null);
        board[moveTo_x][moveTo_y].setIcon(board[save_x][save_y].getIcon());
        board[save_x][save_y].setIcon(null);            
    }
    else if (save_x - moveTo_x == -2 && save_y - moveTo_y == 2)
    {
        board[save_x+1][save_y-1].setIcon(null);
        board[moveTo_x][moveTo_y].setIcon(board[save_x][save_y].getIcon());
        board[save_x][save_y].setIcon(null);            
    }
    else if (save_x - moveTo_x == -2 && save_y - moveTo_y == -2)
    {
        board[save_x+1][save_y+1].setIcon(null);
        board[moveTo_x][moveTo_y].setIcon(board[save_x][save_y].getIcon());
        board[save_x][save_y].setIcon(null);            
    }        
}

, X Y . , , .

X Y 1, , , , X Y 2, , . X Y- , , , ​​ Icon X Y .

, , , , imageIcon .

AI , , AI X Y, .

, , AI , . , AI ? , , , AI [/p >

+3
1

board[save_x][save_y].repaint();

+1

All Articles