import javax.swing.*; import java.awt.*; import java.awt.event.*; /**** * * Class DrawTextDialog is the pop-up dialog that is displayed when the user * selects the 'Text' item in the 'Draw' menu. * */ public class DrawTextDialog 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 ellipse 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 the text user input */ JTextField textField = new JTextField(8); /** 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 DrawTextDialog(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(); vbox.add(textField); 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 Text"); 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, and the text * input whatever it is. */ Integer x = NumericInputParser.parse(xTextField.getText()); Integer y = NumericInputParser.parse(yTextField.getText()); String text = textField.getText(); /* * If the numbers are legal, draw the text and repaint the canvas. * Note that negative values are OK here, as are postitive values * that are off-canvas. */ if ((x != null) && (y != null)) { int x_fudge = 1; int y_fudge = -4; int width = (int) canvas.getFont().getStringBounds(text, canvas.getGraphics2D().getFontRenderContext()).getWidth(); int height = (int) canvas.getFont().getStringBounds(text, canvas.getGraphics2D().getFontRenderContext()).getHeight(); workSpace.add(shape = new Text( text, width, height, x + x_fudge, canvas.getHeight() - y + y_fudge - height, Color.BLACK, false)); workSpace.setSelection(shape); canvas.repaint(); } /* * Bye. */ setVisible(false); } } void p(String s) {System.out.println(s);} }