Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
96 views
in Technique[技术] by (71.8m points)

java - How to retrieve image from project folder?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Put all the images in images folder that you have in your project in parallel to src folder.

 F:/>Kiosk
             |
             |___src
             |
             |___lib
             |
             |___koisk
                   |
                   |__main1.java
             |
             |__images
                |
                |__c.jpg
                |__d.jpg
                |__e.jpg
                |__f.jpg

Use this code

list.add(resizeImage(ImageIO.read(new File("images\c.jpg"))));
list.add(resizeImage(ImageIO.read(new File("images\d.jpg"))));
list.add(resizeImage(ImageIO.read(new File("images\e.jpg"))));
list.add(resizeImage(ImageIO.read(new File("images\f.jpg"))));

-- EDIT --

You can try any one

// Read from same package 
list.add(resizeImage(ImageIO.read(getClass().getResourceAsStream("c.png"))));

// Read from absolute path
list.add(resizeImage(ImageIO.read(new File("E:\SOFTWARE\TrainPIS\res\drawable\c.png"))));

// Read from images folder parallel to src in your project
list.add(resizeImage(ImageIO.read(new File("images\c.jpg"))));

Repeat any one for all four images.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...