Yurttas/PL/OOL/Java/F/05/01/05/10/GeometryApplet00.java

  1/**
  2 *  GeometryApplet00: Demonstrating GUI + Event Handling in Swing
  3 */
  4
  5import javax.swing.*;
  6import javax.swing.border.*;
  7
  8import java.awt.*;
  9import java.awt.event.*;
 10
 11import java.applet.*;
 12
 13interface IGeometryConstants {
 14  int SQUARE  = 0;
 15  int CIRCLE  = 1;
 16  int ELLIPSE = 2;
 17  String[] shapeNames = {"Square", "Circle", "Ellipse"};
 18
 19  int SMALL  = 0;
 20  int MEDIUM = 1;
 21  int LARGE  = 2;
 22  String[] sizeNames = {"Small", "Medium", "Large"};
 23}
 24
 25public class GeometryApplet00 extends JApplet
 26                              implements IGeometryConstants {
 27
 28  // Panel for shape
 29  JPanel shapePanel;
 30  ButtonGroup shapeGroup;
 31  JRadioButton squareRB;
 32  JRadioButton circleRB;
 33  JRadioButton ellipseRB;
 34
 35  // Panel for x, y coordinates
 36  JPanel xyPanel;
 37  JLabel xLabel;
 38  JTextField xInput;
 39  JLabel yLabel;
 40  JTextField yInput;
 41
 42  // Panel for size and fill
 43  JPanel sizePanel;
 44  JLabel sizeLabel;
 45  JComboBox sizeChoices;
 46  JCheckBox fillCB;
 47
 48  // Box for shape, coordinates, size and fill
 49  Box leftBox;
 50
 51  // Panel for draw button, drawing region and message display.
 52  JPanel rightPanel;
 53  JButton drawButton;
 54  JScrollPane scrollPane;
 55  DrawRegion drawRegion;
 56  JTextField messageDisplay;
 57
 58  // Top container
 59  Container topContainer;
 60
 61  public void init() {
 62    makeShapePanel();
 63    makeXYPanel();
 64    makeSizePanel();
 65    makeLeftBox();
 66    makeRightPanel();
 67
 68    addListeners();
 69
 70    topContainer = getContentPane();
 71    topContainer.add(leftBox, BorderLayout.WEST);
 72    topContainer.add(rightPanel, BorderLayout.CENTER);
 73
 74    try {
 75      UIManager.setLookAndFeel(
 76        UIManager.getSystemLookAndFeelClassName()
 77//        "com.sun.java.swing.plaf.motif.MotifLookAndFeel"
 78//        "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
 79//        "javax.swing.plaf.mac.MacLookAndFeel"
 80      );
 81    }
 82    catch(Exception e) {
 83      messageDisplay.setText(e.toString());
 84    }
 85    SwingUtilities.updateComponentTreeUI(this);
 86  }
 87
 88  // Panel for shape
 89  void makeShapePanel() {
 90    squareRB = new JRadioButton(shapeNames[SQUARE], true);
 91    circleRB = new JRadioButton(shapeNames[CIRCLE], false);
 92    ellipseRB = new JRadioButton(shapeNames[ELLIPSE], false);
 93
 94    shapeGroup = new ButtonGroup();
 95    shapeGroup.add(squareRB);
 96    shapeGroup.add(circleRB);
 97    shapeGroup.add(ellipseRB);
 98
 99    shapePanel = new JPanel();
100    shapePanel.setLayout(new FlowLayout());
101    shapePanel.add(squareRB);
102    shapePanel.add(circleRB);
103    shapePanel.add(ellipseRB);
104    shapePanel.setBorder(new TitledBorder("Shapes"));
105  }
106
107  // Panel for x,y coordinates
108  void makeXYPanel() {
109    xyPanel = new JPanel();
110
111    xLabel  = new JLabel("X Coordinate:");
112    yLabel  = new JLabel("Y Coordinate:");
113
114    xInput  = new JTextField(5);
115    yInput  = new JTextField(5);
116
117    xyPanel.setLayout(new GridLayout(2,2));
118    xyPanel.add(xLabel);
119    xyPanel.add(xInput);
120    xyPanel.add(yLabel);
121    xyPanel.add(yInput);
122  }
123
124  // Panel for size and fill
125  void makeSizePanel() {
126    sizePanel   = new JPanel();
127
128    sizeLabel   = new JLabel("Size:");
129
130    sizeChoices = new JComboBox(sizeNames);
131    sizeChoices.setSelectedIndex(0);
132
133    fillCB = new JCheckBox("Fill", false);
134
135    sizePanel.setLayout(new FlowLayout());
136    sizePanel.add(sizeLabel);
137    sizePanel.add(sizeChoices);
138    sizePanel.add(fillCB);
139  }
140
141  // Box for shape, coordinates, size and fill
142  void makeLeftBox() {
143    leftBox = new Box(BoxLayout.Y_AXIS);
144
145    leftBox.add(shapePanel);
146    leftBox.add(Box.createGlue());
147    leftBox.add(xyPanel);
148    leftBox.add(Box.createGlue());
149    leftBox.add(sizePanel);
150  }
151
152  // Panel for message display, draw button, and drawing region
153  void makeRightPanel() {
154    rightPanel = new JPanel();
155
156    messageDisplay = new JTextField("MESSAGE DISPLAY");
157    messageDisplay.setEditable(false);
158
159    drawButton = new JButton("Draw");
160
161    drawRegion = new DrawRegion();
162    scrollPane = new JScrollPane(drawRegion);
163
164    rightPanel.setLayout(new BorderLayout());
165    rightPanel.add(drawButton, BorderLayout.NORTH);
166    rightPanel.add(messageDisplay, BorderLayout.SOUTH);
167    rightPanel.add(scrollPane, BorderLayout.CENTER);
168  }
169
170  // Add the listeners.
171  void addListeners() {
172    drawButton.addActionListener(new ActionListener() {
173      public void actionPerformed(ActionEvent evt) {
174        int shape,
175            xCoord,
176            yCoord,
177            width;
178
179        messageDisplay.setText("");
180        // Get the shape
181        if(squareRB.isSelected())
182          shape = SQUARE;
183        else if(circleRB.isSelected())
184          shape = CIRCLE;
185        else if(ellipseRB.isSelected())
186          shape = ELLIPSE;
187        else {
188          messageDisplay.setText("Unknown shape.");
189          return;
190        }
191
192        // Get the coordinates
193        try {
194          xCoord = Integer.parseInt(xInput.getText());
195          yCoord = Integer.parseInt(yInput.getText());
196        } catch (NumberFormatException e) {
197          messageDisplay.setText("Illegal coordinates.");
198          return;
199        }
200
201        // Get the size
202        switch(sizeChoices.getSelectedIndex()) {
203          case SMALL : width = 30;
204                       break;
205          case MEDIUM : width = 60;
206                        break;
207          case LARGE :  width = 120;
208                        break;
209          default : messageDisplay.setText("Unknown size.");
210                    return;
211        }
212
213        messageDisplay.setText("Drawing " + shapeNames[shape]);
214
215        drawRegion.doDraw(shape,
216                          xCoord,
217                          yCoord,
218                          fillCB.isSelected(),
219                          width);
220      }
221    });
222  }
223}
224
225class DrawRegion extends JPanel
226                 implements IGeometryConstants {
227  // Values needed for drawing the shape
228  private int shape;
229  private int xCoord;
230  private int yCoord;
231  private boolean fillFlag;
232  private int width;
233
234  // Default constructor
235  public DrawRegion() {
236    setSize(300,300);
237    setBackground(Color.white);
238  }
239
240  // Overridden to always keep the same size
241  public Dimension getPreferredSize() {
242    return getSize();
243  }
244
245  // Set the values and repaint the drawing region.
246  public void doDraw(int shape,
247             int xCoord, int yCoord,
248             boolean fillFlag, int width) {
249
250    this.shape  = shape;
251    this.xCoord   = xCoord;
252    this.yCoord   = yCoord;
253    this.fillFlag = fillFlag;
254    this.width  = width;
255
256    repaint();
257  }
258
259  // Do the drawing of the shape
260  public void paintComponent (Graphics g) {
261    super.paintComponent(g);
262    switch (shape) {
263      case SQUARE:
264        if (fillFlag) g.fillRect(xCoord, yCoord, width, width);
265        else g.drawRect(xCoord, yCoord, width, width);
266        break;
267      case CIRCLE:
268        if (fillFlag) g.fillOval(xCoord, yCoord, width, width);
269        else g.drawOval(xCoord, yCoord, width, width);
270        break;
271      case ELLIPSE:
272        if (fillFlag) g.fillOval(xCoord, yCoord, width, width/2);
273        else g.drawOval(xCoord, yCoord, width, width/2);
274        break;
275    }
276  }
277
278}