我是Java的總新手,但我試圖構建一個跳棋遊戲。我已經構建了一個板子,但我很難確定如何以及在哪裏最好爲鼠標懸停和點擊添加某種事件監聽器。JavaFX將事件監聽器添加到顏色爲黑色的圓圈
我已經使用了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的事情,我會用一個選擇抓取任何形式的圓,顏色發黑,並有這麼當鼠標光標懸停在,它會向圓圈添加邊框。然後點擊,整個圓或包圍矩形的顏色會改變顏色。
任何有用的建議,以最好的方式去做到這一點。
非常感謝,
神祕向下選民請評論,如果有什麼你不喜歡我的問題 –