0

This is my code

    ImageView contImg = new ImageView();
    Timeline timeline = new Timeline(
    new KeyFrame(javafx.util.Duration.ONE, new KeyValue(contImg.imageProperty(), cartas.get(0).getImg())),
    new KeyFrame(javafx.util.Duration.ONE, new KeyValue(contImg.imageProperty(), cartas.get(1).getImg())),
    new KeyFrame(javafx.util.Duration.ONE, new KeyValue(contImg.imageProperty(), cartas.get(2).getImg())),
    new KeyFrame(javafx.util.Duration.ONE, new KeyValue(contImg.imageProperty(), cartas.get(3).getImg())));

    timeline.play();

but the problem is that the image don't change (cartas is a Arraylist where each cart have a image how attribute)

ROOT
  • 11,363
  • 5
  • 30
  • 45
  • Have you tried just setting the image on your image view to make sure that cartas contains different images? – matt Jan 09 '20 at 05:55
  • 1
    You're using Duration.ONE, which is 1 millisecond, it might just be cycling through them to fast to see. Why not use `Duration.seconds(n)`? – matt Jan 09 '20 at 05:59
  • Similar questions: [Slideshow from an ArrayList of images with JavaFX](https://stackoverflow.com/questions/27749060/slideshow-from-an-arraylist-of-images-with-javafx), [In order transition on ImageView in JavaFX](https://stackoverflow.com/questions/17536466/in-order-transition-on-imageview-in-javafx), [Timeline to Display Random images one by one in a slideshow](https://stackoverflow.com/questions/35869192/timeline-to-display-random-images-one-by-one-in-a-slideshow). – jewelsea Jan 09 '20 at 06:20
  • I've posted an approach here: https://stackoverflow.com/a/51611368/2991525 – fabian Jan 09 '20 at 06:48

1 Answers1

3

Your approach to use a timeline to change the image property of the image is correct, but your durations are wrong.

You use Duration.ONE, which is:

A Duration of 1 millisecond.

So that is not every N seconds as you wish.

To get every n seconds, use:

Duration.seconds(n)

The other issue is that you are setting the keyframes to all be triggered at the same duration and that is not what you want. The duration of the keyframe is based on time elapsed for the entire timeline cycle, not just since the last keyframe.

So putting this together you can get the following code to do what you want:

final int NUM_FRAMES = 4;
final int PAUSE_BETWEEN_FRAMES = 5;

ImageView imageView = new ImageView();
Timeline timeline = new Timeline();
List<Image> images = List.of(/** initialize your images here **/);

for (int i = 0; i < NUM_FRAMES; i++) {
    timeline.getKeyFrames().add(
        new KeyFrame(
            Duration.seconds(i * PAUSE_BETWEEN_FRAMES), 
            new KeyValue(imageView.imageProperty(), images.get(i))
        )
    );
}

timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();

List.of(elements..) is a Java 9 construct for initializing a list, use something else if you aren't using Java 9+.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • I know you mean well, but please don’t call me bro, I just don’t like that form of slang, thanks. – jewelsea Jan 22 '20 at 14:01