Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.animation.KeyFrame;
- import javafx.animation.Timeline;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.layout.VBox;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- // Lab 21
- public class main extends Application {
- private int index = 0;
- private Image images[];
- private ImageView imgView;
- @Override
- public void start(Stage primaryStage) {
- VBox root = new VBox();
- root.setSpacing(10);
- root.setPadding(new Insets(20));
- root.setAlignment(Pos.CENTER);
- images = new Image[]{new Image("https://pngimg.com/uploads/flags/flags_PNG14655.png"),
- new Image("https://wiki2.railml.org/images/5/50/German_flag.png"),
- new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Flag_of_Saudi_Arabia.svg/800px-Flag_of_Saudi_Arabia.svg.png")};
- imgView = new ImageView(images[index]);
- imgView.setFitHeight(400);
- imgView.setFitWidth(600);
- Timeline animation = new Timeline(new KeyFrame(Duration.millis(1000), eventHandler));
- animation.setCycleCount(Timeline.INDEFINITE);
- Button PlayPause = new Button("Play");
- root.getChildren().addAll(imgView, PlayPause);
- Scene scene = new Scene(root);
- primaryStage.setScene(scene);
- primaryStage.setTitle("Image Animator");
- primaryStage.show();
- PlayPause.setOnMouseClicked(e -> {
- if (PlayPause.getText().equalsIgnoreCase("Play")) {
- animation.play();
- PlayPause.setText("Pause");
- }
- else {
- animation.pause();;
- PlayPause.setText("Play");
- }
- });
- }
- EventHandler<ActionEvent> eventHandler = e -> {
- if(index == images.length - 1)
- index = 0;
- else
- index++;
- imgView.setImage(images[index]);
- };
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement