FileDocCategorySizeDatePackage
ImagePreview.javaAPI DocExample2333Mon Jan 09 11:01:58 GMT 2006None

ImagePreview

public class ImagePreview extends JPanel implements PropertyChangeListener

Fields Summary
private JFileChooser
jfc
private Image
img
Constructors Summary
public ImagePreview(JFileChooser jfc)

        this.jfc = jfc;
        Dimension sz = new Dimension(200,200);
        setPreferredSize(sz);
    
Methods Summary
public static voidmain(java.lang.String[] args)

        JFileChooser jfc = new JFileChooser();
        ImagePreview preview = new ImagePreview(jfc);
        jfc.addPropertyChangeListener(preview);
        jfc.setAccessory(preview);
        jfc.showOpenDialog(null);
    
public voidpaintComponent(java.awt.Graphics g)

        // fill the background
        g.setColor(Color.gray);
        g.fillRect(0,0,getWidth(),getHeight());
        
        if(img != null) {
            // calculate the scaling factor
            int w = img.getWidth(null);
            int h = img.getHeight(null);
            int side = Math.max(w,h);
            double scale = 200.0/(double)side;
            w = (int)(scale * (double)w);
            h = (int)(scale * (double)h);
            
            // draw the image
            g.drawImage(img,0,0,w,h,null);
            
            // draw the image dimensions
            String dim = w + " x " + h;
            g.setColor(Color.black);
            g.drawString(dim,31,196);
            g.setColor(Color.white);
            g.drawString(dim,30,195);
            
        } else {
            
            // print a message
            g.setColor(Color.black);
            g.drawString("Not an image",30,100);
        }
    
public voidpropertyChange(java.beans.PropertyChangeEvent evt)

        try {
            System.out.println("updating");
            File file = jfc.getSelectedFile();
            updateImage(file);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
    
public voidupdateImage(java.io.File file)

        if(file == null) {
            return;
        }
        
        img = ImageIO.read(file);
        repaint();