Imageview没有使用正确的大小进行初始化

时间:2017-07-30 23:25:08

标签: java javafx

我目前正在编写一个小游戏,其中间有一个大字段。

该字段显示一个网格窗格,其中有一个堆栈窗格绑定到每个" Cells",在其上添加了一个画布以便稍后绘制不同的字段类型。

字符显示为图像视图,这些图像视图被添加并带走以在场地周围移动它们。

问题是在加载(这意味着从文本文件中读取所有必要的数据)之前保存的游戏后,图像视图不适合堆栈窗格,这会导致字段失真。

以下内容包含初始化网格窗格,堆栈窗格和画布的代码。

    public class RealGUI {@Override
public void createGrid(GridPane grid, int row, int col) {
    this.gridPane = grid;
    System.out.println("col creategrid: " + col);
    System.out.println("row creategrid " + row);
    for (int i = 0; i < row; i++) {
        this.gridPane.getRowConstraints().add(new RowConstraints(0, Control.USE_COMPUTED_SIZE,
                Double.POSITIVE_INFINITY, Priority.ALWAYS, VPos.CENTER, true));
    }
    for (int i = 0; i < col; i++) {
        this.gridPane.getColumnConstraints().add(new ColumnConstraints(0, Control.USE_COMPUTED_SIZE,
                Double.POSITIVE_INFINITY, Priority.ALWAYS, HPos.CENTER, true));
    }
    colcount = gridPane.getColumnConstraints().size();
    rowcount = gridPane.getRowConstraints().size();
    this.stackPane = initImageStackPane();
    initCanvases();
}



private StackPane[][] initImageStackPane() {
    StackPane[][] square = new StackPane[colcount][rowcount];
    // bind each Imageview to a cell of the gridpane
    for (int y = 0; y < colcount; y++) {
        for (int x = 0; x < rowcount; x++) {
            square[y][x] = new StackPane();
            square[y][x].setStyle("-fx-background-color: black;");
            square[y][x].setMouseTransparent(true);
            square[y][x].setMinSize(0, 0);
            gridPane.add(square[y][x], y, x);
        }
    }
    return square;
}

private void initCanvases() {
    this.canvas = new ResizableCanvas[colcount][rowcount];
    for (int y = 0; y < colcount; y++) {
        for (int x = 0; x < rowcount; x++) {
            this.canvas[y][x] = new ResizableCanvas();
            this.canvas[y][x].setMouseTransparent(true);
            this.stackPane[y][x].getChildren().add(this.canvas[y][x]);
            this.canvas[y][x].widthProperty().bind(stackPane[y][x].widthProperty());
            this.canvas[y][x].heightProperty().bind(stackPane[y][x].heightProperty());

        }
    }
}}

这是在上一个代码

之后调用的
public class EditorBoard{
public void loadField(File file, int ySize, int xSize) throws IOException {
    load = new fileLoader(file, ySize, xSize);
    this.board = load.getBoard();
    this.monsterPosi[0] = load.getMonster().getY();
    this.monsterPosi[1] = load.getMonster().getX();
    this.treasurePosi = load.getTreasurePosi();
    changeGUIafterLoad();
    this.game.uncoverFieldEditor(monsterPosi[1], monsterPosi[0], FieldTypeEnums.MONSTER, false);
}

private void changeGUIafterLoad() {
    for (int y = 0; y < board.length; y++) {
        for (int x = 0; x < board[0].length; x++) {
            game.uncoverFieldEditor(x, y, board[y][x].getField(),
                    board[y][x].getEnergyStone());
        }
    }
}



public class RealGUI{
@Override
public void uncoverFieldEditor(int y, int x, FieldTypeEnums fieldType, boolean energy) {
    switch (fieldType) {
        case CAVE:

            this.canvas[y][x].draw(fieldType, energy);
            break;
        case FOREST:
            this.canvas[y][x].draw(fieldType, energy);
            break;
        case GRAS:
            this.canvas[y][x].draw(fieldType, energy);
            break;
        case LIANE:
            this.canvas[y][x].draw(fieldType, energy);
            break;
        case MOUNTAIN:
            this.canvas[y][x].draw(fieldType, false);
            break;
        case RIVER:
            this.canvas[y][x].draw(fieldType, false);
            break;
        case TREASURE:
            this.canvas[y][x].draw(fieldType, false);
            break;
        case MONSTER:
            // imageview with the monster that is moved on the field of the game / editor
            this.mon = new ImageView(imgMonster);
            this.mon.minHeight(0.0);
            this.mon.minWidth(0.0);

            this.mon.setPreserveRatio(false);
            this.mon.setSmooth(true);
            stackPane[y][x].getChildren().add(mon);
            this.mon.fitHeightProperty().bind(stackPane[y][x].heightProperty().multiply(0.7));
            this.mon.fitWidthProperty().bind(stackPane[y][x].widthProperty().multiply(0.7));

        default:
            break;
    }

}
}
我试图找到导致这个问题的真正原因。我希望提供的代码片段足以找到问题。

感谢任何花时间看这个的人。

0 个答案:

没有答案