• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparing co-ordinates

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can make a private method that checks whether an x and y position are within bounds.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm just using the Polygon class, I've never even heard of GeneralPath so I'll look into that, thank you.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, I can't believe I made such a mistake! But thank you very much, that's really useful
 
John Jericho
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Does this tiny ad smell okay to you?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic