0

Quick question. I have a jPanelSquareGrid with labels in some squares (image labels, representing chess pieces)

now when I drag and drop a piece to a certain square, I want to check if there isn't already a chesspiece in it.

Does anyone have any idea how I can check a jPanelSquareGrid for existing items inside a square?

Thanks in advance!

here are my mouse events

   public void mousePressed(MouseEvent e)
{
    chessPiece = null;
    Component c =  chessBoard.findComponentAt(e.getX(), e.getY());

    if (c instanceof JPanel) return;

    Point parentLocation = c.getParent().getLocation();
    xAdjustment = parentLocation.x - e.getX();
    yAdjustment = parentLocation.y - e.getY();
    chessPiece = (JLabel)c;
    chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);

    layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
    layeredPane.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
}

/*
**  Move the chess piece around
*/
public void mouseDragged(MouseEvent me)
{
    if (chessPiece == null) return;

    //  The drag location should be within the bounds of the chess board

    int x = me.getX() + xAdjustment;
    int xMax = layeredPane.getWidth() - chessPiece.getWidth();
    x = Math.min(x, xMax);
    x = Math.max(x, 0);

    int y = me.getY() + yAdjustment;
    int yMax = layeredPane.getHeight() - chessPiece.getHeight();
    y = Math.min(y, yMax);
    y = Math.max(y, 0);

    chessPiece.setLocation(x, y);
 }

/*
**  Drop the chess piece back onto the chess board
*/
public void mouseReleased(MouseEvent e)
{
    layeredPane.setCursor(null);

    if (chessPiece == null) return;

    //  Make sure the chess piece is no longer painted on the layered pane

    chessPiece.setVisible(false);
    layeredPane.remove(chessPiece);
    chessPiece.setVisible(true);

    //  The drop location should be within the bounds of the chess board

    int xMax = layeredPane.getWidth() - chessPiece.getWidth();
    int x = Math.min(e.getX(), xMax);
    x = Math.max(x, 0);

    int yMax = layeredPane.getHeight() - chessPiece.getHeight();
    int y = Math.min(e.getY(), yMax);
    y = Math.max(y, 0);

    Component c =  chessBoard.findComponentAt(x, y);

    if (c instanceof JLabel)
    {
        Container parent = c.getParent();
        parent.remove(0);
        parent.add( chessPiece );
        parent.validate();
    }
    else
    {
        Container parent = (Container)c;
        parent.add( chessPiece );
        parent.validate();
    }
}

public void mouseClicked(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

jPanelSquare is made of this class:

class JPanelSquare extends JPanel {
private int rank;
private int file;
private JLabel chessPiece = null;

public JPanelSquare(int rank, int file, Color bkgrnd) {
    this.rank = rank;
    this.file = file;
    setBackground(bkgrnd);
    setLayout(new GridBagLayout());
}

public int getRank() {
    return rank;
}
public int getFile() {
    return file;
}

@Override
public Component add(Component c) {
    chessPiece = (JLabel)c;
    return super.add(c);
}

@Override
public void remove(Component comp) {
    chessPiece = null;
    super.remove(comp);
}

public JLabel getChessPiece() {
    return chessPiece;
}

}

zeta
  • 1,113
  • 3
  • 15
  • 24
  • See also this [example](http://stackoverflow.com/questions/2561690/placing-component-on-glass-pane/2562685#2562685) and [variation](http://stackoverflow.com/questions/2561690/placing-component-on-glass-pane/2563350#2563350). – trashgod Nov 08 '11 at 20:06
  • I already have all of that, I just need to know, when I drag n drop my piece, how do I check if a label already is in the jPanelSquareGrid? – zeta Nov 08 '11 at 20:40
  • What's the class of jPanelSquareGrid? Where is its javadoc? – JB Nizet Nov 08 '11 at 20:40
  • it's actually jPanelSquare, sorry I don't know where my mind was, it's a custom class just learning java guys, got the class from stackoverflow somewhere – zeta Nov 08 '11 at 20:44

1 Answers1

1

got the class from stackoverflow somewhere

Looks to me like you are combining code from 2 difference sources.

I recoginize my dragging code from the ChessBoard example I've posted.

In my code its easy to tell if a chess piece already exists since the code already does that in the mouseReleased code. The what the "if statement" at the end of the method is for.

I don't know where you got the other code from so I can't comment on that.

I suggest you stick with one solution.

camickr
  • 321,443
  • 19
  • 166
  • 288