Sign in to follow this  

[java] How to detect OverLapping ImageIcons

This topic is 4017 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

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

This topic is 4017 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.

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