简体   繁体   English

在 Java Swing 中重绘板

[英]Redrawing the board in Java Swing

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.它发生在一个 9 x 9 的棋盘上,包含墙和一个 Pawn 和 Empty 块。 Both of these are a subclass of GameObject.这两个都是 GameObject 的子类。 GameObject extends JLabel. GameObject 扩展了 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.对于数组中的每个位置,我检查哪个 GameObject 必须从 String 放置在那里并将其放在那里。 So that means nothing will get put there(Empty), a Wall or a Pawn.所以这意味着什么都不会放在那里(空),墙或 Pawn。 To add the GameObject(extending JLabel) to my JPanel I use the following code:要将 GameObject(扩展 JLabel)添加到我的 JPanel,我使用以下代码:

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.一旦用户请求移动,Pawn 将被放置在棋盘上的不同位置。 So the current place in the array is set to Empty and the new place is set to Pawn.因此,数组中的当前位置设置为 Empty,新位置设置为 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.必须绘制一个空的 JLabel 并且必须绘制 Pawn 的新位置。 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.数组确实发生了变化,Pawn 的内部坐标发生了变化。 But in the JPanel nothing happens.但是在 JPanel 中没有任何反应。 How do I make sure that, every time I call the move function, my JPanel gets updated to reflect the change?我如何确保每次调用移动函数时,我的 JPanel 都会更新以反映更改?

Ill make your example a little simpler.我会让你的例子更简单一点。

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

Now there is a 2D array with space for JLabels.现在有一个带有 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.我现在已经充满的JLabel阵列加入这些相同的JLabel到面板上。

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.这将我数组中的 JLabel 更改为具有 XXX 文本,它也是我添加到 JPanel 的 JLabel,因此更改将反映在屏幕上。

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.将数组更改为指向新的 JLabel,它不会影响 JPanel 中的 JLabel。 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.数组有一个新的Empty,面板中的Object根本没有改变。 Then you set the background color of the new Empty, but it is not displayed anywhere.然后你设置了新的Empty的背景颜色,但它没有显示在任何地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM