0

Is it possible to shuffle my elements after i enter to the stack ? SO, the plan is every-time to display random images. my code below:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package imagedisplay;

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 ImageDisplay extends Application {

    @Override
    public void start(Stage primaryStage) {
        Image Img1 = new Image("file:lib/1.jpg");
        Image Img2 = new Image("file:lib/2.jpg");
        Image Img3 = new Image("file:lib/3.jpg");
        Image Img4 = new Image("file:lib/4.jpg");
        ImageView ViewImg = new ImageView();
        Timeline tl = new Timeline(

                new KeyFrame(Duration.seconds(25), new KeyValue(ViewImg.imageProperty(), Img1)),
                new KeyFrame(Duration.seconds(20), new KeyValue(ViewImg.imageProperty(), Img2)),  
                new KeyFrame(Duration.seconds(15), new KeyValue(ViewImg.imageProperty(), Img3)),
                new KeyFrame(Duration.seconds(10), new KeyValue(ViewImg.imageProperty(), Img4)),   
                new KeyFrame(Duration.seconds(5), new KeyValue(ViewImg.imageProperty(), null)));
        tl.play();
        StackPane st = new StackPane();
        st.getChildren().add(ViewImg);
        primaryStage.setScene(new Scene(st, 800, 600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Basically what this code does is , it displays some images one by one for 5 seconds each. I want to display them but shuffeled.

dailyadd
  • 91
  • 10

1 Answers1

-1

Collections class has a built in shuffle method.

Timeline tl = new Timeline(
    Collections.shuffle(Arrays.asList(
            new KeyFrame(Duration.seconds(25), new KeyValue(ViewImg.imageProperty(), Img1)),
            new KeyFrame(Duration.seconds(20), new KeyValue(ViewImg.imageProperty(), Img2)),  
            new KeyFrame(Duration.seconds(15), new KeyValue(ViewImg.imageProperty(), Img3)),
            new KeyFrame(Duration.seconds(10), new KeyValue(ViewImg.imageProperty(), Img4)),   
            new KeyFrame(Duration.seconds(5), new KeyValue(ViewImg.imageProperty(), null)))));
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
PyThon
  • 1,007
  • 9
  • 22
  • Thanks for correcting Andy!! – PyThon Mar 08 '16 at 21:28
  • Thanks but this method does not return anything, thats why im getting an error using it. – dailyadd Mar 08 '16 at 21:35
  • If you try this PyThon you will probably find that it does not do what you think it will do. For example, Img4, will always be displayed after a duration of 10 seconds. This is because you are shuffling keyframes, which a timeline will internally sort by duration. Probably what you want to do is put the images in an array, then shuffle the array before creating the keyframes based upon the array. – jewelsea Mar 08 '16 at 21:53
  • @jewelsea im actually trying to put the images in an array right now then i will try to use the shuffle method, however i am having some difficulties with that because i am a newbie in java and i am not finding a proper way to put these images in an array – dailyadd Mar 08 '16 at 22:16
  • 1
    OK, I'll add an answer to the duplicate question which should address your issue (unless somebody else does it first). – jewelsea Mar 08 '16 at 22:24