[英]How to shuffle list of images?
I'm trying to make a Java desktop application where I want to shuffle image from project folder. 我正在尝试创建一个Java桌面应用程序,我想从项目文件夹中移植图像。 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. 我能够实现这一点,但我必须传递pic的名称,但我不想在我的代码中传递任何图片名称。 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));
}
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
... 从那里你只需要迭代列表并将每个添加到List
...
for (File file : files) {
list.add(resizeImage(ImageIO.read(file));
}
Have a look at 看一下
For more details... 更多细节...
This code should work with Java 7 此代码应与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 使用Java 8,此代码可以编写为
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: 以及NIO.2和流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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.