/* * Created on Jul 20, 2004 */ package imageprocessing; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JPanel; /** A panel with buttons to control an image viewer frame. Code is provided only for reference. You don't need to modify it, and won't be able to hand in a modified version. You don't need to understand how it works, either -- but you might learn something by studying it. * @author dickey */ public class ImageButtonPanel extends JPanel { private final IImageProcessor iprocessor; private final IImageFilter[] filters; private final AbstractImagePanel leftPanel, rightPanel; private final Color buttonColor = new Color(255, 255, 200); private final Color backgroundColor = new Color(120, 0, 0); /** Create a panel with buttons to control image operations. Typically, * pressing a button causes an operation to occur, followed by the result * of the operation being displayed. Some operations (LOAD, SAVE, SWAP) * are built-in, and others are supplied as image filters. * @param iprocessor a non-null image processor object. * @param filters an array of non-null filters, which may be zero-length but should * not be null. * @param leftPanel a non-null image panel * @param rightPanel a non-null image panel */ public ImageButtonPanel(IImageProcessor iprocessor, IImageFilter[] filters, AbstractImagePanel leftPanel, AbstractImagePanel rightPanel) { if (iprocessor == null) { throw new IllegalArgumentException("Image processor for button panel" + " must not be null"); } this.iprocessor = iprocessor; if (filters == null) { throw new IllegalArgumentException("filter array for button panel" + " must not be null"); } this.filters = new IImageFilter[filters.length]; for (int f = 0; f < filters.length; f++) { if (filters[f] == null) { throw new IllegalArgumentException("filter " + f + " for button panel" + " must not be null"); } this.filters[f] = filters[f]; } if (leftPanel == null) { throw new IllegalArgumentException("left image panel for button panel" + " must not be null"); } this.leftPanel = leftPanel; // Arguments have been saved. if (rightPanel == null) { throw new IllegalArgumentException("right image panel for button panel" + " must not be null"); } this.rightPanel = rightPanel; //arguments have been saved this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); //this.setBackground(backgroundColor); // Start building buttons. ; this.add(new ReadFileButton()); this.add(new SaveFileButton()); //add a button for each filter for (int b = 0; b < this.filters.length; b++) { JButton aButton = new FilterButton(this.filters[b]); this.add(aButton); } this.add(new SwapPanelsButton()); } /** A button, which when pressed, causes a file to be chosen, loaded, * and shown. * @author dickey */ private class ReadFileButton extends JButton { ReadFileButton() { super("Read File"); setToolTipText("Prompt for a file and attempt to load and display it." + " Only .pgm and .ppm file formats are supported."); addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser("choose an image file"); int result = chooser.showOpenDialog(ImageButtonPanel.this); if (result == JFileChooser.APPROVE_OPTION) { //a file was selected File f = chooser.getSelectedFile(); String fname = f.getAbsolutePath(); IImage image = ImageButtonPanel.this.iprocessor.produceImage(fname); if (image != null) { ImageButtonPanel.this.leftPanel.setImage(image); ImageButtonPanel.this.leftPanel.repaint(); } } } }); } } /** A button, which when pressed, causes a file to be chosen, and the image * in the right panel to be saved to the file. The user must give the * full file name, including extension. * @author dickey */ private class SaveFileButton extends JButton { SaveFileButton() { super("Save File"); setToolTipText("Prompt for a file, and save the right-hand image" + " in that file. Be sure to enter the full filename," + " including the extension, since no extension is added" + " automatically."); addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { IImage imageToSave = ImageButtonPanel.this.rightPanel.getImage(); if (imageToSave != null) { JFileChooser chooser = new JFileChooser("choose a file to write in"); int result = chooser.showSaveDialog(ImageButtonPanel.this); if (result == JFileChooser.APPROVE_OPTION) { //a file was selected File f = chooser.getSelectedFile(); String fname = f.getAbsolutePath(); boolean saved = ImageButtonPanel.this.iprocessor.writeImage( imageToSave, fname); if (saved) { System.out.println("Image saved to " + fname); } else { System.out.println("Image not successfully saved to " + fname); } } } } }); } } /** A button, which when pressed, causes the images in the two panels * to be swapped. * @author dickey */ private class SwapPanelsButton extends JButton { SwapPanelsButton() { super("Swap"); setToolTipText("Swap the images in the left and right panels."); addActionListener(new ActionListener () { public void actionPerformed(ActionEvent e) { IImage oldleft = ImageButtonPanel.this.leftPanel.getImage(); IImage oldright = ImageButtonPanel.this.rightPanel.getImage(); ImageButtonPanel.this.leftPanel.setImage(oldright); ImageButtonPanel.this.leftPanel.repaint(); ImageButtonPanel.this.rightPanel.setImage(oldleft); ImageButtonPanel.this.rightPanel.repaint(); } }); } } /** A button which, when pressed, causes the image in the left-hand window * to be given to an image filter, after which the resulting image is * displayed in the right-hand window. * @author dickey */ private class FilterButton extends JButton { private final IImageFilter filter; FilterButton (IImageFilter filter) { assert filter != null; this.filter = filter; this.setText(filter.shortName()); this.setToolTipText(filter.description()); this.setBackground(buttonColor); this.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { IImage inputImage = ImageButtonPanel.this.leftPanel.getImage(); if (inputImage != null) { IImage outputImage = FilterButton.this.filter.filter(inputImage); if (outputImage != null) { ImageButtonPanel.this.rightPanel.setImage(outputImage); ImageButtonPanel.this.rightPanel.repaint(); } } } }); } } }