javafx调整组件及其位置的大小

时间:2016-10-13 00:01:50

标签: image javafx resize screen

我正在搜索如何调整列的大小(红色和白色),并在调整窗口大小时保持其在棋盘上的位置。

正确

enter image description here

问题

enter image description here

代码架构和电路板上的列绑定。

//the points of columns in the board map
private int[][] whitepoints={{54,27},{235,27},{417,27}};
private int[][] redpoints={{145,27},{325,27},{507,27}};

StartBoard(Stage primaryStage){

    board.fitWidthProperty().bind(primaryStage.widthProperty());
    board.fitHeightProperty().bind(primaryStage.heightProperty());

    for(int i=0; i<3; i++){

        whiteArray[i]= new ImageView(white);
        whiteArray[i].setLayoutX(whitepoints[i][0]);
        whiteArray[i].setLayoutY(whitepoints[i][1]);

        redArray[i]= new ImageView(red);
        redArray[i].setLayoutX(redpoints[i][0]);
        redArray[i].setLayoutY(redpoints[i][1]);

    }

}

开始方法代码

Pane root = new Pane();

Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();

Scene scene = new
Scene(root,screenBounds.getWidth(),screenBounds.getHeight());

primaryStage.setTitle("Backgammon");
primaryStage.setScene(scene);
//primaryStage.setMaximized(true);
//primaryStage.setResizable(false);
primaryStage.show();

注释是阻止用户调整窗口大小的另一种方法。

关于其他技术的建议是可取的,因为我知道这不是正确的方法。

1 个答案:

答案 0 :(得分:0)

您可以使用GridPane来定位三角形。

private static Node createTriangle(boolean up, boolean red) {
    final double height = 100d;
    final double width = 40d;
    Polygon result = up ? new Polygon(
            0d, height,
            width / 2, 0d,
            width, height
    ) : new Polygon(
            0d, 0d,
            width / 2, height,
            width, 0d);

    result.setFill(red ? Color.RED : Color.WHITE);
    return result;
}

private static GridPane generateBoard() {
    GridPane board = new GridPane();

    ColumnConstraints columnConstraints = new ColumnConstraints();
    columnConstraints.setHgrow(Priority.ALWAYS);
    columnConstraints.setHalignment(HPos.CENTER);

    RowConstraints rowConstraintsTop = new RowConstraints();
    rowConstraintsTop.setVgrow(Priority.ALWAYS);
    rowConstraintsTop.setValignment(VPos.TOP);

    RowConstraints rowConstraintsBottom = new RowConstraints();
    rowConstraintsBottom.setVgrow(Priority.ALWAYS);
    rowConstraintsBottom.setValignment(VPos.BOTTOM);

    board.getColumnConstraints().addAll(Stream.generate(() -> columnConstraints).limit(6).toArray(ColumnConstraints[]::new));
    board.getRowConstraints().addAll(rowConstraintsTop, rowConstraintsBottom);

    board.addRow(0, IntStream.range(0, 6).mapToObj(i -> createTriangle(false, (i & 1) != 0)).toArray(Node[]::new));
    board.addRow(1, IntStream.range(0, 6).mapToObj(i -> createTriangle(true, (i & 1) == 0)).toArray(Node[]::new));

    board.setStyle("-fx-background-color: #444;");
    return board;
}

@Override
public void start(Stage primaryStage) {
    GridPane board1 = generateBoard(), board2 = generateBoard();
    HBox.setHgrow(board1, Priority.ALWAYS);
    HBox.setHgrow(board2, Priority.ALWAYS);
    HBox root = new HBox(20, board1, board2);
    root.setFillHeight(true);
    root.setPadding(new Insets(20));
    root.setStyle("-fx-background-color: brown;");

    Scene scene = new Scene(root, 800, 300);
    primaryStage.setScene(scene);
    primaryStage.show();
}