JavaFX KeyFrame create slide show animation

Description

JavaFX KeyFrame create slide show animation


import java.io.File;
import java.util.Scanner;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {
   private int currentSlide = 0;

   @Override/*from   www .j a v  a2 s . co  m*/
   public void start(Stage primaryStage) {
      TextArea taSlide = new TextArea();
      Button btStart = new Button("Start");

      btStart.setOnAction(e -> {
         KeyFrame kf = new KeyFrame(Duration.millis(1000), f -> {
            File file = new File("slide" + currentSlide + ".txt");
            try (Scanner input = new Scanner(file);) {
               StringBuilder sb = new StringBuilder();
               if (input.hasNext()) {
                  sb.append(input.nextLine() + "\n");
               }
               taSlide.setText(sb.toString());
            } catch (Exception ex) {
               System.out.println(ex);
            }
            if (currentSlide == 9) {
               currentSlide = 0;
            } else {
               currentSlide++;
            }
         });

         Timeline timeline = new Timeline(kf);
         timeline.setCycleCount(Timeline.INDEFINITE);
         timeline.play();
         btStart.setDisable(true);
      });

      BorderPane pane = new BorderPane();
      pane.setCenter(taSlide);
      pane.setBottom(btStart);
      pane.setAlignment(btStart, Pos.CENTER);

      Scene scene = new Scene(pane);
      primaryStage.setTitle("java2s.com");
      primaryStage.setScene(scene);
      primaryStage.setResizable(false);
      primaryStage.show();
   }

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



PreviousNext

Related