//import java.awt.*; import javax.swing.*; import java.awt.FlowLayout; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.Dimension; /** *
* the GUI for the Pixmap application * separation of GUI from application makes it easier to * modify either one although these classes are still tightly * coupled ** * @see PixApp * * @author Owen Astrachan */ public class PixGui extends JFrame { /** * construct a GUI * @param app the application bound to this GUI */ public PixGui(PixController control) { myControl = control; setTitle("Pixmap Viewer"); JPanel contentPane = new JPanel(); contentPane.setLayout(new BorderLayout()); // button bar at top myButtonPanel = new JPanel(); myButtonPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); contentPane.add(myButtonPanel, BorderLayout.NORTH); // scrollable panel for images in center myImagePanel = new JPanel(new GridLayout(0,3)); myScroller = new JScrollPane(myImagePanel); myScroller.setPreferredSize(new Dimension(OUR_WIDTH, OUR_HEIGHT)); contentPane.add(myScroller, BorderLayout.CENTER); // create all commands Command read = new ReadCommand(myControl); Command vert = new SimpCommand(myControl,new VertReflect()); Command horiz = new SimpCommand(myControl,new HorizReflect()); Command expand = new SimpCommand(myControl, new ExpandOperator(this)); // use anonymous inner class to show how this works // see class Invert below Command invert = new SimpCommand(myControl, new PixmapOperator() { public void execute() { myPixmap.invert(); } }); Command quit = new QuitCommand(myControl); // create a menu item for each command // (should create a helper function to do this, associating // each menu item with a command) JMenuItem openMI = new JMenuItem("Open"); openMI.addActionListener(read); JMenuItem quitMI = new JMenuItem("Quit"); quitMI.addActionListener(quit); JMenuItem vertMI = new JMenuItem("Vert"); vertMI.addActionListener(vert); JMenuItem horizMI = new JMenuItem("Horiz"); horizMI.addActionListener(horiz); JMenuItem invertMI = new JMenuItem("Invert"); invertMI.addActionListener(invert); JMenuItem expandMI = new JMenuItem("Expand"); expandMI.addActionListener(expand); // set up a menu bar with "file" and "pix" menus myMenuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); myMenuBar.add(fileMenu); JMenu pixMenu = new JMenu("Pix", true); // tear-off myMenuBar.add(pixMenu); setJMenuBar(myMenuBar); // add the menu items to the menus fileMenu.add(openMI); fileMenu.add(quitMI); pixMenu.add(vertMI); pixMenu.add(horizMI); pixMenu.add(invertMI); pixMenu.add(expandMI); // now the buttons JButton openJButton = new JButton("Open"); openJButton.addActionListener(read); JButton quitJButton = new JButton("Quit"); quitJButton.addActionListener(quit); JButton vertJButton = new JButton("Vert"); vertJButton.addActionListener(vert); JButton horizJButton = new JButton("Horiz"); horizJButton.addActionListener(horiz); // add buttons to button panel myButtonPanel.add(openJButton); myButtonPanel.add(vertJButton); myButtonPanel.add(horizJButton); myButtonPanel.add(quitJButton); // make the content part of the frame setContentPane(contentPane); // make the window resize to fit all elements 'add'ed to the frame pack(); // display the window frame // show(); setVisible(true); } /** * adds an image (via its thumbnail) to the GUI * @param icon the thumbnail version of the added Pixmap * */ public void addImage(PixIcon icon) { myImagePanel.add(icon); ((JComponent)getContentPane()).revalidate(); ((JComponent)getContentPane()).repaint(); } private PixController myControl; // associated controller private JPanel myButtonPanel; // panel for buttons private JMenuBar myMenuBar; // where the menus are private JPanel myImagePanel; // thumbnails displayed here private JScrollPane myScroller; // scrolls the thumbnails private static final int OUR_WIDTH = 300; private static final int OUR_HEIGHT = 200; } class VertReflect extends PixmapOperator { public VertReflect() { } public void execute() { myPixmap.vertReflect(); } } class HorizReflect extends PixmapOperator { public HorizReflect() { } public void execute() { myPixmap.horizReflect(); } } class Invert extends PixmapOperator { public Invert() { } public void execute() { myPixmap.invert(); } }