屏幕上只呈现最后一个StackPane(JavaFX)
我试图显示一个棋盘上有多个皇后,但它只显示其中一个。我有一个2D屏幕上只呈现最后一个StackPane(JavaFX),java,javafx,Java,Javafx,我试图显示一个棋盘上有多个皇后,但它只显示其中一个。我有一个2Dint数组,我穿过它,只想在被占用的方块上渲染一个皇后 public class Test5 extends Application { @Override public void start(Stage primaryStage) { Label Queen = new Label("Q"); Queen.setTextFill(Color.web("#0076a3"));
int
数组,我穿过它,只想在被占用的方块上渲染一个皇后
public class Test5 extends Application {
@Override
public void start(Stage primaryStage) {
Label Queen = new Label("Q");
Queen.setTextFill(Color.web("#0076a3"));
Queen.setFont(new Font(30));
GridPane root = new GridPane();
Random random = new Random();
primaryStage.setScene(new Scene(root, 1000, 1000));
int[][] b = {
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
};
final Animation animation = new Transition() {
{
setCycleDuration(Duration.INDEFINITE);
}
protected void interpolate(double frac) {
//Iterate over loop and whenever a position is occupied add Queen to that particular StackPane
for(int i = 0; i < b.length; i++) {
for(int j = 0; j < b.length; j++) {
if(b[i][j] == 1) {
StackPane place = (StackPane)root.getChildren().get(i * 10 + j);
place.getChildren().add(Queen);
}
}
}
try {
Thread.sleep(2000);
}
catch(Exception e) {
};
}
};
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 10; col ++) {
StackPane square = new StackPane();
String color ;
if ((row + col) % 2 == 0) {
color = "white";
} else {
color = "black";
}
square.setStyle("-fx-background-color: "+color+";");
root.add(square, col, row);
}
}
for (int i = 0; i < 10; i++) {
root.getColumnConstraints().add(new ColumnConstraints(10, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, HPos.CENTER, true));
root.getRowConstraints().add(new RowConstraints(10, Control.USE_COMPUTED_SIZE, Double.POSITIVE_INFINITY, Priority.ALWAYS, VPos.CENTER, true));
}
animation.play();
primaryStage.show();
}
public static void main(String[] args) {
Test5.launch();
}
}
公共类Test5扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
标签皇后=新标签(“Q”);
Queen.setTextFill(Color.web(“#0076a3”);
Queen.setFont(新字体(30));
GridPane root=新的GridPane();
随机=新随机();
原始阶段。设置场景(新场景(根,10001000));
int[]b={
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
{0, 0, 1, 1, 1, 0, 0, 1, 0, 1},
};
最终动画=新过渡(){
{
设置周期(持续时间不确定);
}
受保护的孔隙插值(双重压裂){
//迭代循环,每当某个位置被占用时,将Queen添加到该特定的StackPane
for(int i=0;i
您无法在FX应用程序线程上睡眠;您可以阻止它渲染。此外,不能将同一节点多次添加到场景图中。现在还不清楚你想在这里做什么,但是你误解了动画API的工作原理。是的,我创建了一个新节点,现在它可以工作了。调整大小时速度非常慢,这可能是由于Thread.sleep()
。在呈现另一个配置之前,是否有其他方法可以等待。感谢您的输入。请使用。