Sign in to follow this  
The Hitman

[java] How to detect OverLapping ImageIcons

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this