Java实验(18) 幻灯片放映

来源:互联网 发布:福建广电网络宽带费用 编辑:程序博客网 时间:2024/04/30 10:46

写一个程序,将54张扑克牌图片,按照每次1张,每张1秒的放映间隔,按顺序循环显示图片。并且要求:(1)向上箭头加快放映速度;向下箭头减缓放映速度;(2)双击鼠标左键暂停放映,再次双击则继续。

程序界面自定。


1、读取图片

a)用一个大小为55ImagView数组存储读取的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);    }}


0 0
原创粉丝点击