Javafx棋盘游戏
我需要做一个棋盘游戏使用javafx,播放器v播放器。我可以建立董事会,但我一辈子都不知道从那里走到哪里。我的特殊问题是让碎片移动。我看了他一眼,但没用。我已经搜索了几个小时,找不到一个有用的起点。我并不是要求有人帮我完成代码,而是开始我应该走的方向,因为我迷路了。谢谢 我添加了一个拖放方法,该方法检测拖放,但实际上不移动棋盘格。有什么帮助吗Javafx棋盘游戏,java,javafx,drag-and-drop,click,Java,Javafx,Drag And Drop,Click,我需要做一个棋盘游戏使用javafx,播放器v播放器。我可以建立董事会,但我一辈子都不知道从那里走到哪里。我的特殊问题是让碎片移动。我看了他一眼,但没用。我已经搜索了几个小时,找不到一个有用的起点。我并不是要求有人帮我完成代码,而是开始我应该走的方向,因为我迷路了。谢谢 我添加了一个拖放方法,该方法检测拖放,但实际上不移动棋盘格。有什么帮助吗 import javafx.application.Application; import javafx.event.ActionEvent; impor
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.Cell;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
public class CheckerboardSetUp extends Application{
private static final int BOARD_DIM = 8;
private static final int SQUARES = 64;
private static final int PIECES = 12;
private Cell[][] boardG = new Cell[8][8];
private Circle[] p1p = new Circle[12];
private Circle[] p2p = new Circle[12];
private GridPane board = new GridPane();
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage){
buildBoard(board);
putSquaresOnBoard(board);
putPiecesOnBoard(board,p1p,p2p);
intializeListeners();
board.setPadding(new Insets(15,15,15,15));
Scene scene = new Scene(board, 700, 700);
primaryStage.setTitle("Checkers");
primaryStage.setScene(scene);
primaryStage.show();
}
private void intializeListeners() {
for (int i = 0; i < PIECES; i++){
p1p[i].setOnDragDetected(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent e){
for(int i = 0; i < PIECES; i++){
Dragboard db = p1p[i].startDragAndDrop(TransferMode.ANY);
ClipboardContent content = new ClipboardContent();
content.putString("Hello");
db.setContent(content);
System.out.println("setOnDragDetected");
}}
});
board.setOnDragOver(new EventHandler<DragEvent>(){
@Override
public void handle(DragEvent e){
e.acceptTransferModes(TransferMode.ANY);
System.out.println("dragOverDetected");
}
});
board.setOnDragDropped(new EventHandler<DragEvent>(){
@Override
public void handle(DragEvent e){
e.acceptTransferModes(TransferMode.ANY);
System.out.println("dropDetected");
}
});
}
}
private void buildBoard(GridPane board){
for(int i = 0; i < BOARD_DIM; i++){
RowConstraints rc = new RowConstraints();
rc.setMinHeight(SQUARES);
rc.setMaxHeight(SQUARES);
rc.setPrefHeight(SQUARES);
rc.setValignment(VPos.CENTER);
board.getRowConstraints().add(rc);
ColumnConstraints cc = new ColumnConstraints();
cc.setMinWidth(SQUARES);
cc.setMaxWidth(SQUARES);
cc.setPrefWidth(SQUARES);
cc.setHalignment(HPos.CENTER);
board.getColumnConstraints().add(cc);
}
}
private void putSquaresOnBoard(GridPane board) {
Color[] sqColors = new Color[]{Color.RED, Color.BLACK};
for (int i = 0; i < BOARD_DIM; i ++){
for (int j = 0; j < BOARD_DIM; j++){
board.add(new Rectangle(SQUARES,SQUARES,sqColors[(i+j)%2]),i,j);
}
}
}
private void putPiecesOnBoard(GridPane board, Circle[] p1p, Circle[] p2p) {
for (int i = 0; i < PIECES; i++){
p1p[i] = new Circle(SQUARES/2-4, Color.AZURE);
p1p[i].setStroke(Color.CORAL);
board.add(p1p[i], i % (BOARD_DIM / 2) * 2 + (2 * i / BOARD_DIM) % 2,
BOARD_DIM - 1 - (i * 2) / BOARD_DIM);
p2p[i] = new Circle(SQUARES/2-4, Color.CORAL);
p2p[i].setStroke(Color.AZURE);
board.add(p2p[i], i % (BOARD_DIM / 2) * 2 + (1 + 2 * i / BOARD_DIM) % 2,
(i * 2) / BOARD_DIM);
}
}
导入javafx.application.application;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.geometry.*;
导入javafx.scene.*;
导入javafx.scene.control.Cell;
导入javafx.scene.input.*;
导入javafx.scene.layout.*;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.*;
导入javafx.stage.stage;
公共类棋盘设置扩展了应用程序{
专用静态最终int板尺寸=8;
专用静态最终整数平方=64;
专用静态最终整数=12;
专用小区[][]boardG=新小区[8][8];
私人圈子[]p1p=新圈子[12];
私人圈子[]p2p=新圈子[12];
私有GridPane板=新建GridPane();
公共静态void main(字符串[]args){
发射(args);
}
公共无效开始(阶段primaryStage){
建筑板;
板(板);
单板(单板、p1p、p2p);
初始化Listeners();
板。设置填充(新插图(15,15,15,15));
场景=新场景(board,700700);
初级阶段。设置标题(“跳棋”);
初级阶段。场景(场景);
primaryStage.show();
}
私有无效初始化侦听器(){
for(int i=0;i
}这可能有助于您在问题中解释您到底被困在哪里?你面临的具体问题是什么?我编辑后添加了我的问题。我想不出如何让这些碎片移动。我已经看过了HelloDragAndDrop,但它没有任何帮助。也许这可以帮助你,你能在你的问题中解释一下你到底被困在哪里吗?你面临的具体问题是什么?我编辑后添加了我的问题。我想不出如何让这些碎片移动。我看了他一眼,但没用。