【发布时间】:2019-06-07 12:02:58
【问题描述】:
我的目标是创建一个六边形瓷砖场。我已经有了一个单元矩阵,每个单元都足够高以适应完整的六边形图像:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class UITest extends Application {
final private static String TILE_IMAGE_LOCATION = System.getProperty("user.dir") + File.separatorChar +"resources"+ File.separatorChar + "blueTile.png";
final private static Image HEXAGON_IMAGE = initTileImage();
private static Image initTileImage() {
try {
return new Image(new FileInputStream(new File(TILE_IMAGE_LOCATION)));
} catch (FileNotFoundException e) {
throw new IllegalStateException(e);
}
}
public void start(Stage primaryStage) {
int height = 4;
int width = 6;
GridPane tileMap = new GridPane();
Scene content = new Scene(tileMap, 800, 600);
primaryStage.setScene(content);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
ImageView tile = new ImageView(HEXAGON_IMAGE);
GridPane.setConstraints(tile, x, y);
tileMap.getChildren().add(tile);
}
}
primaryStage.show();
}
}
我的问题不在于垂直间隙,我可以通过将 GridPane 的 vGap() 添加到适当的值来确定。对我来说,困难是每第二行向右移动半个单元格宽度。
我尝试将两个 GridPanes 放在彼此之上,一个包含奇数行,一个包含偶数行,目的是为其中一个添加填充,完全移动它。然而,据我所知,除了将 GridPanes 嵌套到另一个上之外,没有办法做到这一点。
我怎样才能最好地实现每隔一行移动一次?
【问题讨论】: