gpt4 book ai didi

java - 使用 JavaFX 的窗口内的棋盘

转载 作者:行者123 更新时间:2023-12-02 13:16:17 26 4
gpt4 key购买 nike

我是新来的,我希望有人能好心帮助我。

我目前正在尝试创建一个跳棋游戏,因此需要创建棋盘。我已经检查过其他答案,但他们似乎从未在另一个窗口中创建棋盘(稍后我将在其中放置按钮以退出并开始新游戏)。现在,尽管我在循环中创建了多个矩形,但仅显示一个矩形。

有谁知道这是为什么吗?这是我的代码

第一个:Checkersboard 类,它创建棋盘的矩形

package application;

import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.StrokeType;

public class CheckersBoard extends Pane {

private int boardWidth = 10;
private int boardHeight = 10;


private Rectangle[][] board;


public CheckersBoard() {
// Declares new board
board = new Rectangle[boardWidth][boardHeight];

// Initializes new board
for(int x=0; x < boardWidth; x++){
for(int j=0; j < boardHeight; j++){
board[x][j] = new Rectangle();
board[x][j].setWidth(50);
board[x][j].setHeight(50);
board[x][j].setStroke(Color.TRANSPARENT);
board[x][j].setStrokeType(StrokeType.INSIDE);
board[x][j].setStrokeWidth(1);
}
}

// Generates colours for the chessboard backgrounds
for(int x=0; x < boardWidth; x++){
for(int j=0; j < boardHeight; j++){
if((x%2==0 && j%2==1) || (x%2==1 && j%2==0)){
board[x][j].setFill(Color.PINK);
}
else if((x%2==0 && j%2==0) || (x%2==1 && j%2==1)){
board[x][j].setFill(Color.DARKGRAY);
}
}
}

}

public void placeboard(final int i, final int j){
getChildren().add(board[i][j]);
}
}

二、将board添加到其他窗口然后显示的主类:

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import .... ;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
CheckersBoard board = new CheckersBoard();

GridPane root = (GridPane)FXMLLoader.load(getClass().getResource("/application/view/Base.fxml"));

for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
board.placeboard(i, j);
}
}
root.add(board, 0, 0);
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}

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

最后是我的 fxml 文件“Base”,其网格 Pane 几乎为空:

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.StackPane?>

<GridPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="test" GridPane.columnIndex="1" GridPane.rowIndex="2" />
</children>
</GridPane>

谢谢大家的帮助! :)

亲切的问候,丽莎

最佳答案

所有矩形都位于(0,0)处。您需要设置xy坐标:

board[x][j].setX(x * 50); // 50 being the width of each rectangle
board[x][j].setY(y * 50); // 50 being the height of each rectangle

关于java - 使用 JavaFX 的窗口内的棋盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43761825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com