I am trying to make a Java desktop application where I have a JLabel
where I shuffle images. I am retrieving images from a folder which is on any drive of my PC. Now I want to retrieve all the images from a project folder where the images' folder name is available.
F:/>Kiosk
|
|___src
|
|___lib
|
|___koisk
|
|__main1.java
|
|__images
Here is my code
How can i achieve this?
public class ImageShuffle1 extends JPanel {
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(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
update();
}
});
public ImageShuffle1() {
try {
list.add(resizeImage(ImageIO.read(new File("E:\SOFTWARE\TrainPIS\res\drawable\c.jpg"))));
list.add(resizeImage(ImageIO.read(new File("E:\SOFTWARE\TrainPIS\res\drawable\d.jpg"))));
list.add(resizeImage(ImageIO.read(new File("E:\SOFTWARE\TrainPIS\res\drawable\e.jpg"))));
list.add(resizeImage(ImageIO.read(new File("E:\SOFTWARE\TrainPIS\res\drawable\f.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);
label.setIcon(new ImageIcon(icon));
}
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 ImageShuffle1().display();
}
});
}
}
Thanks in advance
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…