import javax.swing.*; import java.awt.*; import java.awt.event.*; /**** * * Class MoveDialog is the pop-up dialog that is displayed when the user * selects the 'Move' item in the 'Draw' menu. * */ public class EditMoveDialog extends JFrame implements ActionListener { /** The model data class, the setMoveion method of which we want to * call */ private WorkSpace workSpace; /** The canvas that needs to be redrawn after the selection is made, to * highlight the selected shape. */ DrawingCanvas canvas; /** The text field for x user input */ JTextField xTextField = new JTextField(2); /** The text field for y user input */ JTextField yTextField = 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 current workspace selection. */ Shape selection; /** * The code here is taken from the SetXAndYDialog.java example, q.v. */ public EditMoveDialog(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); vbox.add(hbox); hbox = Box.createHorizontalBox(); hbox.add(new JLabel("y:")); hbox.add(yTextField); 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("Enter a selection number"); setLocation(X_LOCATION, Y_LOCATION);; add(vbox); pack(); } public void actionPerformed(ActionEvent e) { /* * Get the current selection, if any. */ selection = workSpace.getSelection(); /* * Show yourself if there's a selection, otherwise complain and do * nothing. */ if (selection != null) { setVisible(true); } else { new JOptionPane(). showMessageDialog(null, "No shape selected.", "", JOptionPane.ERROR_MESSAGE); } } /** * 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) { /* * Get and parse the (would-be) numeric user inputs. */ Integer x = NumericInputParser.parse(xTextField.getText()); Integer y = NumericInputParser.parse(yTextField.getText()); /* * If the numbers are legal, move the selection and repaint the * canvas. Note that negative values are OK here, as are * postitive values that are off-canvas. */ if ((x != null) && (y != null)) { selection.move(new Point(x, y)); canvas.repaint(); } /* * Adios, mis amigos. */ setVisible(false); } } void p(String s) {System.out.println(s);} }