I'm new to Swing and just need to know how to place an image on the home screen of my GUI. I don't want it to be the background. When my lecturer demoed it I think he placed it inside a JLabel. I saved the image to my laptop, but do I need to import it into the project I'm working on in Eclipse? If so, where is the best place to save it to? Thanks.
Put all the images in images
folder that you have in your project in parallel to src
folder.
F:/>Kiosk
|
|___src
|
|__main1.java
|
|__images
|
|__c.jpg
|__d.jpg
|__e.jpg
|__f.jpg
Use this code
ImageIO.read(new File("images\\c.jpg"));
ImageIO.read(new File("images\\d.jpg"));
You can try any one
// Read from same package
ImageIO.read(getClass().getResourceAsStream("c.png"));
// Read from absolute path
ImageIO.read(new File("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\c.png"));
// Read from images folder parallel to src in your project
ImageIO.read(new File("images\\c.jpg"));
All the above method returns BufferedImage
.
How to convert
BufferedImage
intoImageIcon
?
BufferedImage image = ImageIO.read(new File("images\\c.jpg"));
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);