package imagetest; import java.io.IOException; import java.io.InputStream; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.application.Application; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleIntegerProperty; 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.BorderPane; import javafx.stage.Stage; import javafx.util.Duration; public class ImageTest extends Application { private static final Integer STARTTIME = 10; private IntegerProperty timeSeconds = new SimpleIntegerProperty(STARTTIME); private TimerEventHandler timerEventHandler; private ImageView imageView; private InputStream inputStream; /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } private void delayTime() { Timeline timeline = new Timeline(); timeline.getKeyFrames().add( new KeyFrame(Duration.seconds(STARTTIME), new KeyValue(timeSeconds, 0))); timeline.playFromStart(); timeline.setOnFinished(timerEventHandler); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); timerEventHandler = new TimerEventHandler(); imageView = new ImageView(); BorderPane root = new BorderPane(); root.setCenter(imageView); delayTime(); primaryStage.setScene(new Scene(root, 1024, 768)); primaryStage.show(); } private class TimerEventHandler implements EventHandler { private int count = 1; @Override public void handle(ActionEvent e) { inputStream = ClassLoader.getSystemResourceAsStream("imagetest/" + (count++) + ".jpg"); if (inputStream != null) { imageView.setImage(new Image(inputStream)); } if (count > 3) { count = 1; } try { if (inputStream != null) { inputStream.close(); } } catch (IOException ex) { System.out.println(ex.getMessage()); } delayTime(); } } }