import javax.swing.*; import java.awt.*; import java.awt.event.*; /**** * * Class DrawRectDialog is the pop-up dialog that is displayed when the user * selects the 'Rect' item in the 'Draw' menu. * */ public class DrawRectDialog 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 rectangle is created */ DrawingCanvas canvas; /** The text field for x1 user input */ JTextField xTextField = new JTextField(2); /** The text field for y1 user input */ JTextField yTextField = new JTextField(2); /** The text field for x2 user input */ JTextField widthTextField = new JTextField(2); /** The text field for y2 user input */ JTextField heightTextField = new JTextField(2); /** 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 DrawRectDialog(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("x:")); hbox.add(xTextField); hbox.add(new JLabel(" ")); hbox.add(new JLabel("y:")); hbox.add(yTextField); vbox.add(hbox); hbox = Box.createHorizontalBox(); hbox.add(new JLabel("width:")); hbox.add(widthTextField); hbox.add(new JLabel(" ")); hbox.add(new JLabel("height:")); hbox.add(heightTextField); 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 Rectangle"); 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 x = NumericInputParser.parse(xTextField.getText()); Integer y = NumericInputParser.parse(yTextField.getText()); Integer width = NumericInputParser.parse(widthTextField.getText()); Integer height = NumericInputParser.parse(heightTextField.getText()); /* * If the numbers are legal, draw the rectangle and repaint the * canvas. Note that negative values are OK here, as are postitive * values that are off-canvas. */ if ((x != null) && (y != null) && (width != null) && (height != null)) { workSpace.add(shape = new Rectangle( width, height, new Point(x, y), Color.BLACK, false)); workSpace.setSelection(shape); canvas.repaint(); } /* * Bye. */ setVisible(false); } } void p(String s) {System.out.println(s);} }