Knowing if a user clicked on the area of a shape

The code below draws a neat little arrow-type shape on a jPanel. (The coordinates for the "template" shape are given initially, and a few smaller and larger shapes are transformed and drawn onscreen based on that - I've included an example here.)

My question is - how to know that the user has clicked within the shape which has been drawn, since I don't know the boundaries of its display?

I could just get the coundaries of what's been drawn into a rectangle, and then I'd know if the user clicked in the general area of the shape - but not exactly if the user has clicked on the graphic being displayed since it's an "odd shaped" drawing.

I.e. if the user clicks at screen point x50,y50, and the starting x,y of the shape drawn (which IS always known) is at x10,y10... how do I know if the point clicked includes the shape?

Graphics2D g2 = (Graphics2D) g;

GeneralPath myShape =new GeneralPath(GeneralPath.WIND_EVEN_ODD);

myShape.moveTo(x +8, y +6);

myShape.lineTo(x +51, y +102);

myShape.lineTo(x +49, y +62);

myShape.lineTo(x +93, y +32);

myShape.closePath();

AffineTransform af =new AffineTransform();

af.setToTranslation(x,y);

af.scale(0.5, 0.5);// reduce to a smaller (half size) square

af.translate(-x,-y);

GeneralPath myShape2 = (GeneralPath) af.createTransformedShape(myShape);

g2.fill(myShape2);

g2.draw(myShape2);

[1539 byte] By [BarcleyLSa] at [2007-11-26 22:06:23]
# 1
Use myShape2.contains function to test if the shape contains the (x, y) point the user clicked.
Rodney_McKaya at 2007-7-10 10:51:08 > top of Java-index,Security,Cryptography...
# 2

Wonderful what a bit of reading on the API will reveal lol - sorry, should have spotted that one!!

The only other question I have is that, if I'm drawing 100 of these onscreen (in various sizes, some smaller than the "template", some larger, maybe some of them overlapping each other on occasion)... how would that be dealt with? It's alright if I know the x,y user click values before/whilst creating my shapes... but I don't. The shapes are drawn onscreen using the code example, then later the user may click.

How to get round that?

Graphics2D g2 = (Graphics2D) g;

GeneralPath myShape = new GeneralPath(GeneralPath.WIND_EVEN_ODD);

myShape.moveTo(x +8, y +6);

myShape.lineTo(x +51, y +102);

myShape.lineTo(x +49, y +62);

myShape.lineTo(x +93, y +32);

myShape.closePath();

AffineTransform af = new AffineTransform();

af.setToTranslation(x,y);

af.scale(0.5, 0.5); // reduce to a smaller (half size) square

af.translate(-x,-y);

GeneralPath myShape2 = (GeneralPath) af.createTransformedShape(myShape);

g2.fill(myShape2);

g2.draw(myShape2);

AffineTransform af2 = new AffineTransform();

af2.setToTranslation(x,y);

af2.scale(2, 2); // reduce to a larger (double size) square

af2.translate(-x,-y);

GeneralPath myShape3 = (GeneralPath) af.createTransformedShape(myShape);

g2.fill(myShape3);

g2.draw(myShape3);

BarcleyLSa at 2007-7-10 10:51:08 > top of Java-index,Security,Cryptography...
# 3
Store the shapes inside a Vector and iterate over the Vector checking each shape if it contains the x,y user click.
Rodney_McKaya at 2007-7-10 10:51:08 > top of Java-index,Security,Cryptography...
# 4

Doesn't the Shape.contains(x,y) test if the bounding rectangle contains the point though?

If, what if the shape was a triangle or a irregular shape?

The user might click of the side but the click would still be inside the shapes bounds.

How would one test the shape itself rather then the bounds, if that is the case?

dfgstga at 2007-7-10 10:51:08 > top of Java-index,Security,Cryptography...
# 5
Shape.contains test if the shape contains the point and not if the bounding rectangle contains the point.Why don't you try and see...
Rodney_McKaya at 2007-7-10 10:51:08 > top of Java-index,Security,Cryptography...
# 6
Oh, thanks. I was thinking it was the bounds. don't know where I got that idea from tho. sorry :)
dfgstga at 2007-7-10 10:51:08 > top of Java-index,Security,Cryptography...
# 7
My apologies, I didn't realise further replies had been posted. Cheers :)
BarcleyLSa at 2007-7-10 10:51:08 > top of Java-index,Security,Cryptography...