【问题标题】:JavaFX rectangle doesn't updateJavaFX 矩形不更新
【发布时间】:2020-05-24 15:57:56
【问题描述】:

我正在尝试使用元胞自动机做一个迷宫解析器,但我遇到了显示问题 对于自动机的每一代网格,我们尝试以矩形的形式显示单元格。初始化效果很好,网格显示出来了,最后一代的模拟也显示了,但中间步骤没有显示。

第一代图片

上一代

//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

【问题讨论】:

  • 永远不要在 FX 应用程序线程上休眠。使用PauseTransitionTimeline 来实现这种功能。

标签: java animation javafx thread-sleep


【解决方案1】:

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

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

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

您的事件处理程序中的代码在 FX 应用程序线程上执行(由上述伪代码循环中的第一个块调用)。由于它在该线程上休眠,因此在整个事件处理程序完成之前,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 应用程序线程检查正在运行的动画时,它将检查是否该执行关键帧中的任何一个处理程序,如果是,则执行它们。然后它会像往常一样渲染场景。

【讨论】:

  • 谢谢。我已经找了3天了。我用 a 和 b 替换了 e,因为 e 已经被声明了。
猜你喜欢
  • 2014-10-15
  • 1970-01-01
  • 2021-10-22
  • 2013-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多