FileDocCategorySizeDatePackage
ImagePreview.javaAPI DocExample1668Tue Dec 12 18:59:20 GMT 2000None

ImagePreview

public class ImagePreview extends JComponent implements PropertyChangeListener

Fields Summary
ImageIcon
thumbnail
File
file
Constructors Summary
public ImagePreview(JFileChooser fc)

                             
       
        setPreferredSize(new Dimension(100, 50));
        fc.addPropertyChangeListener(this);
    
Methods Summary
public voidloadImage()

        if (file == null) {
            return;
        }
 
        ImageIcon tmpIcon = new ImageIcon(file.getPath());
        if (tmpIcon.getIconWidth() > 90) {
            thumbnail = new ImageIcon(tmpIcon.getImage().
                                 getScaledInstance(90, -1,
                                                   Image.SCALE_DEFAULT));
        } else {
            thumbnail = tmpIcon;
        }
    
public voidpaintComponent(java.awt.Graphics g)

        if (thumbnail == null) {
            loadImage();
        }
        if (thumbnail != null) {
            int x = getWidth()/2 - thumbnail.getIconWidth()/2;
            int y = getHeight()/2 - thumbnail.getIconHeight()/2;

            if (y < 0) {
                y = 0;
            }

            if (x < 5) {
                x = 5;
            }
            thumbnail.paintIcon(this, g, x, y);
        }
    
public voidpropertyChange(java.beans.PropertyChangeEvent e)

        String prop = e.getPropertyName();
        if (prop.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
            file = (File) e.getNewValue();
            if (isShowing()) {
                loadImage();
                repaint();
            }
        }