How do I detect the colillison of components?

Posted by Coupon22 on Stack Overflow See other posts from Stack Overflow or by Coupon22
Published on 2012-09-07T21:20:05Z Indexed on 2012/09/07 21:38 UTC
Read the original article Hit count: 202

How do I detect the collision of components, specifically JLabels (or ImageIcons?)?I have tried this:

add(test1);
test1.setLocation(x, y);
add(test2);
test1.setLocation(x1, y1);
validate();

if(intersects(test1, test2))
{
    ehealth-=50;
}

public boolean intersects(JLabel testa, JLabel testb)
{
    boolean b3 = false;
    if(testa.contains(testb.getX(), testb.getY()))
    {
        b3 = true;
    }
    return b3;
}

When I run this, it does nothing! I used to use rectangle, but it didn't go well with me. I was thinking about an image with a border (using paint.net) and moving an imageicon, but I don't know how to get the x of an imageicon or detect collision. I don't know how to detect collision of a label or increase the location either.

I have searched for collision detection with components/ImageIcons, but nothing has came up. I have also searched for getting the x of ImageIcons.

© Stack Overflow or respective owner

Related posts about java

Related posts about swing