// // ImageIOExample // // A JComponent that uses ImageIO to display an image // package com.knutejohnson.demos; import java.awt.*; import java.awt.image.*; import java.io.*; import java.net.*; import javax.imageio.*; import javax.swing.*; public class ImageIOExample extends JPanel { private BufferedImage image; public ImageIOExample(String fname) { try { image = ImageIO.read(new File(fname)); setPreferredSize(new Dimension( image.getWidth(),image.getHeight())); } catch (IOException ioe) { ioe.printStackTrace(); } } public ImageIOExample(URL url) { try { image = ImageIO.read(url); setPreferredSize(new Dimension( image.getWidth(),image.getHeight())); } catch (IOException ioe) { ioe.printStackTrace(); } } public void paint(Graphics g) { g.drawImage(image,0,0,null); } public static void main(final String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { JFrame f = new JFrame("ImageIOExample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ImageIOExample iioe = new ImageIOExample(args[0]); f.getContentPane().add(iioe,BorderLayout.CENTER); f.pack(); f.setVisible(true); } }); } }