1

I am trying to shuffle image from my folder image which I am able to do. what I have to pass the name of image but I don't want pass the name of image I just want to give the name of folder and all image should from there how can I do this

Here is my code

public class main1 extends javax.swing.JFrame {

    private JLabel ecause = new JLabel();
    private List<BufferedImage> list = new ArrayList<BufferedImage>();
    private List<BufferedImage> shuffled;
    private JLabel label = new JLabel();
    private int width = 700;
    private int height = 700;
    private Timer timer = new Timer(4000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            update();
        }
    });

    public main1() {
        this.getContentPane().setBackground(new java.awt.Color(153, 153, 0));
        this.setUndecorated(true);
        ecause.setText("           eCause List");
        ecause.setBounds(0, 1278, 496, 88);
        ecause.setFont(new java.awt.Font("Times New Roman", 1, 40));
        ecause.setBackground(new java.awt.Color(255, 153, 0));
        ecause.setOpaque(true);
        this.add(ecause);
        initComponents();
        try {
            list.add(resizeImage(ImageIO.read(new File("images\\Picture2.png"))));
            list.add(resizeImage(ImageIO.read(new File("images\\Picture3.png"))));
            list.add(resizeImage(ImageIO.read(new File("images\\Picture4.png"))));
            list.add(resizeImage(ImageIO.read(new File("images\\Picture5.png"))));
        } catch (IOException e) {
            e.printStackTrace();
        }
        shuffled = new ArrayList<BufferedImage>(list);
        Collections.shuffle(shuffled);
        timer.start();

    }

    private BufferedImage resizeImage(BufferedImage originalImage) throws IOException {
        BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, width, height, null);
        g.dispose();
        return resizedImage;
    }

    private void update() {
        if (shuffled.isEmpty()) {
            shuffled = new ArrayList<BufferedImage>(list);
            Collections.shuffle(shuffled);
        }
        BufferedImage icon = shuffled.remove(0);
        jLabel3.setIcon(new ImageIcon(icon));
    }
}

How can I achieve my output?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3493831
  • 47
  • 1
  • 1
  • 8

2 Answers2

1

You can list every file contained in the folder like this :

File[] files = new File("images/").listFiles();

Note that it will also give the subdirectories.

So instead of

list.add(resizeImage(ImageIO.read(new File("images\\Picture2.png"))));
list.add(resizeImage(ImageIO.read(new File("images\\Picture3.png"))));
list.add(resizeImage(ImageIO.read(new File("images\\Picture4.png"))));
list.add(resizeImage(ImageIO.read(new File("images\\Picture5.png"))));

You can simply loop over each files given by listFiles method. You could also use listFiles(FileFilter) in order to filter out each file that isn't an image.

cheseaux
  • 5,187
  • 31
  • 50
0

I would add this as a comment but don't have the rep yet - doing

new File("images/myimage.png")

will not work if you package your code into a jar. To do this, you will need to use getResourceAsStream (http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html) and if your images folder is at your project root, you would do:

this.getClass().getResourceAsStream("/images")

The first slash here is important - it denotes the fact that you are going from the project root. If you just put "images", it would look from whatever class you put this code in (say you were in com.somepackage.blah, it would look in the blah package for your image).

Hope this helps!

Jarob22
  • 370
  • 1
  • 6
  • 13