Question

I'm trying to make a Java desktop application where I want to shuffle image from project folder. I'm able to achieve this but I have to pass the name of pic but I don't want to pass any pic name in my code. I just want to use folder name and it should automatically retrieve all images from there.

Here is my code:

public class Main1 extends JFrame
{
      private java.util.List<BufferedImage> list = new ArrayList<BufferedImage>();
    private java.util.List<BufferedImage> shuffled;
    private JLabel label = new JLabel();
       private int width = 708;
    private int height = 800;
    private Timer timer = new Timer(4000, new ActionListener() {
      @Override
       public void actionPerformed(ActionEvent e) {
        update();
       }
   });

  public Main1(  )
  {
        super("Simple Timer");
      {
getContentPane().setBackground(new java.awt.Color(153,153,0));
} 
 try {
           list.add(resizeImage(ImageIO.read(new File("images\\1.jpg"))));
            list.add(resizeImage(ImageIO.read(new File("images\\2.jpg"))));
            list.add(resizeImage(ImageIO.read(new File("images\\3.jpg"))));
            list.add(resizeImage(ImageIO.read(new File("images\\4.jpg"))));
        } 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);
        jLabel6.setIcon(new ImageIcon(icon));
}
Was it helpful?

Solution

Assuming the files are stored on the file system (and based on your example they are), you can just grab a listing of files from a given directory...

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

From there you would just need to iterate over the list and add each one to the List...

for (File file : files) {
    list.add(resizeImage(ImageIO.read(file));
}

Have a look at

For more details...

OTHER TIPS

This code should work with Java 7

File dir = new File("images");
List<File> files = Arrays.asList(dir.listFiles(new FileFilter() {
   boolean accept(File pathName) {
      return pathName.getName().toLowerCase().endsWith(".jpg");
   }
}));
for (File f: files) {
   list.add(resizeImage(ImageIO.read(f)));
}
Collections.shuffle(list);

With Java 8 this code can be written as

File dir = new File("images");
List<File> files = Arrays.asList(dir.listFiles(
   (pathName) -> pathName.getName().toLowerCase().endsWith(".jpg");
));
for (File f: files) {
   list.add(resizeImage(ImageIO.read(f)));
}
Collections.shuffle(list);

and with NIO.2 and stream API:

Path folder = Paths.get("images");
List<Path> paths = Files.list(folder)
   .filter((path) -> path.toString().toLowerCase().endsWith(".jpg"))
   .collect(Collectors.asList());
for (Path path: paths) {
   list.add(resizeImage(ImageIO.read(path.toFile())));
}
Collections.shuffle(list);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top