import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.awt.Color; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Java101ImageUtil{ public static int [][][] getRGBArrayFromFile(){ JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter( "JPG & GIF Images", "jpg", "gif"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(null); if(returnVal == JFileChooser.APPROVE_OPTION) { return getRGBArrayOfSelectedFile(chooser.getSelectedFile()); }else{ return null; } } public static int [][][] getRGBArrayOfSelectedFile(File file){ BufferedImage img = null; try { img = ImageIO.read(file); } catch (IOException e) { System.out.println("Cannot read the selected file."); System.exit(1); } int h = img.getHeight(); int w = img.getWidth(); int[][][] rgb = new int[w][h][3]; for( int i = 0; i < w; i++ ){ for( int j = 0; j < h; j++ ){ Color c = new Color(img.getRGB(i,j)); rgb[i][j][0] = c.getRed(); rgb[i][j][1] = c.getGreen(); rgb[i][j][2] = c.getBlue(); } } return rgb; } public static void showViewer(int [][][] rgb,String title){ Java101Viewer frame = new Java101Viewer(rgb,title); initViewer(frame); } public static void showViewer(int [][][] rgb1,int [][][] rgb2,String title){ Java101Viewer frame = new Java101Viewer(rgb1,rgb2,title); initViewer(frame); } public static void showViewer(int [][][][] rgbs,String title){ Java101Viewer frame = new Java101Viewer(rgbs,title); initViewer(frame); } private static void initViewer(Java101Viewer frame){ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); System.out.println("Close the image viewer to exit the program."); } } class Java101Viewer extends JFrame{ public Java101Viewer(int [][][] rgb,String title){ super(title); setLayout(new GridLayout(1,1)); Java101DrawingPanel drawing = new Java101DrawingPanel(rgb); add(drawing); } public Java101Viewer(int [][][] rgb1,int [][][] rgb2,String title){ super(title); setLayout(new GridLayout(1,2)); Java101DrawingPanel drawingLeft = new Java101DrawingPanel(rgb1); add(drawingLeft); Java101DrawingPanel drawingRight = new Java101DrawingPanel(rgb2); add(drawingRight); } public Java101Viewer(int [][][][] rgbs,String title){ super(title); int num = rgbs.length; Java101DrawingPanel [] drawings = new Java101DrawingPanel[num]; setLayout(new GridLayout(1,num)); for(int i=0;i