Java GUI着色gridLayout上的特定网格位置

时间:2017-09-28 07:29:15

标签: java swing user-interface

我正在写国际象棋程序,我正在创建国际象棋的GUI部分。

要做到这一点

我制作了一个BoardGUI类和一个TileGUI类。

BoardGUI初始化GridLayout - >循环遍及整个网格 - >在每个网格上初始化TileGUI对象。

TileGUI基本上获取gameBoard的信息 - >根据瓷砖的位置(深色或浅色)涂抹瓷砖 - >将Piece绘制为并将其添加为JButton对象。

这基本上初始化了我的棋盘GUI(下面的代码)。

我的问题是,当我尝试突出显示可移动的瓷砖时,我正在尝试:

获取所选板的板信息和x,y位置作为参数 - >循环遍历整个电路板 - >检查当前图块位置是否可移动 - >的setBackground(彩色)

但是当我这样做时,它只会对棋子当前位置的瓦片进行着色,而不是可移动位置的瓦片。

private class BoardGUI extends JPanel {

        BoardGUI() {

            super(new GridLayout(NUM_OF_ROWS, NUM_OF_COLS)); //Place tile on each 

            for(int row = 0; row < NUM_OF_ROWS; row++) {
                for(int col = 0; col < NUM_OF_COLS; col++) {
                    TileGUI tilePanel = new TileGUI(this, new Point(row, col));
                    add(tilePanel);
                }
            }

            // Invoked when container's subcomponents are modified (added or removed from the container)
            validate();
        }
    }

和TileGUI

private class TileGUI extends JPanel {

        private Point tileCoord;

        TileGUI(final BoardGUI boardPanel, final Point position) {

            super(new GridLayout());
            this.tileCoord = position;

            paintTile();

            placePieceGraphic(boardSetting);

            validate();

        }


        private void placePieceGraphic(Piece[][] boardSetting) {

            // Adds an empty JButton when the tile is empty
            if( boardSetting[tileCoord.x][tileCoord.y] == null) {
                JButton pieceButton = new JButton();

                pieceButton.setOpaque(false);
                pieceButton.setContentAreaFilled(false);
                pieceButton.setBorderPainted(false);
                add(pieceButton);

                pieceButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        JOptionPane.showMessageDialog(null, "empty tile");
                    }
                });

            }

            if( boardSetting[tileCoord.x][tileCoord.y] != null) {

                // Retrieves the first letter of the class name

                // Retrieves the team name: UP or DOWN

                try {
                    BufferedImage image = ImageIO.read(filename + ".png"));
                    JButton pieceButton = new JButton(new ImageIcon(image));

                    add(pieceButton);
                    pieceButton.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            highlightValidMovement(getMovablePositions(boardSetting, tileCoord.x, tileCoord.y));   // This tries to highlight the movable tiles

                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }


        private void paintTile() {

            boolean isBrightTile = (this.tileCoord.x % 2) == (this.tileCoord.y % 2);
            boolean isDarkTile = (this.tileCoord.x % 2) != (this.tileCoord.y % 2);

            if(isBrightTile) setBackground(darkColor);
            if(isDarkTile) setBackground(brightColor);


        }

        public ArrayList<Piece> getMovablePositions(Piece[][] piece, int x, int y) {

            ArrayList<Piece> movableList = new ArrayList<>();

            for(int row = 0; row < NUM_OF_ROWS; row++) {
                for(int col = 0; col < NUM_OF_COLS; col++) {
                    if(piece[x][y].canMove(piece, x, y, row, col)) {

                        movableList.add(piece[row][col]);
                    }
                }
            }

            return movableList;

        }

        public void highlightValidMovement(ArrayList<Piece> movablePos) {

            for(Piece pos : movablePos) {
                TileGUI tilePanel = new TileGUI(boardImage, new Point(pos.getX(), pos.getX()));
                setBackground(highlightColor);
                add(tilePanel);

            }

        }

    }

隐藏不重要的代码。

这导致

enter image description here

而不是

enter image description here

0 个答案:

没有答案