0

I am dragging a JLabel around the screen, and when I release above the JPanel it is supposed to snap to where it completely covers the JPanel. Also, if I release anywhere else it is supposed to snap to its original position. I have the snap part, but I don't know how to tell if it is over the JPanel. I have my code below.

    import java.awt.Color;
    java.awt.Dimension;
    import javax.swing.*;
    import javax.swing.event.MouseInputAdapter;
    import java.awt.event.MouseEvent;

public class Main {
public static final int CARD_HEIGHT = 97;
public static final int CARD_WIDTH = 73;

/**
 * Mouse Handler components
 * Changes the location of the JLabel with the mouse
 */
public static MouseInputAdapter mouseHandler = new MouseInputAdapter(){
    public int labelDisX;   
    public int labelDisY;                 
    public void mousePressed(MouseEvent e) {
        labelDisX = e.getX();
        labelDisY = e.getY();
        //move the card above all others
        e.getComponent().getParent().setComponentZOrder(e.getComponent(), 0); 
        e.getComponent().getParent().repaint();         
    }
    public void mouseReleased(MouseEvent e) {
        //if not above panel, then move to original spot
        if(!abovePanel()) {
            e.getComponent().setLocation(labelDisX, labelDisY);
        }
    }
    public void mouseDragged (MouseEvent e) {
        JPanel panel = (JPanel) e.getComponent().getParent();
        //get preliminary new X coordinate
        int newX = e.getComponent().getX() + e.getX() - labelDisX; 
        //get preliminary new Y coordinate
        int newY = e.getComponent().getY() + e.getY() - labelDisY;  
        //Not moved off edges of JFrame
        if(newX > panel.getWidth() - CARD_WIDTH) {
            newX = panel.getWidth() - CARD_WIDTH;                             
        }                                                                     
        if(newY > panel.getHeight() - CARD_HEIGHT) {                          
            newY = panel.getHeight() - CARD_HEIGHT;                           
        }                                                                     
        if(newX < 0) { newX = 0; }                                            
        if(newY < 0) { newY = 0; }
        e.getComponent().setLocation(newX, newY);
    }
};

/**
 * check to see if the JLabel is above the JPanel
 * @return
 */
public static boolean abovePanel() {

    return false;
}

/**
 * Adds ability to drag to JLabel
 * @param label
 */
public static void addDrag( JLabel label) {
    System.out.println("Adding drag");
    label.addMouseMotionListener(mouseHandler);                        
    label.addMouseListener(mouseHandler);      
}

public static void main(String[] args) {

// Create a JFrame
JFrame frame = new JFrame("Example Frame");
// JPanel to add JLabels to
JPanel panel = new JPanel();

// Add a drop target text area in the center of the frame
DropTargetArea dropPanel = new DropTargetArea();
dropPanel.setPreferredSize(new Dimension(CARD_WIDTH, CARD_HEIGHT));
dropPanel.setBackground(Color.gray);
panel.add(dropPanel);

// Add several draggable labels to the container
JLabel blue = new JLabel();
blue.setOpaque(true);
blue.setPreferredSize(new Dimension(CARD_WIDTH, CARD_HEIGHT));
blue.setBackground(Color.blue);
addDrag(blue);


panel.add(blue);


// Add the container to the frame
frame.add(panel);
// Display the frame
frame.setPreferredSize(new Dimension(400,400));
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
}
}

1 Answers1

0

I've done this using a JLayeredPane that holds a JPanel that itself holds a grid of JPanels using BorderLayout, with each smaller JPanel representing a chess square, and each smaller JPanel can accept a single JLabel. I added the MouseAdapter to the JLayeredPane itself, and when clicked, it checks to see if a moveable JLabel is located below the click. If so, the JLabel is raised up to the JLayeredPane's DRAG_LAYER, and then when released, check which JPanel the mouse cursor is over, and drop the JLabel if it is a valid square, otherwise return it to its original position. You can see my code here.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373