目录
题目:**16.23(创建一个有声的图像动画)
如图16-46b创建一个动画,满足以下要求:
- 允许用户在文本区域中指定动画速度。
- 用户输入帧数和图像文件名前缀。例如,如果用户输入的帧数为n,图像文件名的前缀为L,那么图像文件就是L1.gif,L2.gif一直到Ln.gif。假设这些图像都存在Image目录下,该目录是程序类目录的子目录。动画依次显示这些图像。
- 允许用户指定音频文件URL,动画开始时播放这个音频。
-
习题思路
- 创建一个HBox用于放置顶部的开始动画按钮。
- 创建一个ImageView作为以后的动画
- 创建一个GridPane放置五个Label和四个TextField
- 创建一个BorderPane,把HBox设置在顶部,把ImageView设置在中心,将GridPane设置在底部。
- 为顶部的开始按钮注册一个事件,当按钮被按下时,如果四个TextField都不为空,那么实例化一个Timeline和一个AudioClip。
-
Timeline循环调用一个更换图片的方法,方法内实例化一个Image()并设置在ImageView上,可以用一个int类型的参数来计数,当这个数值大于用户输入的帧数时,数值归为1;
-
代码示例
编程练习题16_23AudioVisualAnimation.java
package chapter_16;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.media.AudioClip;
import javafx.stage.Stage;
import javafx.util.Duration;
public class 编程练习题16_23AudioVisualAnimation extends Application{
private int speed;
private String prefix;
private int num;
private String URL;
private TextField tfSpeed;
private TextField tfPrefix;
private TextField tfNumber;
private TextField tfURL;
private String FilePath = "file:/C:/Users/Lenovo/eclipse-workspace/JavaFX/src/Image/";
private Timeline timeline;
private ImageView imageView;
private int count;
private AudioClip audioClip;
EventHandler<ActionEvent> handle = e ->{
show();
count++;
if(count > num)
count = 1;
};
@Override
public void start(Stage primaryStage) throws Exception {
Button btStart = new Button("Start Animation");
HBox hBox1 = new HBox(btStart);
hBox1.setAlignment(Pos.TOP_RIGHT);
GridPane gridPane = new GridPane();
gridPane.setVgap(5);
gridPane.setHgap(5);
Label lbInfo = new Label("Enter information for animation:");
tfSpeed = new TextField();
Label lbSpeed = new Label("Animation speed in milliseconds");
tfPrefix = new TextField();
Label lbPrefix = new Label("Image file prefix");
tfNumber = new TextField();
Label lbNumber = new Label("Number of images");
tfURL = new TextField();
Label lbURL = new Label("Audio file URL");
gridPane.setAlignment(Pos.CENTER);
gridPane.add(lbInfo, 0, 0);
gridPane.add(lbSpeed, 0, 1);
gridPane.add(lbPrefix, 0, 2);
gridPane.add(lbNumber, 0, 3);
gridPane.add(lbURL, 0, 4);
gridPane.add(tfSpeed, 1, 1);
gridPane.add(tfPrefix, 1, 2);
gridPane.add(tfNumber, 1, 3);
gridPane.add(tfURL, 1, 4);
imageView = new ImageView();
BorderPane borderPane = new BorderPane();
borderPane.setTop(hBox1);
borderPane.setCenter(imageView);
borderPane.setBottom(gridPane);
btStart.setOnAction(e ->{
if(tfSpeed.getText()!=""
&&tfPrefix.getText()!=""
&&tfNumber.getText()!=""
&&tfURL.getText()!="") {
speed = Integer.valueOf(tfSpeed.getText());
prefix = tfPrefix.getText();
num = Integer.valueOf(tfNumber.getText());
URL = tfURL.getText().replaceAll("\\\\", "/");
audioClip = new AudioClip("file:/"+URL);
audioClip.play();
timeline = new Timeline(new KeyFrame(Duration.millis(speed),handle));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
}
});
Scene scene = new Scene(borderPane,900, 1000);
primaryStage.setTitle("编程练习题16_23AudioVisualAnimation");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
public void show() {
Image i = new Image(FilePath+prefix+count+".jpg");
imageView.setImage(i);
}
}
-
结果展示
! 图片来源 !