Jump to content
  • Advertisement
Sign in to follow this  
The Hitman

[java] How to detect OverLapping ImageIcons

This topic is 4233 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, Is there a way to find out if an Imageicon is overlapping another imageicon,not just if they have the same x and y position but if they touch each other at all. Thanks

Share this post


Link to post
Share on other sites
Advertisement
I don't know of any direct methods you can use, but you could build some java.awt.Rectangle 's from their dimensions and use the contains(), intersects() methods (Or calculate it yourself :) ).

Share this post


Link to post
Share on other sites
Hey there,

the way I detect overlapping rectangles (which is basically your problem) is by detecting if they are NOT intersecting. THis would give you following method:



public static boolean isIntersecting(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2){

//w and h are width and height

if(x2 > x1 + w1){
return false; //first rectangle is too much to the left
}

if(x1 > x2 + w2){
return false; //first rectangle is too much to the right
}

if(y2 > y1 + h1){
return false; //first rectangle is too low
}

if(y1 > y2 + h2){
return false; //first rectangle is too high
}

return true//it could only be intersecting

}

I still have to test this code, but it should work
If somebody knows a more efficient way, I would like to hear about it.

Share this post


Link to post
Share on other sites
Hey there,

the way I detect overlapping rectangles (which is basically your problem) is by detecting if they are NOT intersecting. THis would give you following method:



public static boolean isIntersecting(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2){

//w and h are width and height

if(x2 > x1 + w1){
return false; //first rectangle is too much to the left
}

if(x1 > x2 + w2){
return false; //first rectangle is too much to the right
}

if(y2 > y1 + h1){
return false; //first rectangle is too low
}

if(y1 > y2 + h2){
return false; //first rectangle is too high
}

return true//it could only be intersecting

}

I still have to test this code, but it should work
If somebody knows a more efficient way, I would like to hear about it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!