I want to display my images randomly one by one. The code below displays images one by one only if i put the duration time in seconds like:
new KeyFrame(Duration.seconds(1), new KeyValue(imageView.imageProperty(), image1)),
new KeyFrame(Duration.seconds(2), new KeyValue(imageView.imageProperty(), image2)),
new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image3)),
new KeyFrame(Duration.seconds(4), new KeyValue(imageView.imageProperty(), image4)),
So, my code will display image1 firstn image 2 second, image3 thirds and so on.
1) I want it to display random images every time.
2) Not to depend on duration time . Because if i put Duration.seconds(3)
to all of them it will display just the first one.
Code looks like this:
package imagedisplayy;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
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;
/**
*
* @author D
*/
public class ImageDisplayy extends Application {
@Override
public void start(Stage primaryStage) {
Image image1 = new Image("file:lib/1.jpg");
Image image2 = new Image("file:lib/2.jpg");
Image image3 = new Image("file:lib/3.jpg");
Image image4 = new Image("file:lib/4.jpg");
ImageView imageView = new ImageView();
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image1)),
new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image2)),
new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image3)),
new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), image4)),
new KeyFrame(Duration.seconds(3), new KeyValue(imageView.imageProperty(), null))
);
timeline.play();
StackPane root = new StackPane();
root.getChildren().add(imageView);
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}