【问题标题】:JavaFX 2 Timeline setcyclecount()JavaFX 2 时间线 setcyclecount()
【发布时间】:2015-02-20 08:01:41
【问题描述】:

我在 javase8 和 netbeans 8.0.2 中使用 javafx,我制作了随机生成的形状图像并按时间轴顺序显示它们。但最后一张图片没有显示。 timeline.setcyclecount(12) 我使用 java 生成 12 张图像,但没有显示 12. 时间轴中的图像。

public class JavaFXApplication3 extends Application {
        int k;
        Timeline timeline;

        class ResizableCanvas extends Canvas {
            private void draw() {
                int[] uyaran = {3, 7, 12};
                boolean[] type = new boolean[12];
                for (int i = 0; i < 12; i++) {
                    type[i] = false;
                }
                for (int v : uyaran) {
                    type[v - 1] = true;
                }
                double w = getWidth();
                double h = getHeight();
                GraphicsContext gc = getGraphicsContext2D();
                gc.clearRect(0, 0, w, h);
                gc.setFill(Color.RED);
                System.out.println(k);
                if (type[k]) {
                    gc.fillOval(0, 0, w, h);
                }
                k++;
            }
        }

        @Override
        public void start(Stage stage) throws Exception {
            k = 0;
            ResizableCanvas canvas = new ResizableCanvas();
            timeline = new Timeline(new KeyFrame(Duration.millis(1000), ae -> canvas.draw()));
            timeline.setCycleCount(12);
            timeline.setOnFinished(ActionEvent -> {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException ex) {}
                stage.close();
            });
            timeline.play();
            Pane pane = new Pane();
            pane.getChildren().add(canvas);
            canvas.widthProperty().bind(pane.widthProperty());
            canvas.heightProperty().bind(pane.heightProperty());
            stage.setScene(new Scene(pane));
            stage.show();
        }

}

【问题讨论】:

    标签: javafx java-8 netbeans-8


    【解决方案1】:

    您在 FX 应用程序线程上调用 Thread.sleep(...),这会阻塞线程并阻止其更新。最后的椭圆只有在暂停结束时才会真正呈现,但当然你会关闭窗口,这样你就永远看不到它了。

    使用PauseTransition 暂停,并在暂停结束时使用其onFinished 处理程序执行某些操作:

            timeline.setOnFinished(ae -> {
                PauseTransition pause = new PauseTransition(Duration.seconds(10));
                pause.setOnFinished(event -> stage.close());
                pause.play();
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 2014-04-24
      • 1970-01-01
      • 2013-08-20
      • 2018-05-19
      相关资源
      最近更新 更多