Burakkepuc
Burakkepuc

Reputation: 55

Object fit into Gridpane in Java

i have below code for JavaFx Chess Project. I need to center the piece when i released the mouse. What's the solution ? There is two lambda expression, first is when i drag mouse, change the position of piece , second is (here i need to help), when i released mouse the piece should be fit on object. Any Solution?

  public ChessBoard(){

        for (int x = 0 ; x < ChessBoard.length; x++){
            for (int y = 0; y < ChessBoard.length; y++){
                Rectangle rectangle = new Rectangle(86,86);
                rectangle.setStroke(Color.BLACK);
                rectangle.setFill((x + y) % 2 == 0 ? Color.WHITE : Color.DARKGRAY);
                gridpane.add(rectangle,x,y);
                gridpane.setAlignment(Pos.BASELINE_LEFT);
            }
        }



        /*Image blackKing = new Image("ChessPiece\\Black_King.png");
        ImageView imageView1 = new ImageView(blackKing);*/
        Image blackBishop = new Image("ChessPiece\\Black_Bishop.png");
        ImageView imageView2 = new ImageView(blackBishop);
        gridpane.getChildren().add(imageView2);



            imageView2.setOnMouseDragged(e->{
            imageView2.setTranslateX(e.getSceneX() - 25);
            imageView2.setTranslateY(e.getSceneY() - 25);

            if(imageView2 == gridpane.onMouseReleasedProperty()){
            }
        });


    }

}

Upvotes: 0

Views: 143

Answers (1)

James_D
James_D

Reputation: 209724

Use a full-press-drag-release gesture, as described in the MouseEvent documentation, under "Dragging gestures". Briefly, this dragging mode allows mouse drag events to be delivered to nodes other than the one on which the drag gesture was initiated. You can start this mode by calling startFullDrag() in a dragDetected handler.

This way, you can detect the drag release on the underlying square in the board, and simply set the grid pane coordinates of the piece to that square.

Here's a full working example:

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Checkerboard extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }

    private static final int SIZE = 8 ;

    private double mouseDragX ;
    private double mouseDragY ;


    @Override
    public void start(Stage primaryStage) throws Exception {
        GridPane board = new GridPane();

        for (int i = 0 ; i < SIZE ; i++) {
            ColumnConstraints cc = new ColumnConstraints();
            cc.setHalignment(HPos.CENTER);
            cc.setPrefWidth(100);
            cc.setMinWidth(100);
            cc.setMaxWidth(100);
            RowConstraints rc = new RowConstraints();
            rc.setValignment(VPos.CENTER);
            rc.setPrefHeight(100);
            rc.setMinHeight(100);
            rc.setMaxHeight(100);
            board.getColumnConstraints().add(cc);
            board.getRowConstraints().add(rc);
        }


        Node piece = new Circle(25, Color.DEEPSKYBLUE);

        for (int column = 0 ; column < SIZE ; column++) {
            for (int row = 0 ; row < SIZE ; row++) {
                Color color = (row + column) % 2 == 0 ? Color.WHITE : Color.DARKGREY ;
                Rectangle square = new Rectangle(100, 100, color);
                final int c = column ;
                final int r = row ;
                square.setOnMouseDragReleased(e -> {
                    piece.setTranslateX(0);
                    piece.setTranslateY(0);
                    GridPane.setColumnIndex(piece, c);
                    GridPane.setRowIndex(piece, r);
                });

                board.add(square, column, row);
            }
        }

        board.add(piece, 0, 0);

        piece.setOnDragDetected(e -> {
            piece.startFullDrag();
        });
        piece.setOnMousePressed(e -> {
            mouseDragX = e.getSceneX();
            mouseDragY = e.getSceneY();
            piece.setMouseTransparent(true);
        });
        piece.setOnMouseDragged(e -> {
            piece.setTranslateX(e.getSceneX() - mouseDragX);
            piece.setTranslateY(e.getSceneY() - mouseDragY);
        });
        piece.setOnMouseReleased(e -> piece.setMouseTransparent(false));

        Scene scene = new Scene(board);
        primaryStage.setScene(scene);
        primaryStage.show();

    }



}

Upvotes: 1

Related Questions