Java语言程序设计基础篇_编程练习题**16.23(创建一个有声的图像动画)

目录

题目:**16.23(创建一个有声的图像动画)

习题思路

代码示例 

 结果展示


题目:**16.23(创建一个有声的图像动画)

如图16-46b创建一个动画,满足以下要求:

  1. 允许用户在文本区域中指定动画速度。
  2. 用户输入帧数和图像文件名前缀。例如,如果用户输入的帧数为n,图像文件名的前缀为L,那么图像文件就是L1.gif,L2.gif一直到Ln.gif。假设这些图像都存在Image目录下,该目录是程序类目录的子目录。动画依次显示这些图像。
  3. 允许用户指定音频文件URL,动画开始时播放这个音频。
  • 习题思路
  1. 创建一个HBox用于放置顶部的开始动画按钮。
  2. 创建一个ImageView作为以后的动画
  3. 创建一个GridPane放置五个Label和四个TextField
  4. 创建一个BorderPane,把HBox设置在顶部,把ImageView设置在中心,将GridPane设置在底部。
  5. 为顶部的开始按钮注册一个事件,当按钮被按下时,如果四个TextField都不为空,那么实例化一个Timeline和一个AudioClip。
  6. 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);
		
	}
}
  •  结果展示

   图片来源  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值