我正在制作一个程序,其中有3个图像,并通过幻灯片显示每个图像显示两秒钟。我的秒数与目前的秒数不同。但是我的代码行声明KeyFrame有问题。我把所有代码都放在了下面。关于我应该使用关键帧或我的代码中的其他任何内容更改的建议。这是使用JavaFX。
5
答案 0 :(得分:0)
以下代码适用于图片幻灯片。它循环显示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();
}
}