Gearbox
Gearbox

Reputation: 64

Action listener actionPerformed does through code double

I have been grindig my gears over this one for a while now, but have not been able to solve it. In this memorygame, every time you choose a button and then the upper left one (the first time that button is chosen) the upper left button goes green. This is because the code in public void actionPerformed(ActionEvent e) gets executed twice whenever you have chosen the upper left button for the first time and as second choice. Any ideas what it is that might cause this and how do I stop it from happening?

I am sure there are a lot of ways to better create a memorygame, but I hate moving on without knowing what went wrong.

I appreciate your help.

All the code:

public class Alt3_3 extends JFrame implements ActionListener {

    JButton button[] = new JButton[52];
    String[] kort = {"POTATIS", "GLASS", "UNIX", "GLAS", "FOSTERS", "AIGH",
        "VAT 69", "SPIK", "FREDAG", "SITS", "FEST", "DaTe", "ALBIN",
        "42", "BOTTLE", "SANDELS", "DEW", "STOL", "PETSKI", "LAGER", "STOUT",
        "MALT", "EN RUTA", "BASS", "PrtScr", "DEL"};
    String[] svar1;
    String[] svar2;
    boolean firstVald;
    boolean green;
    int score = 0;
    int progress = 0;
    int index1;
    int index2;
    String svark1;
    String svark2;

    Alt3_3() {
        List<String> list1 = Arrays.asList(kort);
        Collections.shuffle(list1);
        svar1 = list1.toArray(new String[0]);
        List<String> list2 = Arrays.asList(kort);
        Collections.shuffle(list2);
        svar2 = list2.toArray(new String[0]);

        setLayout(new FlowLayout());
        setPreferredSize(new Dimension(650, 700));
        setTitle("Memorygame");

        for (int i = 0; i < button.length; i++) {
            button[i] = new JButton("");
            button[i].setPreferredSize(new Dimension(100, 50));
            add(button[i]);
            button[i].addActionListener(this);
        }

        pack();
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {


        if (firstVald == false) {
            resetRed();
            firstVald = true;

            int index = -1;
            for (int i = 0; i < button.length; i++) {
                if (e.getSource() == button[i]) {
                    index = i;
                    break;
                }
            }
            index1 = index;

            if (index % 2 == 1) {
                button[index].setText(svar1[(index - 1) / 2]);
                svark1 = svar1[(index - 1) / 2];
            } else {
                button[index].setText(svar2[index / 2]);
                svark1 = svar2[index / 2];
            }
            button[index1].removeActionListener(this);

        } else {


            int index = -1;
            for (int i = 0; i < button.length; i++) {
                if (e.getSource() == button[i]) {
                    index = i;
                    break;
                }
            }
            index2 = index;

            if (index % 2 == 1) {
                button[index].setText(svar1[(index - 1) / 2]);
                svark2 = svar1[(index - 1) / 2];
            } else {
                button[index].setText(svar2[index / 2]);
                svark2 = svar2[index / 2];
            }

            if (svark1 == svark2) {
                progress++;
                green = true;
                button[index1].setBackground(Color.green);
                button[index2].removeActionListener(this);
                button[index2].setBackground(Color.green);
            } else {
                green = false;
                score++;
                button[index2].removeActionListener(this);
                button[index1].setBackground(Color.red);
                button[index2].setBackground(Color.red);

            }
            firstVald = false;

        }
        if (progress > 26) {
            showMessageDialog(null, "grattis" + Integer.toString(score));
            filhant.highScore(score);
            System.exit(0);
        }
    }

    public void resetRed() {
        if (green == false) {
            button[index1].setBackground(null);
            button[index2].setBackground(null);
            button[index1].setText("");
            button[index1].addActionListener(this);
            button[index2].setText("");
            button[index2].addActionListener(this);
        }
    }

    public static void main(String[] args) {
        new Alt3_3();
    }
}

Upvotes: 0

Views: 679

Answers (1)

MvG
MvG

Reputation: 60858

For the first of two buttons, you always call resetRed. And resetRed will addActionListener to the buttons indicated by index1 and index2. Since these are initialized to zero, you have three copies of the action listener installed, and the codes gets executed not only twice but thrice: the first as the second element of an unmatched pair, and the second and third as the two elements of a matched pair. Avoid calling resetRed the very first time.

Upvotes: 1

Related Questions