【发布时间】:2015-12-29 19:21:42
【问题描述】:
我正在使用 GridPane 在 JavaFX 中创建棋盘游戏。
可以在网格的每个网格(单元格)中放置 7 种不同的动画。
最初的网格是这样的
在对动画插入进行编程之前,我测试了向其添加一个简单的圆圈。它看起来像这样
添加的节点是包含时间线动画的子场景。每个单元格大小为 40x40,SubScene 大小也是 40x40。
添加后的子场景,在网格窗格的边界线上,看起来不太好。
如何才能将节点添加到网格线下方?即网格线位于节点之上。
如果使用 GridPane 无法实现,还有什么我可以使用的吗?
我为游戏执行的类
class Game {
static GridPane grid;
public void start(final Stage stage) throws Exception {
int rows = 5;
int columns = 5;
stage.setTitle("Enjoy your game");
grid = new GridPane();
for(int i = 0; i < columns; i++) {
ColumnConstraints column = new ColumnConstraints(40);
grid.getColumnConstraints().add(column);
}
for(int i = 0; i < rows; i++) {
RowConstraints row = new RowConstraints(40);
grid.getRowConstraints().add(row);
}
grid.setOnMouseReleased(new EventHandler<MouseEvent> () {
public void handle(MouseEvent me) {
grid.add(Anims.getAnim(1), (int)((me.getSceneX() - (me.getSceneX() % 40)) / 40), (int)((me.getSceneY() - (me.getSceneY() % 40)) / 40)); //here the getAnim argument could be between 1-7
}
});
grid.setStyle("-fx-background-color: white; -fx-grid-lines-visible: true");
Scene scene = new Scene(grid, (columns * 40) + 100, (rows * 40) + 100, Color.WHITE);
stage.setScene(scene);
stage.show();
}
public static void main(final String[] arguments) {
Application.launch(arguments);
}
}
包含动画的类,这里我只是创建一个圆圈
public class Anims {
public static SubScene getAnim(final int number) throws Exception {
Circle circle = new Circle(20, 20f, 7);
circle.setFill(Color.RED);
Group group = new Group();
group.getChildren().add(circle);
SubScene scene = new SubScene(group, 40, 40);
scene.setFill(Color.WHITE);
return scene;
}
}
【问题讨论】:
-
如果没有代码,您的问题是不可能回答的:我们怎么知道您在做什么,而没有看到您是如何实现它的?创建一个 minimal reproducible example 以显示相同的行为并编辑您的问题以包含它。
-
@James_D 希望这会有所帮助
-
如果您发布 minimal reproducible example 会更容易,但那里有足够的东西来看看出了什么问题。
-
这个问题与How to create a background grid类似(可能重复?)。
-
使用子场景是一个非常实用的解决方案,通常子场景用于混合具有不同相机视角和照明的 2D/3D 场景(这似乎不是您在这里所做的),您可能可以从您的实现中摆脱 SubScenes,而只需使用常规容器节点,例如 Groups of Pane 子类。
标签: java animation javafx javafx-2 javafx-8