I have a project set up using a JLayeredPane with a number of JLabel objects. Some are gray images, and others are images of cards. The cards are drag-able, but I'm trying to make them so they can only be dropped onto the blank spots, if its not one of the blank spots then it snaps back.
The problem I am having is that I cant figure out how to get the component that is under the card that is being dragged.
This is what I have tried in the mouseReleased method in a mouse listener
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
JLabel c = (JLabel)Global.window.getComponentAt(e.getXOnScreen()-x, e.getYOnScreen()-y);
System.out.println((e.getXOnScreen()) + ","+(e.getYOnScreen()) + " "+c.getName()); //debug
if (c.getName() == "blankLeft2" && card == cardTY.ace)
{
//snap card to this spot
setBounds(c.getX(),c.getY(),super.getIcon().getIconWidth(), super.getIcon().getIconHeight());
}
else if (c.getName() == "blank") //snap to blank spot
setBounds(c.getX(),c.getY(), super.getIcon().getIconWidth(), super.getIcon().getIconHeight());
else //set back to old spot spot
setBounds(xx,yy,super.getIcon().getIconWidth(), super.getIcon().getIconHeight());
}
x and y are cursor offsets for its position on the card being dragged and xx and yy are the previous position, set in the mouse clicked event
how do I get whats underneath it?