import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; /**** * * Class DrawLineDialog is the pop-up dialog that is displayed when the user * selects the 'Polygon' item in the 'Draw' menu. * */ public class DrawPolygonDialog extends JFrame implements ActionListener { /** The model data class, the add method of which we want to call */ WorkSpace workSpace; /** The canvas that needs to be redrawn after the line is created */ DrawingCanvas canvas; /** The vertical box of x,y text field pairs; it's a text field so it can * be accessed by the '+' and '-' button listeners */ Box xyBox = Box.createVerticalBox(); /** The OK button */ JButton okButton = new JButton("OK"); /** X screen coordinate for this display */ final int X_LOCATION = 50; /** X screen coordinate for this display */ final int Y_LOCATION = 75; /** * Construct this by laying out the components and setting up the OK button * listener. */ public DrawPolygonDialog(WorkSpace workSpace, DrawingCanvas canvas) { /* * Call daddy. */ super(); /* * Set the local data field that refers to the model. This is used * subsequently in the OK button listener. */ this.workSpace = workSpace; /* * Set the local data field for the canvas that needs redrawing. */ this.canvas = canvas; /* * Set up the "OK" button, which the user presses to confirm the input * and dismiss the dialog. */ okButton.addActionListener(new OKButtonListener()); /* * Lay out the display using boxes, as done in other 102 examples. */ xyBox.add(createXYBox("1")); xyBox.add(createXYBox("2")); xyBox.add(createXYBox("3")); xyBox.add(createXYBox("4")); xyBox.add(createXYBox("5")); Box vbox = Box.createVerticalBox(); vbox.add(xyBox); vbox.add(createPlusMinusButtons()); okButton.setAlignmentX(Box.CENTER_ALIGNMENT); vbox.add(okButton); /* * Add the window dressing. This includes moving the window down and * to the right, so that it does not appear directly on top of the * menubar. Note that closing this dialog with an OS close gadget does * NOT close the entire application. That only happens for the * outermost frame of the app. */ setTitle("Draw Polygon"); setLocation(X_LOCATION, Y_LOCATION); add(vbox); pack(); } /** * Create a horiontal box with two text fields, labeled x and y. */ Box createXYBox(String i) { Box hbox = Box.createHorizontalBox(); hbox.add(new JLabel("x" + i + ":")); hbox.add(new JTextField(4)); hbox.add(new JLabel(" ")); hbox.add(new JLabel("y" + i + ":")); hbox.add(new JTextField(4)); return hbox; } /** * Create the '+' and '-' buttons that add and remove x,y text box pairs. */ Box createPlusMinusButtons() { Box hbox = Box.createHorizontalBox(); JButton but; hbox.add(but = new JButton("+")); but.addActionListener(createPlusButtonListener()); hbox.add(new JLabel(" ")); hbox.add(but = new JButton("-")); but.addActionListener(createMinusButtonListener()); return hbox; } /** * Create the listener that adds a new x,y text field box. */ private ActionListener createPlusButtonListener() { return new ActionListener() { public void actionPerformed(ActionEvent e) { xyBox.add(createXYBox(new Integer( xyBox.getComponentCount()).toString())); getFrame().pack(); } }; } /** * Create the listener that removes the last x,y text field box. */ private ActionListener createMinusButtonListener() { return new ActionListener() { public void actionPerformed(ActionEvent e) { xyBox.remove(xyBox.getComponentCount() - 1); getFrame().pack(); } }; } /** * All we have to do here is setVisible(true). */ public void actionPerformed(ActionEvent e) { setVisible(true); } /** * Inner class that implements the OK button listener. */ public class OKButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Shape shape; // Shape for setting selection Integer x; // Input from a x text field Integer y; // Input from a y text field int n = xyBox.getComponentCount(); // Number of vertices Point[] vertices = new Point[n]; // Array of vertices boolean error = false; // True if 1 or more input error /* * Get and parse the (would-be) numeric user inputs. */ for (int i = 0; i < n; i++) { /* * Look carefully here. The getComponent(1) is accessing the * x-value text field in the xyBox; getComponent(4) is * accessing the y-value text field. Positions 0, 2, and 3 * contain, respectively, the "x:" JLabel, the " " spacing * JLabel, and the "y:" JLabel. */ x = NumericInputParser.parse( ((JTextField) (((Container) (xyBox.getComponent(i))). getComponent(1))).getText()); y = NumericInputParser.parse( ((JTextField) (((Container) (xyBox.getComponent(i))). getComponent(4))).getText()); /* * If the numbers are legal, add the next vertex point to the * array. */ if ((x != null) && (y != null)) { vertices[i] = new Point(x, y); } else { error = true; } } /* * If there is no input error and there's at least one vertex, draw * the polygon and repaint the canvas */ if ((! error) && (n > 0)) { workSpace.add(shape = new ConvexPolygon(vertices, Color.BLACK, false)); workSpace.setSelection(shape); canvas.repaint(); } /* * Dismiss the dialog. */ setVisible(false); } } JFrame getFrame() { return this; } }