File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Comparing co-ordinates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Comparing co-ordinates" Watch "Comparing co-ordinates" New topic
Author

Comparing co-ordinates

John Jericho
Greenhorn

Joined: Apr 11, 2011
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

Joined: Sep 20, 2010
Posts: 3378
    
    9
You can make a private method that checks whether an x and y position are within bounds.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Apr 11, 2011
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

Joined: Oct 13, 2005
Posts: 36513
    
  16
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

Joined: Apr 11, 2011
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

Joined: Oct 27, 2005
Posts: 19544
    
  16

Any class that implements Shape has a contains(double, double) method. That includes Polygon, which also has a contains(int, int) method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Jericho
Greenhorn

Joined: Apr 11, 2011
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

Joined: Apr 11, 2011
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

Joined: Oct 27, 2005
Posts: 19544
    
  16

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

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

Joined: Apr 11, 2011
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

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
John Jericho
Greenhorn

Joined: Apr 11, 2011
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)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing co-ordinates
 
Similar Threads
Alternate mouse control
Draw rectangle by dragging mouse
Help with maths...snif,sniff
Resizing frame when positioning the mouse in the frame
Problem in finding the bounds of rectangle.....