Arrays 来自数组的JAVAFX文本动画

Arrays 来自数组的JAVAFX文本动画,arrays,image,loops,animation,javafx,Arrays,Image,Loops,Animation,Javafx,这是一个正在进行的工作,所以我为我的代码需要清理表示歉意,但我认为最好包括到目前为止的所有内容 我试图通过循环一系列图像来演示如何为文本设置动画。我的代码在数组中循环,只显示最后一幅图像。我需要一次显示一个图像,并重复显示以获得所需的动画效果。我做错了什么或没有做什么 import javafx.animation.Timeline; import javafx.application.Application; import javafx.scene.Scene; import javafx.s

这是一个正在进行的工作,所以我为我的代码需要清理表示歉意,但我认为最好包括到目前为止的所有内容

我试图通过循环一系列图像来演示如何为文本设置动画。我的代码在数组中循环,只显示最后一幅图像。我需要一次显示一个图像,并重复显示以获得所需的动画效果。我做错了什么或没有做什么

import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.event.EventHandler;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;


public class ImageAnimatorWithAudio extends Application { 
    private final static int NUMBER_OF_SLIDES = 10;
    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
        Image[] images = new Image[NUMBER_OF_SLIDES];

        Timeline animation = new Timeline(new KeyFrame(Duration.millis(5000)));
        animation.setCycleCount(Timeline.INDEFINITE);
        animation.play();
        HBox hBox = new HBox();
        hBox.setSpacing(10);
        hBox.setAlignment(Pos.TOP_RIGHT); 
        Button btStartPause = new Button("Start Animation"); 
        hBox.getChildren().add(btStartPause); 

        //Create border pane
        BorderPane borderPane = new BorderPane(); 
        borderPane.setTop(hBox); //Add hBox to borderPane
        BorderPane.setAlignment(hBox, Pos.TOP_RIGHT); //Align hBox


        btStartPause.setOnAction(e -> {
            if (btStartPause.getText().equals("Start Animation")) {
                btStartPause.setText("Pause Animation");
                animation.play();

            } else {
                btStartPause.setText("Start Animation");
                animation.pause();
            }
        });


        GridPane pane = new GridPane();
        pane.setPadding(new Insets(5,5,5,5));
        for (int i = 0; i < NUMBER_OF_SLIDES; i++) {
            images[i] = new Image("image_path" + i + ".png"); //file names are numerically named(i)
            pane.getChildren().add(new ImageView(images[i]));
        }

        pane.getChildren().add(borderPane);

        Scene scene = new Scene(pane, 450, 450);
        primaryStage.setTitle("TextAnimation"); //Set the stage title
        primaryStage.setScene(scene); //Place the scene in the stage
        primaryStage.show(); //Display the stage

}

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

}
}

非常感谢您的帮助。

这是一个应用程序,演示了如何将图像添加到ArrayList中。类似的方法用于阵列。它还演示了如何将这些图像加载到ImageView中,并使用Timeline更改它们

主要

FXML

控制器

输出:如下所示


这到底是怎么回事?动画不做任何事情-它只是在5秒时有一个空的关键帧。如果你想一个接一个地显示图像,为什么要将它们全部添加到网格窗格?如果你只在单元格0,0中添加了子元素,为什么还要使用网格窗格?从一开始就提供最少的代码,而不是要求道歉这不是最小的。
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Sedrick
 */
public class JavaFXApplication1 extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="371.0" prefWidth="607.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="javafxapplication1.FXMLDocumentController">
    <children>
        <Button fx:id="btnMain" layoutX="263.0" layoutY="326.0" onAction="#handleButtonAction" text="Click Me!" />
        <Label fx:id="lblMain" layoutX="269.0" layoutY="28.0" minHeight="16" minWidth="69" />
      <ImageView fx:id="ivMain" fitHeight="201.0" fitWidth="278.0" layoutX="165.0" layoutY="85.0" pickOnBounds="true" preserveRatio="true" />
    </children>
</AnchorPane>
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.util.Duration;

/**
 *
 * @author Sedrick
 */
public class FXMLDocumentController implements Initializable {

    @FXML private Label lblMain;
    @FXML private ImageView ivMain;
    @FXML private Button btnMain;

    Timeline timeline;
    List<Image> imageContainer;
    int currentImageBeingDisplayed;

    @FXML
    private void handleButtonAction(ActionEvent event) {  
        lblMain.setText("Animation started!");
        currentImageBeingDisplayed = 0;
        timeline.play();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        //Load images
        imageContainer = new ArrayList();        
        for(int i = 1; i <= 12; i++)
        {
            try 
            {
                System.out.println("/images/Slide" + i + ".PNG");
                imageContainer.add(new Image(getClass().getResource("/images/Slide" + i + ".PNG").toURI().toString()));
            } catch (URISyntaxException ex) {
                System.out.println(ex.toString());
            }
        }

        //Change the image every second
        timeline = new Timeline(
            new KeyFrame(
                Duration.seconds(1),
                new EventHandler<ActionEvent>() {
                    @Override public void handle(ActionEvent actionEvent) {                        
                        Platform.runLater(()->{
                            ivMain.setImage(imageContainer.get(currentImageBeingDisplayed++));
                        });
                    }
                }
            )
        );
        timeline.setCycleCount(12);   
    }    

}