doClick not working as intended!

Hello everyone, I'm working on a Simon Says game, when the computer generates the sequence I do a doClick(400) on my array of four colors(red,blue,yellow,green) in a for loop that goes trough a LinkedList.

The logic is perfect, I've checked, everything is working as intended except the animation...

When doClick run's on the green for 400 milli's then goes off for let's say the yellow button which calls another doClick of a 400 milli's, they start at a different time...as intended, but the dang buttons keep unClicking at the same time, as if they weren't really stopping at their time.

Here's the thread containing that code:

This causes that they do not unpress when a new color arrives, also that if a color is called twice you can't tell the difference except for a longer period.

It seems as if a doClick runs off, he waits until all of them are done before unpressing.

Sorry if I have not made myself clear, I recently learned about threads a week ago, the concept is new to me. I have already read the document provided.

I will try to give you an better overview of my current issue:

Firstly, I initiate four buttons, setting ImageIcon's with different pictures depending on their state(Pressed or none).

Then I put these in a simple array:

Once the game starts I call the simonDit() method:

The simonDit method contains one line which is:

The first parameter is my array of buttons while the second is my LinkedList which stores each new int value generated randomly.

That's it, the thread code is in my first post. I hope I didn't make this too general, I've done some testing, I've noticed that a simple doClick() on arrayButton[i] will click really quickly on the first round(When there's only one color generated and clicked). On the second round when their is an attempt of two doClick()'s the first press will activate itself and wait until the second one finishes before clearing out.

Quick pattern: Green alone(1 sec click), Green + Blue(Each start at a different time, but both clear at the same time).

P.S Sorry that my variables are in french, I study computer science in a french place.

Pat Ziel wrote:Hello everyone, I'm working on a Simon Says game, when the computer generates the sequence I do a doClick(400) on my array of four colors(red,blue,yellow,green) in a for loop that goes trough a LinkedList.

The logic is perfect, I've checked, everything is working as intended except the animation...

When doClick run's on the green for 400 milli's then goes off for let's say the yellow button which calls another doClick of a 400 milli's, they start at a different time...as intended, but the dang buttons keep unClicking at the same time, as if they weren't really stopping at their time.

There's also another problem that you may not have noticed yet. While the sequence is being performed the GUI is unresponsive, so the user will be able to cancel or quit until the playback is over. This could be a problem for longer sequences.

Mr. Camick actually described what's going wrong, but you have to know how the EDT works to understand it.

A quick fix is to make sure your run() method isn't running on the EDT, but also execute your calls to doClick() inside invokeAndWait(). However, this is still not quite ideal because doClick(400) is implemented in a way that it will freeze the EDT for 400ms. It may be good enough for your purposes, though.

The "correct" way would be to run something like this pseudocode (on a non-EDT thread, of course).