Ejemplo n.º 1
0
    /**
     * Terminates the drag process, putting the dragged piece in the nearest square.
     *
     * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
     */
    @Override
    public void mouseReleased(MouseEvent e) {
      if (pieceBeingDragged == null) return;
      Rectangle oldRect = pieceBeingDragged.getRectangle();
      Rectangle newRect = oldRect;
      int newRow = board.yToRow(pieceBeingDragged.y + board.getCellHeight() / 2);
      int newColumn = board.xToColumn(pieceBeingDragged.x + board.getCellWidth() / 2);

      if (pieceBeingDragged.canMoveTo(newRow, newColumn)) {
        pieceBeingDragged.changePosition(newRow, newColumn);
      }
      pieceBeingDragged.moving = false;
      newRect = pieceBeingDragged.getRectangle();
      pieceBeingDragged.redraw(pieceBeingDragged.enlarge(oldRect.union(newRect)));
      board.dragEvent.reportEvent(pieceBeingDragged);
      pieceBeingDragged = null;
    }
Ejemplo n.º 2
0
 /**
  * When the mouse button is pressed over a draggable piece, begins the dragging process. Only
  * one piece can be dragged at a time.
  *
  * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
  */
 @Override
 public void mousePressed(MouseEvent e) {
   board.setSelectedSquare(board.yToRow(e.getY()), board.xToColumn(e.getX()));
   Piece chosenPiece = board.findPiece(e.getX(), e.getY());
   if (chosenPiece == null) {
     return;
   }
   if (chosenPiece.isSelectable()) {
     board.setSelectedPiece(chosenPiece);
   }
   if (pieceBeingDragged != null) {
     return; // can only drag one piece at a time
   }
   if (!chosenPiece.draggable) {
     return;
   }
   pieceBeingDragged = chosenPiece;
   board = pieceBeingDragged.board;
   pieceBeingDragged.moving = true;
   board.moveToTop(pieceBeingDragged);
 }