@@ -0,0 +1,2235 @@
+/**%0A *%0A */%0Apackage com.github.apixandru.games.rummikub.ui;%0A%0Aimport java.awt.Component;%0Aimport java.awt.Container;%0Aimport java.awt.Point;%0Aimport java.awt.event.MouseAdapter;%0Aimport java.awt.event.MouseEvent;%0A%0Aimport javax.swing.JLayeredPane;%0Aimport javax.swing.JPanel;%0A%0A/**%0A * @author Alexandru-Constantin Bledea%0A * @since Oct 13, 2015%0A */%0Afinal class CardDndListener extends MouseAdapter %7B%0A%0A%09private final JLayeredPane layeredPane;%0A%09private final JPanel panel;%0A%0A%09private CardUi movingPiece;%0A%09private int xOffset;%0A%09private int yOffset;%0A%0A%09CardDndListener(final JLayeredPane layeredPane, final JPanel pane) %7B%0A%09%09this.layeredPane = layeredPane;%0A%09%09this.panel = pane;%0A%09%7D%0A%0A%09/* (non-Javadoc)%0A%09 * @see java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)%0A%09 */%0A%09@Override%0A%09public void mousePressed(final MouseEvent e) %7B%0A%09%09final CardUi card = getCard(e);%0A%09%09if (null == card) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09this.movingPiece = card;%0A%0A%09%09final Point parentLocation = card.getParent().getLocation();%0A%09%09this.xOffset = parentLocation.x - e.getX();%0A%09%09this.yOffset = parentLocation.y - e.getY();%0A%0A%09%09layeredPane.add(movingPiece, JLayeredPane.DRAG_LAYER);%0A%0A%09%09updateMovingPieceLocation(e);%0A%09%7D%0A%0A%09/* (non-Javadoc)%0A%09 * @see java.awt.event.MouseAdapter#mouseDragged(java.awt.event.MouseEvent)%0A%09 */%0A%09@Override%0A%09public void mouseDragged(final MouseEvent e) %7B%0A%09%09if (this.movingPiece == null) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09updateMovingPieceLocation(e);%0A%09%7D%0A%0A%09/* (non-Javadoc)%0A%09 * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)%0A%09 */%0A%09@Override%0A%09public void mouseReleased(final MouseEvent e) %7B%0A%09%09if (this.movingPiece == null) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09final Container parent = (Container) this.panel.findComponentAt(e.getX(), e.getY());%0A%09%09parent.add(this.movingPiece);%0A%09%09this.layeredPane.repaint();%0A%09%09parent.validate();%0A%09%09this.movingPiece = null;%0A%09%7D%0A%0A%09/**%0A%09 * @param e%0A%09 * @return%0A%09 */%0A%09private CardUi getCard(final MouseEvent e) %7B%0A%09%09Component c = this.panel.findComponentAt(e.getX(), e.getY());%0A%09%09while (null != c && !(c instanceof CardUi)) %7B%0A%09%09%09c = c.getParent();%0A%09%09%7D%0A%09%09return (CardUi) c;%0A%09%7D%0A%0A%09/**%0A%09 * @param e%0A%09 */%0A%09private void updateMovingPieceLocation(final MouseEvent e) %7B%0A%09%09this.movingPiece.setLocation(e.getX() + xOffset, e.getY() + yOffset);%0A%09%7D%0A%0A%7D%0A

add basic drag and drop listener