-1
private JButton buttons[][] = new JButton[4][4];
int i, j, n, index = 0, calc = 0;
public int open = 0;
private JButton  opens[] = new JButton[1];
public ImageIcon  images[] = new ImageIcon[20];

public Concentration()
{
    super ("Concentration");

    JFrame frame = new JFrame();
    setSize(1000, 1000);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(new GridLayout(4, 4));
    panel.setSize(400, 400);
    // Getting images
    for (i = 0; i < 8; i++) {
        images[i] = new ImageIcon(getClass().getResource("/images/1 ("+i+").jpg"));
    }

    // Copying images for game
    for (i = 8; i < 16; i++) {
        images[i] = images[i - 8];
    }

    // Shuffling
    Random random = new Random();
    int k;
    ImageIcon imageTemp;

    for (i = 0; i < 16; i++) {
        k = random.nextInt(16);
        imageTemp = images[i];
        images[i] = images[(i + k) % 16];
        images[(i + k) % 16] = imageTemp;
    }

    for (i = 0; i < buttons.length; i++) {
        for (j = 0; j < buttons[i].length; j++) {
            n = i * buttons.length + buttons[i].length;
            buttons[i][j] = new JButton();
            buttons[i][j].setIcon(null);
            /*
             * I made null instad of putting images because if i put images,
             * at start it shows all. but how will i take parameters to
             * actionlistener? for comparing if same?
             */
            //images[i*buttons.length+j]
            panel.add(buttons[i][j]);
            buttons[i][j].addActionListener(this);
        }
    }
    add(panel);
    pack();
    setVisible(true);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() instanceof JButton) {
        JButton pressedButton = (JButton) e.getSource();
        if (pressedButton.getIcon() == null) {
            pressedButton.setIcon();
            // How will it take from array? another class?
        } else {
            pressedButton.setIcon(null);
        }
    }
}

I want to make a memory game. when clicked 2 images, they will show to user (at first all of them are null). But in ActionListener how can I take buttons[i][j] i j variables because I need that index to compare if they are same. I need images' places to hold and compare 2 image. I need to access images[] array to compare I guess but how can I use in an ActionListener?

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
clara sampson
  • 11
  • 1
  • 7
  • 1
    Possible duplicate [Addactionlistener doesnot take parameter](http://stackoverflow.com/questions/18958606/addactionlistener-doesnot-take-parameter) – dic19 Sep 23 '13 at 21:16
  • Have you tried to just use images? since your class is the action listener itself then you can use the images inside your actionPerformed, or am I missing something? – porfiriopartida Sep 23 '13 at 21:16
  • 1
    What you need is some way to know which two cards have being flipped/selected by the user. One approach is use some kind of `Map`, keyed to the `JButton` which contains the values that need to compared. When the `actionPerformed` method is triggered, you would look up this map and get the value from it. If you had two values, you would then be able to compare them. You could also check out [this example](http://stackoverflow.com/questions/16292498/swingworker-thread-sleep-or-javax-swing-timer-i-need-to-insert-a-pause/16293498#16293498) for some ideas – MadProgrammer Sep 23 '13 at 21:20
  • images doesnot work because it cant its index in action.that example is really hard:) – clara sampson Sep 23 '13 at 21:29

1 Answers1

1

Your action listeners currently determine which button has been clicked. The buttons themselves have absolutely no way to determine their index. Thus, you need to give the buttons a way of identifying their position in an array of buttons. For this, I recommend that you extend the button class by adding the following lines to it:

MemButton extends JButton {
    private int[] position = new int(2);

    getPosition(int index) {
        if (index >= position.length || index < 0) {
            return null;
        } else {
            return position[index];
        }
    }

    setPosition(int index, int value) {
        if (index >= position.length || index < 0) {
        } else {
            position[index] = value;
        }
    }

}

By doing that, you can tell the button where it is. When a button is clicked, actionPerformed would call pressedButton.setIcon and perform a calculation to convert the new pressedButton.getPosition(0) and pressedButton.getPosition(1) to fetch the corresponding image in your images array. Just remember that you'd be using MemButton as a type (or whatever else you name it as).

I'm sure that you can modify my code as you see fit. Also, because you are extending JButton, MemButton is exactly like a JButton, aside from having a useful way of storing its position. I would prefer not to hack together a solution like this, especially since it will be a pain to make sure each button has the right position stored (especially if you move buttons around a lot or add more). Nonetheless, for a smaller program, this solution is fine. For a larger one, I would do my best to make the game itself independent of the input (the images) and the output (the buttons), so that you could re-use the code and only write a little bit of code to adjust to any GUI or data to compare.

I hope that helps ;)

P.S. You might have to move the new int(2) inside of the constructor for JButton. To do that, you would make the following changes:

MemButton extends JButton {
    private int[] position;    

    MemButton() {
        super();
        position = new int(2);
    }
\\The rest of the example

You may need to fool around a bit to get it set up the way you'd like. Remember that when you are setting up you array, you need to tell each button where it is with MemButton.setPosition(0, i); MemButton.setPosition(1, k);

Modify the code if you'd like it to be simpler to set the position.

Aarowaim
  • 801
  • 4
  • 10
  • By the way, I just did a bit more research and discovered that JButton inherits a method from JComponent called `putClientProperty(Object key, Object value)`, which basically does the same thing. The value may then be retrieved via `getClientProperty(Object key)`. Sometimes reading documentation is useful ;) – Aarowaim Sep 25 '13 at 02:14