Redrawing the board in Java Swing

jdk94 :

I'm trying to implement a game. It takes place on a 9 by 9 board and contains Walls and one Pawn and Empty blocks. Both of these are a subclass of GameObject. GameObject extends JLabel. I store the location in an array:

    GameObject[][] board = {
        {null,null,null,null,null,null,null,null,null},
        {null,null,null,null,null,null,null,null,null},
        {null,null,null,null,null,null,null,null,null},
        {null,null,null,null,null,null,null,null,null},
        {null,null,null,null,null,null,null,null,null},
        {null,null,null,null,null,null,null,null,null},
        {null,null,null,null,null,null,null,null,null},
        {null,null,null,null,null,null,null,null,null},
        {null,null,null,null,null,null,null,null,null},
};

For every place in the array I check which GameObject has to be placed there from a String and put it there. So that means nothing will get put there(Empty), a Wall or a Pawn. To add the GameObject(extending JLabel) to my JPanel I use the following code:

for(int i = 0; i < 9; i++)
        {
            JPanel row = new JPanel(new GridLayout(1,9));
            for(int j = 0; j < 9; j++)
            {
                GameObject col = board[i][j];
                col.setHorizontalAlignment(JTextField.CENTER);
                col.setFont(new Font("SansSerif",Font.BOLD,20));
                col.setOpaque(true);
                col.setBackground(board[i][j].getColor());

                col.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                row.add(col);
                int finalI = i;
                int finalJ = j;
            }
            row.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            panel.add(row);
        }

Once the user requests a move the Pawn will be placed on a different spot on the board. So the current place in the array is set to Empty and the new place is set to Pawn. Then the two squares have to be redrawn. An empty JLabel has to be drawn and the new position for the Pawn has to be drawn. This is done in the following way:

board[pawn.getRow()][pawn.getCol()] = new Empty(board); //Old place is now empty
board[pawn.getRow()][pawn.getCol()].setBackground(Color.WHITE); //Set the old place color to white
pawn.movePawn(rowMove, colMove); //Move the pawn internally
board[pawn.getRow()][pawn.getCol()] = pawn; //Move the pawn on the board
board[pawn.getRow()][pawn.getCol()].setBackground(Color.BLUE); //Set the color to blue

Unfortunately nothing happens when I call the last bit of code. The array does get changed, the internal coordinates of the Pawn change. But in the JPanel nothing happens. How do I make sure that, every time I call the move function, my JPanel gets updated to reflect the change?

matt :

Ill make your example a little simpler.

JLabel[][] labels = new JLabel[2][2];

Now there is a 2D array with space for JLabels.

JPanel panel = new JPanel(new GridLayout(2, 2));
for(int i = 0; i<labels.length; i++){
    for(int j = 0; j<labels[i].length; j++){
        labels[i][j] = new JLabel(i + ", " + j);
        panel.add(labels[i][j]);
    }
}

Now I have filled the array with JLabels and added those same JLabels to the panel.

labels[0][0].setText("XXX");

That changes the JLabel in my array to have the text of XXX, it is also a JLabel that I added to my JPanel, so the changes will be reflected on screen.

labels[0][1] = new JLabel("XXX");

That changes the array to point at a new JLabel, it does not affect the JLabel that is in the JPanel. You will see no changes on the screen.

So what you have done:

board[pawn.getRow()][pawn.getCol()] = new Empty(board);
board[pawn.getRow()][pawn.getCol()].setBackground(Color.WHITE);

The array has a new Empty, the Object in the panel has not been changed at all. Then you set the background color of the new Empty, but it is not displayed anywhere.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=335361&siteId=1