• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mouse Clicks not registering correctly on JPanel  RSS feed

 
Faz Ali
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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:

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to work fine for me. What, exactly, are you experiencing?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!