/** An Applet prgram that draws lines, rectangles, or ovals depending on 
 user's choice entered in a JTextField; it demonstrates the use of the
 repaint() method to update the applet display after each user input;
 the applet is run with the following html file:
    <html>
      <applet code = "RepaintTest.class" width = "200" height = "100">
      </applet>
    </html>
  This html file (say, named "RepaintTest.html") can be run as follows:
    appletviewer RepaintTest.html
  or it can be opened from a web browser such as IE or Netscape
  (Nov. 22, 2001)
*/

import java.awt.Graphics;  // Java core package
import javax.swing.*;  // Java extension packages
import java.awt.*;
import java.awt.event.*;

public class RepaintTest extends JApplet implements ActionListener {
  int choice = 0;  // user's choice for drawing 
  JLabel label = new JLabel("Enter a number 1 - 3: ");;
  JTextField text = new JTextField("", 3);

  /** initialize applet by initilaizing the GUI */
  public void init() {
    Container p = getContentPane();
    p.setLayout(new FlowLayout());
    p.add(label);
    p.add(text);
    text.addActionListener(this);
    repaint();
  }

  /** called when data is entered in the ITextField, followed
    by the ENTER key */
  public void actionPerformed(ActionEvent event) {
    if (!text.getText().equals(""))
      choice = Integer.parseInt(text.getText());
    else 
      choice = 0;

    // repaint the applet displaying results of the new choice
    repaint();  // repaint() calls paint()
  }

  /** draw shapes on applet's background */
  public void paint(Graphics g) {
    super.paint(g);  // call inherited paint() method

    if (choice != 0) // paint nothing if choice == 0
      // draw a chosen shape      
      switch (choice) {
        case 1:  g.drawLine(10, 35, 180, 90);
                 break;
        case 2:  g.drawRect(10, 35, 60, 60); // 60 x 60 square
                 break;
        case 3:  g.drawOval(10, 35, 60, 60); // circle of diameter 60
                 break;
        default: g.drawString("Invalid value entered", 10, 40);
                 break;
      }  // end of switch
  }  // end of paint()
}  // end of class RepaintTest
