Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How can I change the color of the jButtons in my loadPlayerGrid method from my p

ID: 3872418 • Letter: H

Question

How can I change the color of the jButtons in my loadPlayerGrid method from my placePlayerShip method?

private void placePlayerShip(int[][] onBoard, int shipSize, int shipNum, int[] coords) {
        int column;
        int row;
        column = coords[0];
        row = coords[1];

        for (int i = 0; i < shipSize; i++) {
            playerBoard[column][row + i] = shipNum;


            ***** want to change jButton "square" to a different color here *****
          
          
          
    }

    private void loadPlayerGrid() {

        playerGridPanel = new JPanel();

        int[] coordinates;
        coordinates = new int[2];

        //creating player ships
        ShipPiece playerBattleship = new ShipPiece(ShipType.BATTLESHIP);
        ShipPiece playerCruiser = new ShipPiece(ShipType.CRUISER);
        ShipPiece playerSubmarine = new ShipPiece(ShipType.SUBMARINE);
        ShipPiece playerDestroyer = new ShipPiece(ShipType.DESTROYER);
        ShipPiece playerCarrier = new ShipPiece(ShipType.CARRIER);

        playerGridPanel.setBorder(new TitledBorder(new EtchedBorder()));
        GridLayout layout = new GridLayout(0, 10);
        layout.setHgap(0);
        layout.setVgap(0);
        int squareSize = 30;
        playerGridPanel.setLayout(layout);

        // Player Grid code
        int column, row;
        for (row = 0; row < 10; row++) {
            for (column = 0; column < 10; column++) {
                String boardPOS = String.format("%s%s", asChar(column), row + 1);
                JButton square = new JButton(boardPOS);
                square.setPreferredSize(new Dimension(squareSize, squareSize));
                square.setMaximumSize(new Dimension(squareSize, squareSize));
                int thisColumn = column;
                int thisRow = row;

                square.addActionListener(new ActionListener() {

                    // On button press, the coordinates of that button are returned to be used with other methods
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        coordinates[0] = thisColumn;
                        coordinates[1] = thisRow;
                        returnButtonCoordinates(coordinates);
                        System.out.println(boardPOS);

                    }
                });

                playerGridPanel.add(square);
            }
        }

        //placing ships
        // placePlayerShip(opponentBoard, aiCarrier.getSize(), 1);
        playerGridPanel.setBorder(new TitledBorder(new EtchedBorder()));

    }

Explanation / Answer

ANSWER::

public static void showBoard()
{
JFrame frame2 = new JFrame("Go Board");
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
for(int i = 19*19; i > 0; i--)
{
JButton firstButton = new JButton("");

firstButton.setBackground(Color.blue);
firstButton.setVisible(true);
firstButton.setContentAreaFilled(true);
firstButton.setOpaque(true);

firstButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
System.out.println("ddsd");
}
});
}
});
frame2.getContentPane().add(firstButton);
}
frame2.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
frame2.setLayout(new GridLayout(19,19));
frame2.pack();
frame2.setVisible(true);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote