/** An Applet prgram that draws a rectangle or circle depending on 
 user's selection from a JComboBox; the user is further prompt for
 the length and width, or the radius, respectively, after a shape
 is selected.  The applet is run with the following html file:
    <html>
      <applet code = "DrawShapes.class" width = "300" height = "300">
      </applet>
    </html>
  This html file (say, named "DrawShapes.html") can be run as follows:
    appletviewer DrawShapes.html
  or it can be opened from a web browser such as IE or Netscape that
  supports the Swing class
  (Nov. 29, 2001)
*/

import java.awt.Graphics;  // Java core package
import javax.swing.*;  // Java extension packages
import java.awt.*;
import java.awt.event.*;
import java.util.*;  // for StringTokenizer

public class DrawShapes extends JApplet {
  int selection = -1;  // user's selection for drawing 
  JComboBox selectShape;  // for user selections
  int length, width,  // rectangle's dimension
      radius;         // circle's radius

  /** initialize applet by initilaizing the GUI 
    and registering event handler to a JComboBox */
  public void init() {
    // create a JComboBox component
     String[] selections = new String[] {"Rectangle", "Circle"};
    selectShape = new JComboBox(selections);

    // set the initial selected index of the JComboBox
    selectShape.setSelectedIndex(0);

    /* create an ActionListener object and register 
       it to JComboBox */
    ActionHandler handler = new ActionHandler();
    selectShape.addActionListener(handler);
         
    // add JComboBox to applet    
    Container p = getContentPane();
    p.setLayout(new FlowLayout());
    p.add(selectShape);
  }

  /** draw shapes on applet's background */
  public void paint(Graphics g) {
    super.paint(g);  // call inherited paint() method

    // paint the drawing if legitimate selection
    if (selection == 0)  // draw rectangle
      g.drawRect(50, 100, length, width); // 60 x 60 square
    else if (selection == 1)  // draw circle
      g.drawOval(50, 100, radius*2, radius*2); 

  }  // end of paint()

  /** an inner class for the ActionListener */
  private class ActionHandler implements ActionListener{

    /** gets called when any item of the JComboBox gets selected */
    public void actionPerformed(ActionEvent e) {
      selection = selectShape.getSelectedIndex();

      try { // prepare for exceptions
        if (selection == 0) { // Rectangle selected
          String input = JOptionPane.showInputDialog(
                        "Enter two integers separated by one or more\n"
                        + "spaces for the length and width of a rectangle\n");

          StringTokenizer tokens = new StringTokenizer(input);
          length = Integer.parseInt(tokens.nextToken()); 
          width = Integer.parseInt(tokens.nextToken()); 
        }
        else { // Circle selected
          String input = JOptionPane.showInputDialog(
                        "Enter an integer for the radius\n"
                        + "of a circle\n");
          radius = Integer.parseInt(input); 
        }
      } catch (Exception exp) {
          selection = -1;  // in case of exception
      }
      // repaint the applet drawing selected shape 
      repaint();  // repaint() calls paint()

    } // end of method actionPerformed()
  } // end of class ActionHandler

}  // end of class DrawShapes

