Q:

Adding/Changing variable name with an int or string in Java

I'm new to Java so I'm sure my code is abysmal in comparison to cleaner/organized code.
Link to full code: http://pastebin.com/UXJzU2ax
What it looks like when ran.
Image
I'm attempting to add a number onto the end of a variable via a number in an array. I'm unsure as to how this can be achieved.
I have created a 10x10 grid of buttons with randomly generated colors, I wish to change the adjacent buttons color on click, but only if the color is the same.
Color generation.
int[] squares;

        squares = new int[101];

        for (int i = 1; i <= 100; i++){

            squares[i] = i;
            JButton button = new JButton("" + squares[i]);

            int color = (int) (Math.random() * 4); //rand 1 to 4

            if (color == 1){

                button.setBackground(Color.red);

            } else if (color == 2){

                button.setBackground(Color.green);

            } else if (color == 3){

                button.setBackground(Color.yellow);

            } else {

                button.setBackground(Color.blue);

            } 
            button.setName("button" + (Integer.toString(squares[i])));
            button.addActionListener(new ActionListener()

            {

My way of knowing where directions are.
right = Integer.parseInt(button.getText()) + 1;
                        up = Integer.parseInt(button.getText()) - 10;
                        down = Integer.parseInt(button.getText()) + 10;

                        right2 = squares[right];
                        up2 = squares[up];
                        down2 = squares[down];

This is where I'm running into troubles.
if (btnSelectedColor.getBackground() == button.getBackground()){

                            //Where I need the change in "button", like "button[right].getBackground", or something similar.

                            if (button.getBackground() == btnSelectedColor.getBackground()){

                            }

                        }

A:

Here's how you can access the other buttons.
Note: I will not write all the logic here. I will leave the method of getting the neighbors to you.
In the actionPerformed callback, you need somehting like this:
public void actionPerformed(ActionEvent e)
{
    // get all components of the panel
    Component[] comp = panel.getComponents();

    // loop through all components
    for (int i = 0; i < comp.length; i++)
    {
        // if it's a button...
        if (comp[i] instanceof JButton)
        {
            JButton b = (JButton) comp[i];

            // if b is a neighbor of button (e.g. the one clicked),
            // then change its background color
            // I leave this logic to you (seems like you wrote most if it already)

            // Hint: Convert the text of b to an int and convert the
            // text of button to an int and compare the ints to see if 
            // they are neighbors. You can convert the int of 
            // button.getText once outside of the loop. No sense in 
            // doing the conversion every time since it will be the same.
        }

    }

    // Rest of code in your actionPerformed method...

}

Good luck!

