我是新来的,我希望有人可以这么善良并帮助我。
我目前正在尝试创建一个跳棋游戏,因此需要创建棋盘。我已经检查了其他答案,但他们似乎从来没有在另一个窗口内创建电路板(我将在后面放置按钮以辞职并开始新游戏)。现在,只显示一个矩形,即使我已经在循环中创建了几个矩形。
有谁知道这是为什么? 这是我的代码
首先: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]);
}
}
其次,将主板添加到另一个窗口然后显示的主类:
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文件&#34; Base&#34;几乎是空的网格窗格:
<?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 :(得分:1)
所有矩形都在(0,0)
。您需要设置x
和y
坐标:
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