wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Have I clicked on a polygon? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Have I clicked on a polygon?" Watch "Have I clicked on a polygon?" New topic
Author

Have I clicked on a polygon?

Robert Somerville
Greenhorn

Joined: Sep 01, 2004
Posts: 14
I can draw a polygon of various sizes, colors and number of corners. I want to know if the user clicks on it. Any ideas how?

Rob


<a href="http://www.geocities.com/robslearningfun" target="_blank" rel="nofollow">http://www.geocities.com/robslearningfun</a><br /><a href="http://www.geocities.com/appletsforteachers" target="_blank" rel="nofollow">http://www.geocities.com/appletsforteachers</a>
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

This sounds like it's more relevant to our Swing/AWT/JFC forum, so I'm moving it there.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
What you need to implement is called a "point in a polygon" algorithm.

It would be better for you if you could find an article dedicated to describing that algorithm for you.

Basically it says that you (as the programmer) should already know the maximum points of your polygon.

step 1. get the users coordinates
step 2. theoretically draw a straigh line from that point to the one of the max points of your polygon.
step 3. count the number of times you cross your polygon.
step 4. if it's even then you are outside your poly, if odd, then you are inside it.


It's much easier to demo it by hand...

1. draw a square on a piece of paper.
2. put your pencil in the middle of the square and draw a line to the right all the way across the paper (until you have drawn it outside of the square)
3. count how many times you have crossed a line on your square. If you have crossed one line then you are inside, if you have crossed two lines then you are outside.

You can use any shape for this to be true.
Robert Somerville
Greenhorn

Joined: Sep 01, 2004
Posts: 14
Thanks jefff,
On paper I can see how this works for any polygon and using a square it would be easy to see that I have crossed a line, especially if I had drawn the square using drawLine(x0,y0,x1,y1) four times. However how do I know that I have crossed a line if I use drawPolygon(x,y,n)? I had hoped for a way to check the color at the point(x,y). If it was green for example then I must be inside the green polygon. I suppose that life just isn't that easy. It's late here and perhaps in the morning the logic will be clear. I live in hope!
Rob
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
The Polygon class and the Shape interface both have contains methods that can be put to good use here. Click on any polygon to change its color.
Robert Somerville
Greenhorn

Joined: Sep 01, 2004
Posts: 14
Thanks Craig,

You're way ahead of where I am with Java but that's good. I now have some pointers to follow. It is a bit like having a teacher to tell you what chapter to read next.

Now I'll do the work and get back to you if (when?) I get stuck.

Rob
Simon Hunt
Greenhorn

Joined: Jul 17, 2006
Posts: 11
This post is ancient history, but I'd like to log my belated thanks to Craig. I was also having problems cracking this one, but I cut and pasted Craig's code, ran it and finally annotated it until I knew what was going on.

Finally (it took about three hours ) the penny dropped... the listener is the PANEL in which the polygons are drawn, not the polygons themselves. Once the panel says it's clicked THEN you check to see if it's inside a polygon. :") It ALWAYS seems embarrassingly obvious once you understand...

Anyhow, just to let you know that your help is still bearing fruit even 2 years later!



Simon*


"I was born in a water moon..."
The Algebraist by Iain M. Banks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Have I clicked on a polygon?