Catch mouse events on a drawn object (say a Polygon)

Hi,

I have drawn 4 polygons on a JPanel. I wish to capture mouse events so as to say which polygon has been clicked. I can see three possible solutions, could you please tell me which are feasable and which solution you consider best ?

Solution 1 : very conveniently, Java has enabled listeners of some sort for 2D Graphic objects ... how do I catch them ?

Solution 2 : ok, Solution 1 doesn't exist : I have to extend a Swing clickable object (such as a JButton for example) and make it look like my polygon ... how do I go about this ?

Solution 3 : none of the above are possible, I therefore have to check the coordinates of the mouse click and see if it belongs to any of my polygons. I'm kind of hoping this isn't the only solution ;-)

Thanks,

Stephane.
LVL 4
sgalzinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sgalzinAuthor Commented:
PS : any unthought of solution will be more than welcome of course !

Stephane.
0
TimYatesCommented:
I always go for solution 3 ;-)
0
CEHJCommented:
Gui components enabled for the capture of mouse events are based on windowed peers and are therefore rectangular, which is why no solution other than 3 exists. This is trivial though:

someComponent.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
            boolean inPoly = poly.contains(e.getPoint());
      }
});
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

objectsCommented:
Polygon[] polygons;

public void mouseClicked(MouseEvent event)
{
   Polygon inside = null;
   for (int i=0; i<polygons.length; i++)
   {
      if (polygons[i].contains(event.getPoint()))
      {
          inside = polygons[i];
      }
   }
   if (inside!=null)
   {
       // do whatever
   }
}
0
sgalzinAuthor Commented:
Hi,

Thanks to all for your participation.

However, I have done some of my own research yesterday and it does seem that solution 2 is implementable : wouldn't it be possible for me to extend the JComponent class in order to draw clickable polygons ? I haven't been able to achieve that yet but it sure looks possible. Any ideas ?

Thanks,

Stephane.
0
CEHJCommented:
I'm not sure where you'd think the distinction is going to lie between that and the suggested solution
0
sgalzinAuthor Commented:
Hi CEHJ,

The way I see it, solution 2 means I just add a component (say JPolygonComponent, which extends JComponent) : the polygon becomes visible and I can add MouseListeners to it at any time.

The solution 3 on the other hand consisted in drawing 2D objects on a JPanel and catching all mouse events on the JPanel *then* trying to figure out to which polygon the event is applicable.

I would much prefer implementing solution2 (which means I don't need to keep an array of polygons to check every one each time an event is triggered). You said in your post "no solution other than 3 exists" : if your solution applied to a solution2 architecture, please show me how.

Thanks,

Stephane.
0
TimYatesCommented:
All JComponents are rectangular...

You can make them look polygonal, but they will be rectangular

So you will end up doing a load of work to see if they component has been clicked on...

Option 3 is a much easier solution, as you then control things like Z-Ordering, and painting yourself
0
CEHJCommented:
>>
I would much prefer implementing solution2 (which means I don't need to keep an array of polygons to check every one each time an event is triggered).
>>

Then have each polygon on a separate component. You'd still be using 3 though
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sgalzinAuthor Commented:
Ok, i'll try solution 3. thanks to all,

Stephane.
0
TimYatesCommented:
Good luck!

Tim
0
CEHJCommented:
8-)
0
objectsCommented:
(:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.