如何按顺序随机播放图像? [英] How to Shuffle image in sequence?

查看:137
本文介绍了如何按顺序随机播放图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使Java桌面应用程序能够在其中进行自动图像混洗,我能够做到这一点,但是问题是它并没有混洗我给我想要混洗所有图像的所有图像 我该怎么做到

I am trying to make Java desktop application where i want to make auto image shuffle i am able to do this but problem is that it is not shuffling all image which i gave i want to shuffle all image how can i achieve this

以下是我在此处找到的一些代码:

Here is some code I found here:

/**
 * @see https://stackoverflow.com/a/22423511/230513
 * @see https://stackoverflow.com/a/12228640/230513
 */
public class ImageShuffle extends JPanel {

   private List<Icon> list = new ArrayList<Icon>();

   private JLabel label = new JLabel();
   private Timer timer = new Timer(1000, new ActionListener() {

      @Override
       public void actionPerformed(ActionEvent e) {
        update();
       }
   });

   public ImageShuffle() {
       this.setLayout(new GridLayout(1, 0));

      list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\e.jpg"));
    list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\d.jpg"));
    list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png"));
      list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\f.jpg"));
         //   list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.jpg"));
       //label.setIcon(UIManager.getIcon("OptionPane.informationIcon"));

       timer.start();
   }

   private void update() {

    Random r=new Random();
    int i1=(r.nextInt(3) +1);

    label.setIcon(list.get(i1));
}

private void display() {
    JFrame f = new JFrame("ImageShuffle");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(this);
    f.add(label);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

           @Override
          public void run() {
              new ImageShuffle().display();
           }
       });
   }
 }

预先感谢

推荐答案

首先创建一个随机列表...

Start by creating a shuffled list...

private List<Icon> list = new ArrayList<Icon>();
private List<Icon> shuffled;

//...

list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\e.jpg"));
list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\d.jpg"));
list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png"));
list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\f.jpg"));

shuffled = new ArrayList<Icon>();
update();

每次计时器滴答并调用actionPerformed时,弹出shuffled列表的第一个元素,直到什么都没留下...

Each time your timer ticks and calls actionPerformed, pop the first element of the shuffled list until nothing is left...

 if (shuffled.isEmpty()) {
    shuffled.addAll(list);
    // Prevent the current image from been selected next...
    shuffled.remove(label.getIcon());
    Collections.shuffle(shuffled);
 }

 Icon icon = shuffled.remove(0);
 label.setIcon(icon);

这将消除图像被允许连续显示多次的可能性.无论如何,您的随机值计算应该更像int i1 = (r.nextInt(list.size())); ...

This will remove the possibility of an image been allowed to be displayed multiple times in a row. Your random value calculation should have been more like int i1 = (r.nextInt(list.size())); anyway...

注释:

ImageShuffle不需要从JPanel扩展,您无需对其添加任何内容.这意味着您可以摆脱this.setLayout(new GridLayout(1, 0));f.add(this);并且程序仍然可以运行

ImageShuffle doesn't need to extend from JPanel, you not adding anything to it. This means you can get rid of this.setLayout(new GridLayout(1, 0)); and f.add(this); and the program should still run

这篇关于如何按顺序随机播放图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆