Advertisement
iemadxi

Lab21

Mar 31st, 2021
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. import javafx.animation.KeyFrame;
  2. import javafx.animation.Timeline;
  3. import javafx.application.Application;
  4. import javafx.event.ActionEvent;
  5. import javafx.event.EventHandler;
  6. import javafx.geometry.Insets;
  7. import javafx.geometry.Pos;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.image.Image;
  11. import javafx.scene.image.ImageView;
  12. import javafx.scene.layout.VBox;
  13. import javafx.stage.Stage;
  14. import javafx.util.Duration;
  15. // Lab 21
  16. public class main extends Application {
  17. private int index = 0;
  18. private Image images[];
  19. private ImageView imgView;
  20. @Override
  21. public void start(Stage primaryStage) {
  22. VBox root = new VBox();
  23. root.setSpacing(10);
  24. root.setPadding(new Insets(20));
  25. root.setAlignment(Pos.CENTER);
  26.  
  27. images = new Image[]{new Image("https://pngimg.com/uploads/flags/flags_PNG14655.png"),
  28. new Image("https://wiki2.railml.org/images/5/50/German_flag.png"),
  29. new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Flag_of_Saudi_Arabia.svg/800px-Flag_of_Saudi_Arabia.svg.png")};
  30.  
  31. imgView = new ImageView(images[index]);
  32. imgView.setFitHeight(400);
  33. imgView.setFitWidth(600);
  34.  
  35. Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000), eventHandler));
  36. animation.setCycleCount(Timeline.INDEFINITE);
  37.  
  38. Button PlayPause = new Button("Play");
  39. root.getChildren().addAll(imgView, PlayPause);
  40.  
  41.  
  42. Scene scene = new Scene(root);
  43. primaryStage.setScene(scene);
  44. primaryStage.setTitle("Image Animator");
  45. primaryStage.show();
  46.  
  47. PlayPause.setOnMouseClicked(e -> {
  48.  
  49. if (PlayPause.getText().equalsIgnoreCase("Play")) {
  50. animation.play();
  51. PlayPause.setText("Pause");
  52. }
  53. else {
  54. animation.pause();;
  55. PlayPause.setText("Play");
  56. }
  57.  
  58.  
  59. });
  60. }
  61.  
  62.  
  63. EventHandler<ActionEvent> eventHandler = e -> {
  64. if(index == images.length - 1)
  65. index = 0;
  66. else
  67. index++;
  68.  
  69. imgView.setImage(images[index]);
  70.  
  71. };
  72.  
  73.  
  74. public static void main(String[] args) {
  75. launch(args);
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement