1

Hi I'm new to stackoverflow so bear with me if I make mistakes.

I'm making this Java Simon Says Game for a class project. It works by a random number generator for each sequence#. I show the sequence through doClick() but remove the actionlisteners beforehand and add it afterwards.

The problem is the buttons won't unpress or unarm until all other buttons have been pressed. I've tried using thread.sleep to put a delay between each if...else statements yet it only stays pressed for longer. I've tried updating the gui through repaint(), revalidate(), updateUI() within the try... catch of the thread.sleep but that didn't work either.

I've realized this issue is mainly cosmetic because when I tried implementing setPressed or setArmed it said it wasn't being pressed but it looked pressed.

Here is the code snippet in it's most simplest form without thread.sleep or my previous attempts in comments.

public void sequence2() //This is where the issue happens. The buttons won't unpress until every button has been pressed.

{
    level.setText("                                         Level 2"); //Level indicator

    Green.removeActionListener(Listener);
    Red.removeActionListener(Listener);
    Yellow.removeActionListener(Listener);
    Blue.removeActionListener(Listener);

    if(sequence1 == 1)
    {       
        Green.doClick(300); //Programmatically clicks the button
    }
    else if(sequence1 == 2)
    {
        Red.doClick(300);
    }
    else if(sequence1 == 3)
    {
        Yellow.doClick(300);
    }
    else if(sequence1 == 4)
    {
        Blue.doClick(300);
    }

    if(sequence2 == 1)
    {
        Green.doClick(300);
    }
    else if(sequence2 == 2)
    {
        Red.doClick(300);
    }
    else if(sequence2 == 3)
    {
        Yellow.doClick(300);
    }
    else if(sequence2 == 4)
    {
        Blue.doClick(300);
    }

    Green.addActionListener(Listener);
    Red.addActionListener(Listener);
    Yellow.addActionListener(Listener);
    Blue.addActionListener(Listener);

}

I'm very new to java so I'm not skilled in multithreading or working on the Event Dispatch Thread for that manner. But if that's the only solution I'll need some more help with that.

I have the full code in a zip file with previous attempts commented out if that will help. https://drive.google.com/file/d/0Bxg4WleC9jD2VFhoZmZBNjV6Vkk/view?usp=sharing

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Nathaniel
  • 13
  • 2

1 Answers1

3

Invoking doClick() may be an awkward choice for this, as it uses a Timer internally. Instead, use a JToggleButton, which will allow you to control each button's appearance based on its selected state using setSelected(). A complete example is shown in the game Buttons. In the ActionListener of your Swing Timer, select the current button, play its note and increment the sequence index. When all notes have been played, unselect all the buttons.

Addendum: Can you show how you implement the timer?

In outline, given a suitable list of toggle buttons:

private static final int MAX = 4;
List<JToggleButton> buttons = new ArrayList<JToggleButton>(MAX);
private int i;

The timer's listener might look like this:

@Override
public void actionPerformed(ActionEvent e) {
    Object src = e.getSource();
    JToggleButton b = buttons.get(i);
    if (i > MAX) { // reset i and all the buttons
        for (JToggleButton b : buttons) {
            b.setSelected(false);
        }
        timer.stop();
        i = 0;
    } else {
        b.setSelected(true);
        // play tone i
        i++;
    }
}

A toggle button's item listener should update the button's appearance as indicated by its state:

@Override
public void itemStateChanged(ItemEvent e) {
    JToggleButton b = (JToggleButton) e.getItem();
    if (b.isSelected()) {
        // change icon, color etc.
    } else {
        // restore icon, color etc.
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Can you show how you implement the timer? I have the JToggleButtons and I am now aware of setSelected. I just need to make it not selected and sadly I'm drawing a blank. Also thank you for including a link to play sound. I was experimenting with that and your help overall has been great. Thanks! – Nathaniel Jan 04 '15 at 03:25
  • Thanks for responding again! I've implemented the code and I'm good. Sorry I took so long to respond, just got back to my classes so things have been crazy. Thanks again and please continue to help novices like me. We appreciate it. :) – Nathaniel Jan 06 '15 at 03:46