【发布时间】:2015-11-22 18:01:47
【问题描述】:
我在 Java 方面完全是新手,但我正在尝试构建一个跳棋游戏。我已经构建了一个板,但我不知道如何以及在哪里最好地添加某种事件侦听器以进行鼠标悬停和单击。
我使用了 Groovy 和 JavaFX 的组合,Groovy 是因为我喜欢它的语法,而 JavaFX 是因为它似乎是 Swing 更好的替代品。
class Window extends Application {
private int boardSize = 8
private int squareSize = 60
void start(Stage primaryStage) {
primaryStage.setTitle("Draughts")
GridPane checkerBoard = new GridPane()
checkerBoard.setPadding(new Insets(10,10,10,10))
configureBoardSpecs(checkerBoard)
layoutBoard(checkerBoard)
BorderPane root = new BorderPane(checkerBoard);
primaryStage.setScene(new Scene(root, 500, 500))
primaryStage.show()
}
private void layoutBoard(def checkerBoard) {
def fill = Color.WHITE
for (row in 0..boardSize-1) {
for (col in 0..boardSize-1) {
if ((row+col)%2) {
fill = Color.SADDLEBROWN
} else {
fill = Color.PERU
}
checkerBoard.add(new Rectangle(squareSize, squareSize, fill), col, row)
if (row % 2 != col % 2) {
if (row < 3) {
checkerBoard.add(new Circle(squareSize/2-4, Color.WHITE), col, row)
} else if (row > 4) {
checkerBoard.add(new Circle(squareSize/2-4, Color.BLACK), col, row)
}
}
}
}
}
private void configureBoardSpecs(def board) {
for (i in 0..boardSize-1) {
RowConstraints rowConstraints = new RowConstraints()
rowConstraints.setMinHeight(squareSize)
rowConstraints.setPrefHeight(squareSize)
rowConstraints.setMaxHeight(squareSize)
rowConstraints.setValignment(VPos.CENTER)
board.getRowConstraints().add(rowConstraints)
ColumnConstraints colConstraints = new ColumnConstraints()
colConstraints.setMinWidth(squareSize)
colConstraints.setMaxWidth(squareSize)
colConstraints.setPrefWidth(squareSize)
colConstraints.setHalignment(HPos.CENTER)
board.getColumnConstraints().add(colConstraints)
}
}
我更习惯于使用 jQuery 做这种事情,我会使用选择器来抓取任何类型的黑色圆圈并拥有它,所以当鼠标光标悬停在上面时,它会添加一个边框圆圈。然后在点击时,整个圆圈或包围矩形的颜色会改变颜色。
关于解决此问题的最佳方法的任何有用建议。
非常感谢,
【问题讨论】:
-
神秘的投票者如果对我的问题有什么不满意的地方,请发表评论
标签: java groovy javafx java-8 javafx-8