JavaFX 矩形不更新

标签 java animation javafx thread-sleep

我正在尝试用元胞自动机做一个迷宫解析器,但我遇到了显示问题 对于自动机的每个新生成的网格,我们尝试以矩形的形式显示单元格。初始化工作良好并且显示网格,也显示了模拟的最后一代,但不显示中间步骤。

第一代图片

上一代

//Initialise la liste des rectangles
    public void initRectList() {

            for(int height = 0; height < this.mazeA.grid.getHeight(); height++) {
                for(int width = 0; width < this.mazeA.grid.getWidth(); width++) {

                this.rectList[height][width] = new Rectangle(REC_HEIGHT,REC_WIDTH, getColorCell(this.mazeA.grid, height, width));
            }
        }
    }

    //Dessine le labyrinthe
    public void drawGrid() {

        for (int height = 0; height < this.mazeA.grid.getHeight(); height++) {
            for(int width = 0; width < this.mazeA.grid.getWidth(); width++) {

                tilePane.getChildren().add(this.rectList[height][width]);

            }
        }
    }
public class MazeFX  {

    private HBox root = new HBox();
    private Scene scene = new Scene(root,1100,800);
    private TilePane tilePane = new TilePane();
    private Grid grid = new Grid(30,30);

    private Rectangle[][] rectList = new Rectangle[30][30];

    private VBox buttons = new VBox();
    private Button reset = new Button("Reset");
    private Button pas = new Button("Play");
    private Button load = new Button("Load");

    private MazeAutomaton mazeA;

    private final double REC_HEIGHT = 20.;
    private final double REC_WIDTH = 20.;


    public MazeFX(Stage stage) throws InterruptedException {

        scene.getStylesheets().add(getClass().getResource("/src/application.css").toExternalForm());
        initButton();
        initLayout();

        initGridByTopologie();

        mazeA = new MazeAutomaton(this.grid);

        initRectList();
        drawGrid();



        pressedButtons(stage);
        setWindow(stage);

        displayWindow(stage);

    }

要启动下一代,您只需按一个按钮。

//Action de l'utilisateur sur l'un des bouttons
    public void pressedButtons(Stage stage) {
        pressReset();
        pressPAS();
        pressLoad(stage);
    }

   //Boutton Play/Stop préssé
    public void pressPAS() {

        this.pas.setOnMouseClicked(e -> {
            for (int i = 0; i < 30; i++) {

                mazeA.nextStep();

                try {
                    Thread.sleep(100);
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
                updateRectColor();

            }
                }
        );
    }

问题似乎是我们陷入了 setOnMouseClicked() 方法中,并且矩形的更新尚未完成,通过文本显示,我看到了自动机的演变,这表明模拟有效并且问题似乎来自 JavaFX

最佳答案

JavaFX 应用程序线程作为循环运行。该循环实际上(实际的实现细节要复杂得多)执行以下操作(以伪代码形式):

while (applicationIsRunning()) {
    if (thereAreEventsToHandle()) {
        handleEvents();
    }
    if (thereAreAnimationsToUpdate()) {
        updateAnimations();
    }
    if (itsTimeToRenderTheScene()) {
        renderScene();
    }
}

默认情况下,JavaFX 每秒渲染场景最多 60 次。

事件处理程序中的代码在 FX 应用程序线程上执行(由上面伪代码循环中的第一个 block 调用)。由于它在该线程上 hibernate ,因此 FX 应用程序线程永远不会到达循环的第三部分(渲染场景),直到整个事件处理程序完成。因此,您永远不会看到中间更新,因为场景永远不会渲染。

假设mazeA.nextStep()不会阻塞(或需要很长时间才能运行),最好将其重构为 Animation ,例如一个Timeline :

public void pressPAS() {

    this.pas.setOnMouseClicked(e -> {
        KeyFrame updateMaze = new KeyFrame(Duration.ZERO, evt -> mazeA.nextStep());
        KeyFrame updateRect = new KeyFrame(Duration.millis(100), evt -> updateRectColor());
        Timeline timeline = new Timeline(updateMaze, updateRect);
        timeline.setCycleCount(30);
        timeline.play();
    });
}

timeline.play()方法只是启动动画并立即返回,从而允许 FX 应用程序线程继续进行。当 FX 应用程序线程检查正在运行的动画时,它将检查是否是时候执行关键帧中的任一处理程序,如果是,则执行它们。然后它将像往常一样渲染场景。

关于JavaFX 矩形不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62233926/

相关文章:

java - 一个图标中有多个动画图像?

javascript - 对于某些浏览器,元素不会消失

java - 如何在hibernate中从数据库中的字符串FQN实例化一个类?

java - 为什么一个方法引用了 "throws"... 也抛出的 ctor?

Java 无法将reactor.core.publisher.MonoDefer 转换为EntityClass

python - 保存 matplotlib.animation 输出 0 秒视频

JavaFX 应用程序显示奇怪的符号而不是俄语

java - 如何发出和处理自定义事件?

java - TextField 的 prefColumnCount 属性是什么?

java - DTD 文件中枚举和加号一起使用吗?