JavaFX在for循环中向anchorpane添加节点,但每次迭代都要等待几秒钟才能添加下一个节点



我正在制作一个游戏,应该发生的是,在遍历数组列表的同时,在for循环中。在每个循环中,我都想向场景中添加一个节点,然后等待一段时间,再向场景添加另一个节点。每次迭代之间的时间也在arraylist的项中定义,每次迭代可以不同。

我尝试过的:

//Circle is my own class, other than these attributes along with getters it has nothing in it.
//The last number is the time to wait.
//Also for testing i used a static arraylist, normally the items come from a json file.
ArrayList<Circle> lvlNodes = new ArrayList<>();
lvlNodes.add(new Circle(50,50, Color.RED,250,100));
lvlNodes.add(new Circle(200,100, Color.BLUE,500,250));
lvlNodes.add(new Circle(900,500, Color.YELLOW,750,500));
lvlNodes.add(new Circle(400,50, Color.GREEN,1000,500));
//Iterating over the arraylist and adding the nodes.
for (int i=0; i<lvlNodes.size(); i++) {
Circle currentNode = lvlNodes.get(i);
//Add the node the the anchor pane.
view.addLvlNode(currentNode, diameter); //diameter is a final value, all nodes in the game are the same and thus only added once at the top of the json file
//Wait and move on to the next one.
try {
Thread.sleep(currentNode.getTimeToNextNode())
} catch (InterruptedException e) {
System.out.println(e);
}

}

//Adds the next node to the game.
public void addLvlNode(Circle circle, double diameter) {
//Create node.
javafx.scene.shape.Circle lvlNode = new javafx.scene.shape.Circle(circle.getPosX(), 
circle.getPosY(), diameter/2);
//Anchor node the the anchorpane.
AnchorPane.setTopAnchor(lvlNode, (double)circle.getPosY());
AnchorPane.setLeftAnchor(lvlNode, (double)circle.getPosX());
anchrLevel.getChildren().add(lvlNode);
lvlNode.toBack();
}//addLvlNode.

Thread.sleep((是有效的,for循环中的每次迭代都有介于两者之间的时间。但是直到for循环完成迭代,节点才会被添加。

有没有任何方法可以在for循环中添加节点,时间介于两者之间?

您遇到的问题是,您的所有代码都在FXAT上运行,而当FXAT忙于运行该代码时,它无法执行诸如更新屏幕以实际添加圆圈之类的操作。因此,当您的代码完成时,包括所有的Thread.sleep((调用,它会同时完成所有的屏幕绘制。

我没有构建自定义的Circle类,而是构建了一些数组来容纳Circles和等待时间。然后我把addLvlNode&进入后台工作。后台作业使用Platform.runLater((将节点添加到FXAT:上的窗格中

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import java.util.ArrayList;
public class CircleMaker extends Application {
private Pane mainPain;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
ArrayList<Circle> circles = new ArrayList<>();
ArrayList<Integer> waitTimes = new ArrayList<>();
circles.add(new Circle(50, 50, 125, Color.RED));
waitTimes.add(1000);
circles.add(new Circle(200, 200, 250, Color.BLUE));
waitTimes.add(2500);
circles.add(new Circle(900, 500, 375, Color.YELLOW));
waitTimes.add(5000);
circles.add(new Circle(400, 50, 500, Color.GREEN));
waitTimes.add(5000);
Thread waitingThread = new Thread(() -> {
for (int idx = 0; idx < 4; idx++) {
Circle thisCircle = circles.get(idx);
Platform.runLater(() -> addLvlNode(thisCircle));
try {
Thread.sleep(waitTimes.get(idx));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
mainPain = new Pane();
primaryStage.setScene(new Scene(mainPain, 1000, 800));
primaryStage.show();
waitingThread.start();
}
private void addLvlNode(Node circle) {
mainPain.getChildren().add(circle);
circle.toBack();
}
}

我还将等待时间延长了10倍,这样更容易看到每个圆圈都是轮流画的,中间有一个停顿。

你可能可以使用Timeline来做这件事,但由于你说Circle数据最终将是来自外部源的JSON,这对我来说意味着你可能已经有了某种非JavaFX机制,所以最好只使用后台线程来做你需要的任何事情。你可以从这个例子中看到基本框架是什么

最新更新