DisplayGrid.java

package ac.essex.ooechs.imaging.commons.apps.display; 
 
import ac.essex.ooechs.imaging.commons.grids.FaceGrid; 
import ac.essex.ooechs.imaging.commons.PixelLoader; 
import ac.essex.ooechs.imaging.commons.util.panels.ScalingGridPanel; 
 
import javax.swing.*; 
import java.io.File; 
import java.awt.event.*; 
 
/** 
 * A GUI frame which can display a series of images in a folder, each overlaid with a particular grid. 
 * I used this to ensure that all the faces in a set of training data had their features in the same locations. 
 * @author Olly Oechsle, University of Essex, Date: 23-Oct-2006 
 * @version 1.0 
 */ 
public class DisplayGrid extends JFrame { 
 
    public static void main(String[] args) throws Exception { 
        File images = new File("/home/ooechs/ecj-training/faces/essex/mit/test/unscaled"); 
        new DisplayGrid(images.listFiles()); 
    } 
 
    ScalingGridPanel panel; 
    File[] images; 
    int cursor = 0; 
 
    public DisplayGrid(File[] images) { 
 
        this.images = images; 
 
        panel = new ScalingGridPanel(new FaceGrid()); 
 
        /** 
         * Cause images to change when the user scrolls the mouse 
         */ 
        panel.addMouseWheelListener(new MouseWheelListener() { 
            public void mouseWheelMoved(MouseWheelEvent e) { 
                if (e.getUnitsToScroll() > 0) next(); 
                else 
                    prev(); 
            } 
        }); 
 
        /** 
         * Add the panel to the layout. 
         */ 
        getContentPane().add(panel); 
 
        /** 
         * Ensure that clicking the cross will stop the application from running 
         */ 
        addWindowListener(new WindowAdapter() { 
            public void windowClosing(WindowEvent e) { 
                System.exit(0); 
            } 
        }); 
 
        /** 
         * Load the first image 
         */ 
        next(); 
 
        /** 
         * Display the application 
         */ 
        setSize(320, 400); 
        setVisible(true); 
 
    } 
 
    public void next() { 
        try { 
            panel.setImage(new PixelLoader(images[cursor])); 
            setTitle(images[cursor].getName()); 
            cursor++; 
            if (cursor >= images.length) cursor = 0; 
        } catch (Exception error) { 
            JOptionPane.showMessageDialog(panel, error.toString()); 
        } 
    } 
 
    public void prev() { 
        try { 
            panel.setImage(new PixelLoader(images[cursor])); 
            setTitle(images[cursor].getName()); 
            cursor--; 
            if (cursor < 0) cursor = images.length - 1; 
        } catch (Exception error) { 
            JOptionPane.showMessageDialog(panel, error.toString()); 
        } 
    } 
}