Java实验(18) 幻灯片放映
来源:互联网 发布:福建广电网络宽带费用 编辑:程序博客网 时间:2024/04/30 10:46
写一个程序,将54张扑克牌图片,按照每次1张,每张1秒的放映间隔,按顺序循环显示图片。并且要求:(1)向上箭头加快放映速度;向下箭头减缓放映速度;(2)双击鼠标左键暂停放映,再次双击则继续。
程序界面自定。
1、读取图片
a)用一个大小为55的ImagView数组存储读取的54张图片(0号位置不使用)。
ImageView []imag=new ImageView[55];
b)读取图片的方法:
public static ImageView readImageFromJar(Stringjarname, Stringpicname){
ImageView imageView =null;
try {
JarFile jarFile =new JarFile(jarname);
JarEntry entry =jarFile.getJarEntry(picname);
InputStream in =jarFile.getInputStream(entry);
imageView =new ImageView(new Image(in));
in.close();
jarFile.close();
}
catch (IOExceptione) {
System.err.println("read file error.");
}
return imageView;
}
c)读取图片到imag[]
for(int i=1;i<=54;i++){
String card=new String("cards/"+String.valueOf(i)+".png");
imag[i]=readImageFromJar("cards.jar",card);
}
2、事件
a)自动放映图片,事件间隔1s
EventHandler<ActionEvent> eventHandler =e->{
if(n==55)
n=1;
pane.getChildren().add(imag[n]);
n++;
};
Timeline animation =new Timeline(new KeyFrame(Duration.seconds(1),eventHandler));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
b)双击暂停和开始
pane.setOnMouseClicked(e->{
if(animation.getStatus()==Animation.Status.PAUSED &&e.getClickCount()==2){
animation.play(); //双击原本暂停的开始
}
else if(e.getClickCount()==2){
animation.pause(); //双击暂停
}
});
c)加速(UP)和减速(DOWN)
pane.setOnKeyPressed(e->{
switch(e.getCode()){
case UP:animation.setRate(animation.getRate()+1);
case DOWN:animation.setRate(animation.getRate()-1);
}
});
3、补充说明
a)用一个全局变量n记录卡牌序号,当播放到第54张后返回1。
b)将焦点集中在pane上,可以根据鼠标指令进行相对的事件处理。
import java.io.IOException;import java.io.InputStream;import java.util.jar.JarEntry;import java.util.jar.JarFile;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.scene.Scene;import javafx.scene.image.Image;import javafx.scene.image.ImageView;import javafx.scene.layout.StackPane;import javafx.stage.Stage;import javafx.util.Duration;public class DisplayCards extends Application { int n=1; ImageView []imag=new ImageView[55]; public static ImageView readImageFromJar(String jarname, String picname){ ImageView imageView = null; try { JarFile jarFile = new JarFile(jarname); JarEntry entry = jarFile.getJarEntry(picname); InputStream in = jarFile.getInputStream(entry); imageView = new ImageView(new Image(in)); in.close(); jarFile.close(); } catch (IOException e) { System.err.println("read file error."); } return imageView; } @Override public void start(Stage primaryStage) { StackPane pane = new StackPane(); for(int i=1;i<=54;i++){ String card=new String("cards/"+String.valueOf(i)+".png"); imag[i]=readImageFromJar("cards.jar",card); } EventHandler<ActionEvent> eventHandler = e->{ if(n==55) n=1; pane.getChildren().add(imag[n]); n++; }; Timeline animation = new Timeline(new KeyFrame(Duration.seconds(1),eventHandler)); animation.setCycleCount(Timeline.INDEFINITE); animation.play(); pane.setOnMouseClicked(e->{ if(animation.getStatus()==Animation.Status.PAUSED && e.getClickCount()==2){ animation.play(); } else if(e.getClickCount()==2){ animation.pause(); } }); pane.setOnKeyPressed(e->{ switch(e.getCode()){ case UP:animation.setRate(animation.getRate()+1); case DOWN:animation.setRate(animation.getRate()-1); } }); Scene scene = new Scene(pane, 300, 250); primaryStage.setTitle("DisplayCards"); primaryStage.setScene(scene); primaryStage.show(); pane.requestFocus(); } public static void main(String[] args) { launch(args); }}
- Java实验(18) 幻灯片放映
- 幻灯片放映
- 只想放映幻灯片的几张
- 幻灯片放映时无法切换到下一张
- Android Gesture手势操作(手势翻页效果)及幻灯片放映
- 在幻灯片放映过程中动态插入内容
- PPT幻灯片放映方式的典型问题及扩展研究
- PPT幻灯片放映不显示备注,只让备注显示在自己屏幕上!
- PPT幻灯片放映不显示备注,只让备注显示在自己屏幕上!
- 【VC++游戏开发#二】2D篇 —— 平滑的幻灯片放映效果
- Impress.js上手 - 抛开PPT、制作Web 3D幻灯片放映
- js实验2.(1)幻灯片切换实现
- #童游大放映
- Java语言编写的幻灯片
- 幻灯片
- 幻灯片:Why Java Sucks and C# Rocks
- PowerPoint 演讲放映技巧
- Picture Flash(图片放映)
- OpenGL入门(三):绘制一个三角形
- python学习第二章(使用字符串)
- Maximum Subarray(求数组的子数组之和的最大值)
- 备份,VA自用文件头
- MySQL sql-mode
- Java实验(18) 幻灯片放映
- DataX学习笔记-Reader插件开发
- linux shell函数
- SICP 习题2.32 subsets 寻找子集
- Mysql存储引擎
- RxBus简单实现
- Python 3 中print 函数
- 机器分配
- 关于bootstrap框架css无法引用问题