Search code examples
javaswingjbuttonjradiobutton

How to stop buttons that have been clicked from changing their colour after a certain number of colored buttons


I created a battleship GUI program, with a grid of buttons then I created a bunch radio buttons to select which ship the user wants to place on the grid once the user selects a radio Button they are able to change the colour of the buttons on the grid. i.e to position the ship

my problems is once the user selects a radio buttons they can change the colour of the grid button as much as they like.

I also want them to be able only change the colour of the buttons close to the one they initially selected( only vertically and horizontally)

Here's my code

public void actionPerformed(ActionEvent evt){
if(submarine.isSelected())
{
button[r][c].setBackground(Colour.green)

board[r][c]=+1
}

button array stores the position of the current button being used while board is supposed to store the position of the ship

edit: When I run the program I have a grid representing the board. then I made a Group 4 radio buttons representing the Ships( submarine, cruiser, destroyer, and battleship) and another group for the orientation (vertical or horizontal)

if the player chooses a radio button for example "submarine (which has a length of 4) and vertical" the button pressed and the next 3's colour changes in the case to "red". Now I want the player to be able to position a ship on the grid once. in my example, after the user places the submarine (4 red buttons vertically) they should only be able to change the position i.e create a new sets of 4 red button on the grid ( if they wish to change the ships position) meaning the previously selected buttons clear up.

I also don't want the buttons to overlap on grid. for example if they select submarine and vertical after placing a the 4 red buttons indicating the submarine. if they then select a new ship like "battleship" with a length of 6 . the new buttons should not overlap with the previous one being submarine

Hope this clarifies things. if you still don't get it let me know


Solution

  • Maybe try and redesign your logic if I understand correctly:

    Right now, you choose a spot for a certain ship of a certain length. But why do you need to choose more spots for your ship?

    Instead, have two more radio buttons that will determine the orientation of the ship: one for horizontal orientation and one for vertical orientation. This way, when a player choose a spot, the buttons automatically highlight either in the horizontal or vertical direction with of course the correct amount highlighted. Example: a ship of length 4 was chosen and horizontal orientation was chosen. Now the player chooses 3, 2 (assuming 3 is the third row and 2 is the second column) on the grid. 3, 3; 3, 4; 3, 5 also highlight red along with the chosen to show where the ship will be located.

    To highlight the correct amount, you can utilize for loops. Loop for the chosen ship’s length and then increment either the column, c, if horizontally is chosen or row, r, if vertically is chosen and set all those buttons to red i.e

    Button currentButton = null;
    for(int i = 0; i<ship.length; i++){
        if(horizontalOrientation.isSelected())
            currentButton = button[selected r][selected c + i];
            if(currentButton.getMarked == true){
                currentButton.setBackground(Color.RED);
                break;
            }
            else{
                currentButton.setBackground(Color.GREEN);
                currentButton.setMarked(true);
            }
        else
            //same code but for vertical orientation (and incrementing r instead)
    }
    

    I’m sure the above code can be shrunk down from all those if else’s, but this is the main idea.