How do I get a MouseEvent for each multiple overlapping JComponent?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Alex_Peter
Posted On:   Tuesday, October 19, 2010 03:23 PM

I have a canvas that I paint lines on which connect nodes. Each line has a bounding box, and many of them overlap. The lines are JComponents which implement Selectable and MouseListener . I would like the mousePressed event to be passed to each object whose bounding box surrounds the event.getPoint() . Unfortunately, only the 'top most' object gets the event. Overriding isOpaque() and isOptimizedDrawingEnabled() to return false did not help. In other words, how do I make all my lines unobscured by other lines?

Re: How do I get a MouseEvent for each multiple overlapping JComponent?

Posted By:   Alex_Peter  
Posted On:   Wednesday, October 20, 2010 02:22 PM

OK, I found out how to do this. I needed to override the contains() method. This is the method called when it is trying to figure out which component to give the event to. The default implementation is to use the bounding box, which covers way too much real estate for a diagonal line. I used the Graphics2D.hit() method to calculate if the event position is within a few pixels of a line.
About | Sitemap | Contact