Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-11-03
13
Medium Priority
?
622 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:sgalzin
  • 4
  • 4
  • 3
  • +1
13 Comments
 
LVL 4

Author Comment

by:sgalzin
ID: 12484456
PS : any unthought of solution will be more than welcome of course !

Stephane.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12484514
I always go for solution 3 ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12486289
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Assisted Solution

by:objects
objects earned 75 total points
ID: 12488172
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
 
LVL 4

Author Comment

by:sgalzin
ID: 12491788
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12491834
I'm not sure where you'd think the distinction is going to lie between that and the suggested solution
0
 
LVL 4

Author Comment

by:sgalzin
ID: 12491952
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
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 225 total points
ID: 12491982
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 225 total points
ID: 12492107
>>
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
 
LVL 4

Author Comment

by:sgalzin
ID: 12492358
Ok, i'll try solution 3. thanks to all,

Stephane.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12492377
Good luck!

Tim
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12492540
8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12499329
(:
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month20 days, 16 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question