import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * GUI module for simple display system with menu control. * * @author Bob Futrelle * @version 0.1, 19 January 2003 * * Separates the GUI from the rest of the application. * */ class RectOvalGUI { int width, height; Dimension dim; RectOvalDrawer drawer; JFrame frame; ShowRectOval show; JPanel panel; RectOvalGUI(int w, int h) { width = w; height = h; dim = new Dimension(width, height); } /** * Sets up the GUI window and menus for response. */ void setup(JFrame frame, RectOvalDrawer drawer) { this.drawer = drawer; this.frame = frame; frame.setTitle("Show Rectangle Oval demo"); frame.setJMenuBar(buildMenus()); drawer.setPreferredSize(dim); frame.getContentPane().add(drawer, "South"); finish(); } JMenuBar buildMenus() { JMenuBar menuBar = createMenuBar(); JMenu menu = createMenu(); class OvalAction extends AbstractAction { OvalAction(String itemText) { super(itemText); } // set the paramters and call repaint public void actionPerformed(ActionEvent e) { drawer.drawOval(); } } // OvalAction class RectAction extends AbstractAction { RectAction(String itemText) { super(itemText); } // set the paramters and call repaint public void actionPerformed(ActionEvent e) { drawer.drawRect(); } } // OvalAction class QuitAction extends AbstractAction { QuitAction(String itemText) { super(itemText); } // set the paramters and call repaint public void actionPerformed(ActionEvent e) { System.exit(0); } } // OvalAction JMenuItem menuItem = new JMenuItem(new OvalAction("Draw Oval")); menu.add(menuItem); menuItem = new JMenuItem(new RectAction("Draw Rect")); menu.add(menuItem); menuItem = new JMenuItem(new QuitAction("Quit")); menu.add(menuItem); menuBar.add(menu); return menuBar; } JMenuBar createMenuBar() { return new JMenuBar(); } JMenu createMenu() { return new JMenu("Draw-or-Quit"); } void finish(){ frame.pack(); frame.setVisible(true); } } // class RectOvalGUI