• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Window (Game) not registering mouse clicks occasionally

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic