• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doClick not working as intended!

 
Pat Ziel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Camick
Ranch Hand
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but the dang buttons keep unClicking at the same time


Seems to me like the code is executing on the EDT and the Thread.sleep() prevents the buttons from repainting themselves.

Read the section from the Swing tutorial on "Concurrency" for a better explanation.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.
 
Pat Ziel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
but the dang buttons keep unClicking at the same time



If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.


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.
 
Rob Camick
Ranch Hand
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a SSCCE anywhere!
 
Pat Ziel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:I don't see a SSCCE anywhere!


Coming right up.

http://rapidshare.com/files/307185531/simon.rar.html
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No offense, but if you need to RAR* it it's most likely not a short example, is it?

* ZIP is a better solution since RAR is a closed format.
 
Brian Cole
Author
Ranch Hand
Posts: 908
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic