Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Comparing co-ordinates

John Jericho
Greenhorn
Posts: 22
Hello, I'm trying to compare the co-ordinates of where a user clicks with the co-ordinates of a rectangle. The only way I can think of doing this is a long if statement like:

if ( mouseposx > 100 && mouseposx < 350 && mouseposy > 100 && mouseposy < 350 )
{
The click is within the rectangle
}

for example, but I was wondering if there was a better way to do this? I google'd abit and found "getBounds()" method, but I can't find anything similar to what I would use it for online. Any help would be appreciated.

Stephan van Hulst
Bartender
Posts: 6486
83
You can make a private method that checks whether an x and y position are within bounds.

Matthew Brown
Bartender
Posts: 4568
9
If you use an instance of the java.awt.Rectangle class to represent your rectangle, that has a contains(x, y) method you can use.

John Jericho
Greenhorn
Posts: 22
Thank you for your replies, but I forgot to mention that there will be more than one rectangle on screen and also polygons too so I'm not sure an instance of Rectangle will work for a polygon. I really don't want to write a whole lot of if statements because if could get quite messy..

Edit: Ok, after some reading online it seems that the Rectangle class can still be used to make Polygons but will calculate only the bounds of a square that can fit into a polygon? I'm abit confused

Campbell Ritchie
Sheriff
Posts: 50770
83
How are you creating your polygons? Are you using something like GeneralPath? I think that has "inside" methods, but you would have to look on the API documentation.

I think this thread would sit better on the GUIs forum, so I shall move it.

John Jericho
Greenhorn
Posts: 22
I'm just using the Polygon class, I've never even heard of GeneralPath so I'll look into that, thank you.

Rob Spoor
Sheriff
Posts: 20709
68
Any class that implements Shape has a contains(double, double) method. That includes Polygon, which also has a contains(int, int) method.

John Jericho
Greenhorn
Posts: 22
I see, so I can do something like save the co-ordinates of the click to a variable and use the contains method to check if those co-ordinates are within that polygon. Great! Thanks

John Jericho
Greenhorn
Posts: 22
I'm having alittle trouble here, I don't don't why but it isn't printing what I want it to, it doesn't go to the "else if" part when I'm not clicking the polygon, any help please?
Heres that part of it, I can post all of my code if needed:

Rob Spoor
Sheriff
Posts: 20709
68
John Jericho wrote:

Those aren't boolean comparisons - they are assignments. And because the result of an assignment is the new value of the variable this is perfectly legal syntax.

But this is exactly the reason why you should never write == true or == false. The following is better:
But when the if-statement does not get executed then inPoly is false and !inPoly will always return true, so this can be shortened even further:

John Jericho
Greenhorn
Posts: 22
Wow, I can't believe I made such a mistake! But thank you very much, that's really useful

John Jericho
Greenhorn
Posts: 22
Ok this is weird, it now only goes to the else part and ignores the first part, but when I do what I did before and assign it to equalling true then it ignores the else part.. It seems the co-ordinates of the mouse are not being taken into consideration and so the boolean remains false, what should I do?
Here is my code:

Darryl Burke
Bartender
Posts: 5149
11
• 1
Never put business logic in a painting method override. Test for contains(...) in mouseClicked, after assigning the new values of xPos / yPos. And declare the Polygon as an instance field and initialize it either at declaration or in a constructor. Constructing a new Polygon every time the panel is painted is wasteful.

John Jericho
Greenhorn
Posts: 22
Thank you very much for the advice, I've applied te changes, however it still won't accept the first condition. After doing some testing it seems that the boolean wasn't the problem its the contains method, its not checking to see whether the points are within the polygon or not. I've been searching online for awhile but can't find examples that are similar to my usage of it. Is it that I'm using the method in a wrong way? I'm sorry to keep pressing but this is bothering me alot..

Here is the modified code:

Note: I've replaced the addPoints way of making the polygon in favor of arrays (after reading the API)