0

I am trying to use a 2D array of buttons that will be randomly generated in order, however, they do different things. Is there any way to use an action listener to do different things when clicking different buttons of the same 2d array? I want to keep 1 array because the buttons are on in a grid and it will be much simpler to do what I need to later If I can just use the coordinate of a button to change its color depending on what button is currently pressed. maybe there is a solution with grid layout where I can use coordinates of that or something? That way I could use multiple 2d arrays but then I need to have the different arrays randomly spread into the grid.

public class MyFrame extends JFrame implements ActionListener 
    {
        JFrame mainWindow;
        JLabel label = new JLabel();
        private JButton[][] board = new JButton[8][8];
        
        MyFrame()
            {   
                int[] piece = {1,1,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6};
                this.setSize(520,540);//setting main frame attributes
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setResizable(false);
                this.setLayout(new GridLayout(8,8));                
                this.setPreferredSize(new Dimension(500,500));
                this.setLayout(new GridLayout(8,8));
                
                int a = 0;
                
                  for(int r = 0; r < 8; r++)
                    {
                        for(int c = 0; c < 8; c++)
                            {
                                board[r][c] = new JButton();
                                
                                board[r][c].addActionListener(this);
                                
                                board[r][c].putClientProperty("Type",1);
                                
                                this.add(board[r][c]);  
                            }
                    }                 
                 setColor();
                 setVisible(true);
            }
        public void setColor() 
            {
                int a = 0;
                for(int r = 0; r < 8; r++)
                    {
                        for(int c = 0; c < 8; c++)
                            {
                                if(a % 2 == 0)
                                    {
                                        board[r][c].setBackground(Color.BLACK);
                                    }      
                                a++;    
                                
                                if(c == 7) 
                                    {
                                        a++;
                                    }  
                            }
                    }
            }
        @Override
        public void actionPerformed(ActionEvent e) 
            {
            if(e.getSource() == board.getClientProperty("Type",1)) 
                {//if button pressed create instance
                
                    peicePopup newWindow = new peicePopup(); 
                
                }
            
            }
    }

here are my instructions

Using good OOP and GUI-based development, create a chessboard. A sample chessboard is shown.

Each player has two Rooks, two Knights, two Bishops, a King, a Queen, and eight Pawns. Player One has white pieces while Player Two has black pieces. To show the pieces, you can either use icons (you can research chess piece images on the web) or use letters. For the latter, you could use K for King, Q for Queen, R for Rook, B for Bishop, KN for Knight, and P for Pawns.

Initially, across the chessboard, place all the chess pieces, i.e., the black and white pieces, using a randomization. That is, when the program is run, all pieces are placed in a random order across the chessboard. Every run of the program will randomize the initial placement of the chess pieces.

Each square of the chessboard should be a button that is actionable. When any empty square is clicked, a second window should open containing textual instructions on the movement of all the pieces of the chess game. The user should be able to close the second window without closing the first window, i.e., one displaying the chessboard.

For any of the chess pieces, when its square (button) is clicked, highlight the possible spots that piece can legally move to. A piece can only move to an empty square and as per the rules of the game. When highlighting the possible spots a piece can move to, use a different color for the different chess pieces. When another piece’s square is clicked the previously clicked piece’s possible movement positions are unhighlighted, while the possible spots of the newly clicked piece are highlighted. That is, the program highlights the possible spots for only one piece (when clicked) at a time on the chess board.

  • 1
    See: https://stackoverflow.com/questions/70340694/how-to-randomize-placement-of-a-jlabel-on-a-gridlayout-of-jbuttons for a similar question asked a couple of days ago. – camickr Dec 16 '21 at 01:00

1 Answers1

0

nvm, I got it I just made a separate array with values that differentiate the buttons bc they are in the same order.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 17 '21 at 04:11