import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; /**** * * Class DrawingFrame lays out a simple display for a record list viewer, as * specified in the * writeup for CSC 102 Lab 7. * */ public class DrawingFrame extends JFrame { DrawingCanvas canvas; DrawingToolBar toolBar; JMenuBar menuBar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenu editMenu = new JMenu("Edit"); JMenu drawMenu = new JMenu("Draw"); JMenu fillMenu = new JMenu("Fill"); JMenu viewMenu = new JMenu("View"); FileSaveDialog fileSaveDialog; FileOpenDialog fileOpenDialog; EditSelectDialog editSelectDialog; EditMoveDialog editMoveDialog; Box tools; int n; Box hbox = Box.createHorizontalBox(); Box vbox = Box.createVerticalBox(); /** The model class from which the displayed record is fetched */ private WorkSpace workSpace; /** * Construct this frame by constructing each component and adding them to * the panel. The general structure of this constructor follows the * example Chapter 9 and 10 examples from Horstmann's Big Java. */ public DrawingFrame(WorkSpace workSpace) { this.workSpace = workSpace; createCanvas(); createTools(); createMenuBar(); JPanel c = new JPanel(); c.add(canvas); tools.add(c); vbox.add(Box.createVerticalStrut(4)); JPanel p = new JPanel(); p.add(hbox); p.setBackground(Color.WHITE); vbox.add(hbox); vbox.add(tools); hbox.setAlignmentX(LEFT_ALIGNMENT); tools.setAlignmentX(LEFT_ALIGNMENT); add(vbox); pack(); setTitle("Drawing"); } /** * Create the menu bar, its menus, and items. */ private void createMenuBar() { Box hbox2 = Box.createHorizontalBox(); Box hbox3 = Box.createHorizontalBox(); JPanel panel = new JPanel(); hbox2.add(Box.createHorizontalStrut(60)); panel.add(hbox2); panel.add(hbox3); menuBar.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); createFileMenu(); createEditMenu(); createDrawMenu(); createFillMenu(); createViewMenu(); hbox3.add(menuBar); hbox3.add(Box.createHorizontalStrut(375)); hbox.add(panel); } private void createFileMenu() { menuBar.add(fileMenu); fileMenu.add(new JMenuItem("Save ...")).addActionListener( new FileSaveDialog(workSpace, canvas)); fileMenu.add(new JMenuItem("Open ...")).addActionListener( new FileOpenDialog(workSpace, canvas)); fileMenu.add(new JMenuItem("Exit")).addActionListener( createFileExitListener()); } private ActionListener createFileExitListener() { return new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }; } private void createEditMenu() { menuBar.add(editMenu); editMenu.add(new JMenuItem("Select ...")).addActionListener( new EditSelectDialog(workSpace, canvas)); editMenu.add(new JMenuItem("Move ...")).addActionListener( new EditMoveDialog(workSpace, canvas)); editMenu.add(new JMenuItem("Delete")).addActionListener( new DeleteButtonListener()); editMenu.add(new JMenuItem("Delete All")).addActionListener( new DeleteAllButtonListener()); editMenu.add(new JSeparator()); editMenu.add(new JMenuItem("Move Forward")).addActionListener( createEditMoveForwardListener()); editMenu.add(new JMenuItem("Move Backward")).addActionListener( createEditMoveBackwardListener()); editMenu.add(new JMenuItem("Move to Front")).addActionListener( createEditMoveToFrontListener()); editMenu.add(new JMenuItem("Move to Back")).addActionListener( createEditMoveToBackListener()); } private ActionListener createEditMoveForwardListener() { return new ActionListener() { public void actionPerformed(ActionEvent e) { workSpace.moveForward(); canvas.repaint(); } }; } private ActionListener createEditMoveBackwardListener() { return new ActionListener() { public void actionPerformed(ActionEvent e) { workSpace.moveBackward(); canvas.repaint(); } }; } private ActionListener createEditMoveToFrontListener() { return new ActionListener() { public void actionPerformed(ActionEvent e) { workSpace.moveToFront(); canvas.repaint(); } }; } private ActionListener createEditMoveToBackListener() { return new ActionListener() { public void actionPerformed(ActionEvent e) { workSpace.moveToBack(); canvas.repaint(); } }; } private void createDrawMenu() { menuBar.add(drawMenu); drawMenu.add(new JMenuItem("Line ...")).addActionListener( new DrawLineDialog(workSpace, canvas)); drawMenu.add(new JMenuItem("Rect ...")).addActionListener( new DrawRectDialog(workSpace, canvas)); drawMenu.add(new JMenuItem("Ellipse ...")).addActionListener( new DrawEllipseDialog(workSpace, canvas)); drawMenu.add(new JMenuItem("Polygon ...")).addActionListener( new DrawPolygonDialog(workSpace, canvas)); drawMenu.add(new JMenuItem("Text ...")).addActionListener( new DrawTextDialog(workSpace, canvas)); } private void createFillMenu() { menuBar.add(fillMenu); fillMenu.add(new JMenuItem("On/Off")).addActionListener( new FillOnOffButtonListener()); fillMenu.add(new JMenuItem("Color ...")).addActionListener( new ColorButtonListener()); } private void createViewMenu() { menuBar.add(viewMenu); viewMenu.add(new JMenuItem("Area")).addActionListener( new AreaDialog(workSpace)); viewMenu.add(new JMenuItem("Area of All")).addActionListener( new AreaAllDialog(workSpace)); viewMenu.add(new JMenuItem("Number of Shapes")).addActionListener( new NumShapesDialog(workSpace)); } private void createTools() { tools = Box.createHorizontalBox(); toolBar = new DrawingToolBar(canvas); tools.add(toolBar); toolBar.setAlignmentX(CENTER_ALIGNMENT); toolBar.setAlignmentY(CENTER_ALIGNMENT); toolBar.setMaximumSize(new Dimension(toolBar.getWidth(),10000)); } /** */ private void createCanvas() { canvas = new DrawingCanvas(workSpace); canvas.setPreferredSize(new Dimension(600,600)); canvas.setBackground(Color.WHITE); canvas.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1)); canvas.setLayout(null); } /** * Implement the mouse listener for the "Fill On/Off" menu item. The * actionPerformed method toggles the fill value for the currently selected * shape, if any, and repaints the canvas. If no shaape is selected, * complain, and do nothing. */ private class FillOnOffButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Shape shape = workSpace.getSelection(); if (shape == null) { new JOptionPane(). showMessageDialog(null, "No shape selected.", "", JOptionPane.ERROR_MESSAGE); return; } if (shape.getFilled()) { shape.setFilled(false); } else { shape.setFilled(true); } canvas.repaint(); } } /** * Implement the mouse listener for the "Del Rect" button. The * actionPerformed method removes a model rectangle from the shapeList and * repaints the canvas. */ private class ColorButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Shape shape = workSpace.getSelection(); if (shape == null) { new JOptionPane(). showMessageDialog(null, "No shape selected.", "", JOptionPane.ERROR_MESSAGE); return; } shape.setColor(JColorChooser.showDialog(getFrame(), "Select Shape Color", Color.BLACK)); canvas.repaint(); } } /** * Implement the mouse listener for the "Delele" menu item. The * actionPerformed method removes the current selection, if any. If no * shaape is selected, complain, and do nothing. */ private class DeleteButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { /* * Remove the current selection, if any. As noted in an internal * comment in EditRemoveDialog.OKButtonListerner.actionPerformed, * we could put up a "Hey dufus, it's null." dialog, but we don't. */ workSpace.remove(); canvas.repaint(); } } /** * Implement the mouse listener for the "Delele All" menu item. The * actionPerformed method removes all workspace selections, if any. */ private class DeleteAllButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { workSpace.removeAll(); canvas.repaint(); } } private Box.Filler createRigidSpace() { Dimension minSize = new Dimension(0,0); Dimension prefSize = new Dimension(0,0); Dimension maxSize = new Dimension(0,0); return new Box.Filler(minSize, prefSize, maxSize); } public DrawingFrame getFrame() { return this; } public DrawingToolBar getToolBar() { return toolBar; } }