Help coderanch get a
new server
by contributing to the fundraiser
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Devaka Cooray
• Liutauras Vilda
Sheriffs:
• Jeanne Boyarsky
• paul wheaton
• Henry Wong
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Carey Brown
• Mikalai Zaikin
Bartenders:
• Lou Hamers
• Piet Souris
• Frits Walraven

# Have I clicked on a polygon?

Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
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

Bartender
Posts: 1844
• Number of slices to send:
Optional 'thank-you' note:
This sounds like it's more relevant to our Swing/AWT/JFC forum, so I'm moving it there.

Ranch Hand
Posts: 113
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 1535
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
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

Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
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*

 Don't get me started about those stupid light bulbs.