// RectDriverApplet.java
// Applet Driver program for Rect.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// import user defined class Rect in edu.lehigh.notes package
import edu.lehigh.notes.Rect;

public class RectDriverApplet extends JApplet implements ActionListener {
	private Rect r;
	private double perimeter, area;

	// GUI components
	private Container c;
	private JLabel labelLength, labelWidth, labelArea, labelPerim;
	private JTextField inputLength, inputWidth, outputArea, outputPerim;
	private JButton compute;

	public void init( ) {
		c = getContentPane( );
		c.setLayout( new FlowLayout( ) );
		labelLength = new JLabel( "Enter length:  " );
		labelWidth = new JLabel( "Enter width:  " );
		labelArea = new JLabel( "Area of rectangle:  " );
		labelPerim = new JLabel( "Perimeter of rectangle:  " );
		inputLength = new JTextField( 10 );
		inputWidth = new JTextField( 10 );
		outputArea = new JTextField( 10 );
		outputArea.setEditable( false );
		outputPerim = new JTextField( 10 );
		outputPerim.setEditable( false );
		compute = new JButton( "COMPUTE" );
		compute.addActionListener( this );

		c.add( labelLength );
		c.add( inputLength );
		c.add( labelWidth );
		c.add( inputWidth );
		c.add( labelArea );
		c.add( outputArea );
		c.add( labelPerim );
		c.add( outputPerim );
		c.add( compute );
	}

	public void actionPerformed( ActionEvent e ) {
		double l = Double.parseDouble( inputLength.getText( ) );
		double w = Double.parseDouble( inputWidth.getText( ) );

		// instantiate an object r of class Rect
		r = new Rect( l, w );

		// invoke area and perim methods of class Rect on object r
		area = r.area( );
		perimeter = r.perim( );
	
		// invoke method showResults
		showResults( area, perimeter );
	}

	public void showResults( double a, double p ) {
		outputArea.setText( "" + a );
		outputPerim.setText( "" + p );
	}
}