Supported image formats: You can get these with static methods in the ImageIO class. Generally it is reading gif, jpg, png and writing jpg and png up through j2se 1.4. Add support for reading and writing bmp and wbmp in j2se 1.5 and for writing gif in j2se 1.6
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class LoadImages {
private JScrollPane getContent(BufferedImage image) {
JPanel panel = new JPanel(new GridLayout(1,0));
panel.add(new JLabel(new ImageIcon(image)));
panel.add(new JLabel(new ImageIcon(getImage())));
return new JScrollPane(panel);
}
private BufferedImage getImage() {
String path = "images/bclynx.jpg";
// ClassLoader looks up resource which must
// be located/available in the classpath.
URL url = getClass().getResource(path);
BufferedImage image = null;
try {
image = ImageIO.read(url);
} catch(IOException e) {
System.out.println("Read error: " + e.getMessage());
}
return image;
}
public static void main(String[] args) throws IOException {
String path = "images/mtngoat.jpg";
BufferedImage image = ImageIO.read(new File(path));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new LoadImages().getContent(image));
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}