import javax.swing.*; import java.awt.*; import java.awt.event.*; /**** * * Class DrawLineDialog is the pop-up dialog that is displayed when the user * selects the 'Line' item in the 'Draw' menu. * */ public class DrawLineDialog extends JFrame implements ActionListener { /** The model data class, the add method of which we want to call */ private WorkSpace workSpace; /** The canvas that needs to be redrawn after the line is created */ DrawingCanvas canvas; /** The text field for x1 user input */ JTextField x1TextField = new JTextField(4); /** The text field for y1 user input */ JTextField y1TextField = new JTextField(4); /** The text field for x2 user input */ JTextField x2TextField = new JTextField(4); /** The text field for y2 user input */ JTextField y2TextField = new JTextField(4); /** The OK button */ JButton okButton = new JButton("OK"); /** X screen coordinate for this display */ final int X_LOCATION = 50; /** Y screen coordinate for this display */ final int Y_LOCATION = 75; /** * The code here is taken from the SetXAndYDialog.java example, q.v. */ public DrawLineDialog(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()); /* * Layout the display using boxes, as done in other 102 examples. */ Box vbox = Box.createVerticalBox(); Box hbox = Box.createHorizontalBox(); hbox.add(new JLabel("x1:")); hbox.add(x1TextField); hbox.add(new JLabel(" ")); hbox.add(new JLabel("y1:")); hbox.add(y1TextField); vbox.add(hbox); hbox = Box.createHorizontalBox(); hbox.add(new JLabel("x2:")); hbox.add(x2TextField); hbox.add(new JLabel(" ")); hbox.add(new JLabel("y2:")); hbox.add(y2TextField); vbox.add(hbox); 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 Line"); setLocation(X_LOCATION, Y_LOCATION);; add(vbox); 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 { /** * Get the x,y coordinates entered by the user in the text fields. If * they parse as integers, call workSpace.move(x,y). */ public void actionPerformed(ActionEvent e) { Shape shape; // Shape for setting selection /* * Get and parse the (would-be) numeric user inputs. */ Integer x1 = NumericInputParser.parse(x1TextField.getText()); Integer y1 = NumericInputParser.parse(y1TextField.getText()); Integer x2 = NumericInputParser.parse(x2TextField.getText()); Integer y2 = NumericInputParser.parse(y2TextField.getText()); /* * If the numbers are legal, draw the line and repaint the canvas. * Note that negative values are OK here, as are postitive values * that are off-canvas. */ if ((x1 != null) && (y1 != null) && (x2 != null) && (y2 != null)) { /* * Add the new line to the workspace. This is the biznis end * of things here. */ workSpace.add(shape = new Line( new Point(x1, y1), new Point(x2, y2), Color.BLACK, false)); /* * Make the new line the current selection on the canvas. */ workSpace.setSelection(shape); /* * Repaint the canvas so that the new line shows up. */ canvas.repaint(); } /* * Bye. */ setVisible(false); } } void p(String s) {System.out.println(s);} }