对象适合 Java 中的网格窗格
Object fit into Gridpane in Java
我有以下 JavaFx 国际象棋项目的代码。当我松开鼠标时,我需要将作品居中。解决办法是什么?有两个 lambda 表达式,第一个是当我拖动鼠标时,改变棋子的位置,第二个是(这里我需要帮助),当我释放鼠标时棋子应该适合对象。有什么解决办法吗?
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()){
}
});
}
}
如 "Dragging gestures" 下的 MouseEvent
documentation 中所述,使用完全按下-拖动-释放手势。简而言之,这种拖动模式允许将鼠标拖动事件传递到发起拖动手势的节点以外的节点。您可以通过在 dragDetected
处理程序中调用 startFullDrag()
来启动此模式。
这样,您就可以检测到棋盘下方正方形上的拖动释放,只需将棋子的网格窗格坐标设置到该正方形即可。
这是一个完整的工作示例:
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();
}
}
我有以下 JavaFx 国际象棋项目的代码。当我松开鼠标时,我需要将作品居中。解决办法是什么?有两个 lambda 表达式,第一个是当我拖动鼠标时,改变棋子的位置,第二个是(这里我需要帮助),当我释放鼠标时棋子应该适合对象。有什么解决办法吗?
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()){
}
});
}
}
如 "Dragging gestures" 下的 MouseEvent
documentation 中所述,使用完全按下-拖动-释放手势。简而言之,这种拖动模式允许将鼠标拖动事件传递到发起拖动手势的节点以外的节点。您可以通过在 dragDetected
处理程序中调用 startFullDrag()
来启动此模式。
这样,您就可以检测到棋盘下方正方形上的拖动释放,只需将棋子的网格窗格坐标设置到该正方形即可。
这是一个完整的工作示例:
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();
}
}