File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Window (Game) not registering mouse clicks occasionally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Window (Game) not registering mouse clicks occasionally" Watch "Window (Game) not registering mouse clicks occasionally" New topic
Author

Window (Game) not registering mouse clicks occasionally

Ryan Sykes
Ranch Hand

Joined: Jan 18, 2012
Posts: 58
Hi folks,

I've been learning Java and OOP in my spare time for fun lately. After finishing with Head First Java, I'm currently reading through Daniel Liang's excellent Introduction to Java Programming Comprehensive and Core Java Vol 1. I'm currently programming some of the exercise problems at the end of the Event driven programming chapter of the first book. Here is the exercise problem:


Write a program that displays a circle of radius 10pixels filled with a random color at a random location on a panel. When you click the circle, it is gone and a new random-color circle is displayed at another random location. After twenty circles are clicked, display the time spent in the panel.


I've coded this up and it works perfectly, except that when clicking in rapid succession, some of the clicks are not registered. I have noticed this with other programs I have written as well, and when I run the compiled class files provided by the author of the book (to try running all the exercise problems), I observe a similar behavior. Is this a concurrency issue due to conflicts with the GUI EDT? I haven't read up on threads and concurrency yet, but if that is the case, I'd like to identify where the issue is being produced. With the bug as is, this exercise is a bit pointless because you can never get a great "score" due to the unregistered click events.

PS - I have added in some stuff to avoid bugs when resizing the window. I know that I could have set it to not be resizable, but I was just playing around with the code to see if I could handle that case without making a hash of things. Also, I'd appreciate any general comments about the program and what I could do to improve it.



Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I've coded this up and it works perfectly, except that when clicking in rapid succession, some of the clicks are not registered

mouseClicked() is *always* a poor choice.

mousePressed fires first, then mouseReleased, then,
if the x,y of pressed and released are identical,
mouseClicked fires.

so, any slight movement and mouseClicked won't fire
Ryan Sykes
Ranch Hand

Joined: Jan 18, 2012
Posts: 58
Thanks a lot for your reply Michael! That worked perfectly and makes sense. I should have thought of that ahead of time.

On a related note, is there any guideline on how frequently the MouseMotionListener polls the mouse movement? The reason I ask is that I had written a similar program, that drew a circle and allowed me to click inside the circle and drag the circle around. However, I noticed that if I jerked the mouse very fast, the circle would no longer follow the mouse. It turned out that when I jerked the mouse very fast, between two consecutive polls of the mouse position, my mouse moved outside the circle and thus triggered the circle to stop following (my basic logic to start the drag was to keep updating the center co-ordinates of the circle to those of the mouse as long as the mouse was within the circle).

I was able to fix this by using a boolean flag that was triggered when I started a drag event, thus causing the circle to always follow the mouse during the course of the mouse drag action. Still, I'm curious about the fidelity of the MouseMotionListener and any advice/best practices involved with implementing it with regards to the finite polling rate of the mouse location.

PS - I can post the full code to the program if needed.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
similar sounding problems with keylisteners were solved by using
keyPressed and keyReleased to start/stop timers.

the timer allows you to set your own interval/delay.

would be worth looking into using mousePressed to start a timer,
the timer to get the current x,y and do what you want with the redrawing,
then mouseReleased to stop the timer.
Ryan Sykes
Ranch Hand

Joined: Jan 18, 2012
Posts: 58
Thanks a lot for your help Michael. I really appreciate it. I'll keep that in mind the next time I'm working with MouseListeners.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Window (Game) not registering mouse clicks occasionally