【问题标题】:KeyFrame and Duration in JavaFX Image SlideshowJavaFX 图像幻灯片中的关键帧和持续时间
【发布时间】:2016-03-11 20:40:34
【问题描述】:

我正在制作一个程序,其中有 3 张图像,它通过幻灯片放映,每张图像显示两秒钟。我的秒数与当前的秒数不同。但是我在声明 KeyFrame 的代码行中遇到了问题。我把我所有的代码都放在下面。关于我应该在我的代码中使用关键帧或其他任何内容进行更改的任何建议。这是使用 JavaFX。

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package digitalpictureframe;

import java.io.File;
//import java.time.Duration;
import java.util.Arrays;
import javafx.util.Duration;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @author Zachary Murphy
 */
public class DigitalPictureFrame extends Application {


    @Override
    public void start(Stage primaryStage) {

        Image image1 = new Image("1.png");
        Image image2 = new Image("2.png");
        Image image3 = new Image("3.png");
        ImageView imageView = new ImageView();
        Timeline timeline = new Timeline(

                new KeyFrame(Duration.ZERO, new KeyValue(imageView.imageProperty(), image1)),
            new KeyFrame(Duration.seconds(1), new KeyValue(imageView.imageProperty(), image2)),  
            new KeyFrame(Duration.seconds(2), new KeyValue(imageView.imageProperty(), image3)),
            new KeyFrame(Duration.seconds(4), new KeyValue(imageView.imageProperty(), null))
            );
        timeline.play();
        StackPane root = new StackPane();
        root.getChildren().add(imageView);
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}v

【问题讨论】:

  • 其他问题帖子对我没有帮助...
  • 它说不适合关键帧的构造函数。我该如何解决?
  • @jewelsea 另一个问题没有回答我的关键帧有错误。我之前看到过这个问题并尝试了解决方案,因此我问这个问题!
  • “不起作用”是什么意思?相关问题的两个答案都对我有用(OS X 10.9.5,Java Runtime 8u74)。如果您遇到错误或奇怪的行为,您应该在问题中包含编译错误或运行时堆栈跟踪以及意外行为的描述和环境的详细描述(使用的 JDK 版本等)。
  • 最好编辑问题并将格式化的堆栈跟踪放置在其中而不是在 cmets 中。

标签: java javafx duration timeline keyframe


【解决方案1】:

以下代码用于图像幻灯片。它会循环播放 25 张图像,然后单击暂停并再次单击重新开始。我还使用了图像之间的淡入淡出动画。每张图片会停留 2 秒。

import javafx.animation.Animation;
import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class lab12 extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    // Global ImageView array variable.
    ImageView[] imgView = new ImageView[25];
    int imgIndex = 0;

    public void start(Stage stage) {

        Pane pane = new Pane();

        for (int i = 0; i < 25; i++) {
            imgView[i] = new ImageView(new Image("imagescards/" + i + ".jpg"));
            imgView[i].setFitWidth(600);
            imgView[i].setFitHeight(600);

        }

        pane.getChildren().add(imgView[imgIndex]);

        EventHandler<ActionEvent> eventHandler = e -> {
            if (imgIndex < 24) {
                // Adding Children
                pane.getChildren().remove(imgView[imgIndex]);
                imgIndex++;
                pane.getChildren().add(imgView[imgIndex]);
                FadeTransition ft = new FadeTransition(Duration.millis(1000), imgView[imgIndex]);
                ft.setFromValue(0);
                ft.setToValue(1);
                ft.play();
            }
            else if (imgIndex == 24) {
                imgIndex = 0;
                pane.getChildren().remove(imgView[24]);
                pane.getChildren().add(imgView[imgIndex]);
                FadeTransition ft = new FadeTransition(Duration.millis(1000), imgView[imgIndex]);
                ft.setFromValue(0);
                ft.setToValue(1);
                ft.play();
            }
        };

        // Timeline Animation
        Timeline animation = new Timeline(new KeyFrame(Duration.millis(3000), eventHandler));

        animation.setCycleCount(Timeline.INDEFINITE);
        animation.play();

        pane.setOnMouseClicked(e -> {
            if (animation.getStatus() == Animation.Status.PAUSED) {
                animation.play();
            } else {
                animation.pause();
            }
        });

        Scene scene = new Scene(pane, 600, 600);

        stage.setScene(scene);
        stage.setTitle("Slide Show");
        stage.show();
    }
}

【讨论】:

    猜你喜欢
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多