Randomly changing background of JButtons in array? : <p>I'm currently attempting to create a Battleship game. The part I'm having trouble with is when I select the "Randomize" button, I would like it to put the ships at a random part of the grid.</p>

<p>For example, the Carrier ship would cover 5 JButtons within the array. How do I randomly select 5 JButtons within the array that are next to each other?</p>

<pre><code>import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.util.Random;

public class View
{
    private JFrame frame;
    private JPanel panel1;
    private JPanel panel2;
    private JButton grid1[][];
    private JButton randomize;
    private String[] alphabet = { "", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
    private String[] numbers = { "", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };

    public View()
    {
        configureFrame();
        configurePanels();
        configureComponents();
        frame.setVisible(true);
    }
    private void configureFrame()
    {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(900, 600);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
    }
    private void configurePanels()
    {
        panel1 = new JPanel();
        panel1.setLayout(new GridLayout(11, 11));
        frame.getContentPane().add(panel1, BorderLayout.WEST);

        panel2 = new JPanel();
        panel2.setSize(frame.getWidth(), frame.getHeight());
        panel2.setLayout(null);
        frame.getContentPane().add(panel2);
    }

    private void configureComponents()
    {
        grid1 = new JButton[11][11];
        for(int i = 0; i &lt; grid1.length; i++)
        {
            for(int j = 0; j &lt; grid1[i].length; j++)
            {
                grid1[i][j] = new JButton();
                panel1.add(grid1[i][j]);
            }
        }
        for(int i = 0; i &lt; alphabet.length &amp;&amp; i &lt; numbers.length; i++)
        {
            grid1[0][i].setText(alphabet[i]);
            grid1[i][0].setText(numbers[i]);
            grid1[0][i].setFont(new Font("Tahoma", Font.BOLD, 19));
            grid1[i][0].setFont(new Font("Tahoma", Font.BOLD, 19));
            grid1[0][i].setEnabled(false);
            grid1[i][0].setEnabled(false);
            grid1[0][i].setBackground(Color.BLACK);
            grid1[i][0].setBackground(Color.BLACK);
        }
        grid1[0][0].setBackground(Color.BLACK);

        randomize = new JButton("Randomize");
        randomize.setLocation(50, 65);
        randomize.setSize(100, 50);
        randomize.setFocusPainted(false);
        randomize.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                /*
                        Want to randomly put the carrier on the grid somewhere
                grid1[2][3].setBackground(Color.LIGHT_GRAY);
                grid1[3][3].setBackground(Color.LIGHT_GRAY);
                grid1[4][3].setBackground(Color.LIGHT_GRAY);
                grid1[5][3].setBackground(Color.LIGHT_GRAY);
                grid1[6][3].setBackground(Color.LIGHT_GRAY);
                */
            }
        });
        panel2.add(randomize);
    }
}
</code></pre>

<p>I'm very new to Java, so I apologize in advance if I'm not making any sense.</p>
