JavaFX Application create slide show animation

Description

JavaFX Application create slide show animation

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {

   private final static int NUMBER_OF_SLIDES = 10;
   private int current = 0;
   private String[] slides = new String[NUMBER_OF_SLIDES];

   @Override//from w ww. j a v  a2s.  c  om
   public void start(Stage primaryStage) {
      // Load slide from files to strings
      loadTextToSlides();

      TextArea ta = new TextArea();
      ta.setWrapText(true);

      StackPane pane = new StackPane();
      pane.getChildren().add(new ScrollPane(ta));

      Scene scene = new Scene(pane, 400, 200);
      primaryStage.setTitle("java2s.com");
      primaryStage.setScene(scene);
      primaryStage.show();

      Timeline animation = new Timeline(new KeyFrame(Duration.millis(2000), e -> {
         ta.setText(slides[current]);
         current = (current + 1) % NUMBER_OF_SLIDES;
         ;
      }));
      animation.setCycleCount(Timeline.INDEFINITE);
      animation.play(); // Start animation

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

   private void loadTextToSlides() {
      for (int i = 0; i < NUMBER_OF_SLIDES; i++) {
         slides[i] = "text/slide" + i + ".txt";
      }
   }

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



PreviousNext

Related