This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes doClick not working as intended! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "doClick not working as intended!" Watch "doClick not working as intended!" New topic
Author

doClick not working as intended!

Pat Ziel
Greenhorn

Joined: Nov 07, 2009
Posts: 9
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

Joined: Jun 13, 2009
Posts: 2155
    
    7
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

Joined: Nov 07, 2009
Posts: 9
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

Joined: Jun 13, 2009
Posts: 2155
    
    7
I don't see a SSCCE anywhere!
Pat Ziel
Greenhorn

Joined: Nov 07, 2009
Posts: 9
Rob Camick wrote:I don't see a SSCCE anywhere!


Coming right up.

http://rapidshare.com/files/307185531/simon.rar.html
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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).


bitguru blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doClick not working as intended!
 
Similar Threads
OpenYMSG API on android
Making real time digital clock using RCP.
Ping a IP address in Java
Thread safty in static method
Is there any problem with this code?