import javax.swing.*; import java.awt.*; import java.awt.image.*; import java.net.*; public class Imager { // window(): displays and returns a window with image icon public static JFrame window( ImageIcon icon ) { int w = icon.getImage().getWidth( null ) + 10; int h = icon.getImage().getHeight( null ) + 35; JFrame frame = Window.make( "Photo", w, h, Color.black, true, false ); Container pane = frame.getContentPane(); JLabel picture = new JLabel( icon ); pane.add( picture ); frame.setVisible(true); return frame; } // window(): displays and returns a window with image and title public static JFrame window( String title, String iconname ) { ImageIcon icon = Imager.getImage( iconname ); int w = icon.getImage().getWidth( null ) + 10; int h = icon.getImage().getHeight( null ) + 35; JFrame frame = Window.make( title, w, h, Color.black, true, false ); Container pane = frame.getContentPane(); JLabel picture = new JLabel( icon ); pane.add( picture ); frame.setVisible(true); return frame; } // getPixels(): gets grid of pixels making up the image public static int[][] getPixels( ImageIcon icon ) { Image image = icon.getImage(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); BufferedImage buffer = null; try { int transparency = Transparency.OPAQUE; GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); buffer = gc.createCompatibleImage( image.getWidth(null), image.getHeight(null), transparency); } catch (HeadlessException e) { } if ( buffer == null ) { buffer = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB ); } Graphics g = buffer.createGraphics(); g.drawImage(image, 0, 0, null); g.dispose(); int width = buffer.getWidth(); int height = buffer.getHeight(); int[][] grid = new int[ width ][ height ]; for ( int x = 0; x < width; ++x ) { for ( int y = 0; y < height; ++y ) { grid[ x ][ y ] = buffer.getRGB( x, y ); } } return grid; } // setPixels(): resets the pixels of the image according to the grid public static void setPixels( ImageIcon icon, int[][] grid ) { int width = grid.length; int height = grid[0].length; BufferedImage buffer = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB ); for ( int x = 0; x < width; ++x ) { for ( int y = 0; y < height; ++y ) { int pixel = grid[ x ][ y ]; buffer.setRGB( x, y, pixel ); } } icon.setImage( buffer ); } // popup(): displays a popup for the image public static void popup( ImageIcon icon ) { Imager.popup( icon, "" ); } // popup(): displays a popup for the image public static void popup( ImageIcon icon, String title ) { JOptionPane.showMessageDialog( null, "", title, JOptionPane.INFORMATION_MESSAGE, icon ); } // getImage(): returns an image of someone from class folder public static ImageIcon getImage( String s ) { try { String BASE = "http://www.cs.virginia.edu/cs101x/people/" ; String name = BASE + s + "/picture.jpg"; URL u = new URL( name ); return new ImageIcon( u ); } catch ( Exception e ) { return null; } } }