DisplayImage.java

package ac.essex.ooechs.imaging.commons.apps.display; 
 
import ac.essex.ooechs.imaging.commons.util.panels.ImagePanel; 
 
import javax.swing.*; 
import javax.swing.filechooser.FileFilter; 
import java.awt.event.*; 
import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
 
/** 
 * A basic application which allows you to view an image. Also includes a magnifier which allows you to have a closer 
 * look at wherever the mouse is pointing. 
 * 
 * @author Olly Oechsle, University of Essex, Date: 06-Nov-2006 
 * @version 1.0 
 */ 
public class DisplayImage extends JFrame implements ActionListener { 
 
    public static final String APP_NAME = "Image Viewer"; 
 
    JMenuItem file_open; 
    JMenuItem file_exit; 
    JMenuItem view_magnifier; 
 
    JLabel mousePosition; 
 
    MouseImagePanel imagePanel; 
    ZoomPanel zoomPanel; 
 
    Magnifier magnifier; 
 
    //Create a file chooser 
    final JFileChooser fc = new JFileChooser(new File("/home/ooechs/Desktop/sv")); 
 
    public DisplayImage() { 
 
        super(APP_NAME); 
 
        imagePanel = new MouseImagePanel(); 
        imagePanel.setDisplayCentered(true); 
 
        zoomPanel = new ZoomPanel(40, 40); 
        magnifier = new Magnifier(zoomPanel); 
 
        Container c = getContentPane(); 
 
        c.add(imagePanel, BorderLayout.CENTER); 
 
        mousePosition = new JLabel(""); 
 
        c.add(mousePosition, BorderLayout.SOUTH); 
 
        JMenuBar bar = new JMenuBar(); 
        JMenu file = new JMenu("File"); 
        file_open = new JMenuItem("Open"); 
        file_exit = new JMenuItem("Exit"); 
        file_open.addActionListener(this); 
        file_exit.addActionListener(this); 
        file.add(file_open); 
        file.add(file_exit); 
        bar.add(file); 
 
        JMenu view = new JMenu("View"); 
        view_magnifier = new JMenuItem("Magnifier"); 
        view_magnifier.addActionListener(this); 
        view.add(view_magnifier); 
        bar.add(view); 
 
        setJMenuBar(bar); 
 
        setSize(640, 480); 
        setLocation(50, 50); 
        setVisible(true); 
 
    } 
 
    public void actionPerformed(ActionEvent e) { 
 
        if (e.getSource() == view_magnifier) { 
            magnifier.setVisible(!magnifier.isVisible()); 
        } 
 
        if (e.getSource() == file_open) { 
 
            fc.setFileFilter(new FileFilter() { 
                public boolean accept(File f) { 
                    String extension = f.getName().substring(f.getName().lastIndexOf('.') + 1).toLowerCase(); 
                    if (f.isDirectory()) return true; 
                    if (extension.equals("jpg")) return true; 
                    if (extension.equals("png")) return true; 
                    if (extension.equals("gif")) return true; 
                    return false; 
                } 
 
                public String getDescription() { 
                    return "Image Files: jpg, png, gif"; 
                } 
            }); 
 
            int returnVal = fc.showOpenDialog(this); 
            if (returnVal == JFileChooser.APPROVE_OPTION) { 
                File file = fc.getSelectedFile(); 
                try { 
                    // Use the Java ImageIO library to open the file as a buffered image. 
                    BufferedImage img = javax.imageio.ImageIO.read(file); 
                    imagePanel.setImage(img); 
                    zoomPanel.setImage(img); 
                    setTitle(APP_NAME + " - " + file.getName()); 
                } catch (javax.imageio.IIOException e1) { 
                    JOptionPane.showMessageDialog(this, "Could not load image!\n" + e1.getMessage()); 
                } catch (IOException e2) { 
                    JOptionPane.showMessageDialog(this, "Could not load image!\n" + e2.getMessage()); 
                } 
 
            } 
        } 
        if (e.getSource() == file_exit) { 
            System.exit(0); 
        } 
    } 
 
    class MouseImagePanel extends ImagePanel { 
 
        public MouseImagePanel() { 
 
            super(); 
 
            addMouseMotionListener(new MouseMotionAdapter() { 
                public void mouseMoved(MouseEvent e) { 
                    if (image != null) { 
                        int x = e.getX() - getOffsetX(); 
                        int y = e.getY() - getOffsetY(); 
                        mousePosition.setText(x + ", " + y); 
                        zoomPanel.update(x,y); 
                    } else { 
                        mousePosition.setText("No image loaded"); 
                    } 
                } 
            }); 
 
            addMouseListener(new MouseAdapter() { 
                public void mouseClicked(MouseEvent e) { 
                    if (image != null) { 
                        JOptionPane.showMessageDialog(DisplayImage.this, "X: " + e.getX() + ", Y: " + e.getY()); 
                    } 
                } 
            }); 
 
        } 
 
    } 
 
    class Magnifier extends JFrame { 
        public Magnifier(ZoomPanel panel) { 
            super("Magnifier"); 
            getContentPane().add(panel); 
            setSize(250, 250); 
            setLocation(700, 50); 
            setVisible(true); 
            addWindowListener( new WindowAdapter() { 
                public void windowClosing(WindowEvent e) { 
                    setVisible(false); 
                } 
            }); 
        } 
    } 
 
    public static void main(String[] args) { 
        new DisplayImage(); 
    } 
 
}