• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Mouse Clicks not registering correctly on JPanel

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I've just had my first dab in Java 2d graphics and have created a quick and rather crude program which draws a line between two points. The problem I am experiencing is that a lot of the mouse clicks just do not register on the JPanel. Any idea how to fix this? My code is below:

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems to work fine for me. What, exactly, are you experiencing?
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you forgot to call super.paintComponent(g) in your paintComponent method.
 
Faz Ali
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Seems to work fine for me. What, exactly, are you experiencing?



Sometimes the mouse clicks just do not register. I added System.out.println(clickCounter); in the code to detect when they are registered and sometimes I am definitely clicking the panel but it just doesn't register. There's no other way for we to describe what's happening.
 
Faz Ali
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:And you forgot to call super.paintComponent(g) in your paintComponent method.



Why may I ask. - I haven't read that far ahead so maybe it's something that will come up but if you could explain it now, that would be great
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Faz Ali wrote:Sometimes the mouse clicks just do not register.


A mouse click isn't trigerred if there's a movement in between mousePressed and mouseReleased.
 
Rob Spoor
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Faz Ali wrote:

Rob Spoor wrote:And you forgot to call super.paintComponent(g) in your paintComponent method.



Why may I ask. - I haven't read that far ahead so maybe it's something that will come up but if you could explain it now, that would be great


super.paintComponent(g) makes sure the panel is drawn with a clean slate - all custom drawings will be removed. Currently, if the values in your locations array would change the repainting would only add the new locations, not remove the old ones.
 
Faz Ali
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:

Faz Ali wrote:

Rob Spoor wrote:And you forgot to call super.paintComponent(g) in your paintComponent method.



Why may I ask. - I haven't read that far ahead so maybe it's something that will come up but if you could explain it now, that would be great


super.paintComponent(g) makes sure the panel is drawn with a clean slate - all custom drawings will be removed. Currently, if the values in your locations array would change the repainting would only add the new locations, not remove the old ones.



Hmm, that doesn't seem true. If you run the program you will see, it adds a new line whilst simultaneously removing the old line?
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic