【问题标题】:Shuffle elements [duplicate]随机播放元素[重复]
【发布时间】:2016-03-08 21:14:07
【问题描述】:

我进入堆栈后是否可以打乱我的元素? 所以,计划是每次显示随机图像。 我的代码如下:

/*
 * 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);
    }
}

基本上这段代码的作用是,它会一张一张地显示一些图像,每张 5 秒。我想显示它们,但被打乱了。

【问题讨论】:

  • 将它们添加到List<Image>,然后执行Collections.shuffle(list),然后然后将它们放入Timeline

标签: java javafx


【解决方案1】:

Collections 类有一个内置的 shuffle 方法。

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)))));

【讨论】:

  • 感谢安迪的指正!!
  • 谢谢,但是这个方法没有返回任何东西,这就是我使用它时出错的原因。
  • 如果你尝试这个 PyThon,你可能会发现它并没有像你想象的那样做。例如,Img4 将始终在 10 秒后显示。这是因为您正在改组关键帧,时间线将在内部按持续时间排序。可能您想要做的是将图像放入数组中,然后在基于数组创建关键帧之前对数组进行洗牌。
  • @jewelsea 我现在实际上正在尝试将图像放入一个数组中,然后我将尝试使用 shuffle 方法,但是我遇到了一些困难,因为我是 java 的新手,我是找不到将这些图像放入数组中的正确方法
  • 好的,我将添加一个重复问题的答案,它应该可以解决您的问题(除非其他人先这样做)。
猜你喜欢
  • 2011-04-26
  • 2019-04-17
  • 1970-01-01
  • 1970-01-01
  • 2012-12-16
  • 2015-04-20
  • 1970-01-01
  • 2011-11-10
  • 2017-11-11
相关资源
最近更新 更多