// RectDriverApplication.java
// Driver program for Rect.java but is an application, not an applet
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 RectDriverApplication extends JFrame {
	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;

	// constructor
	public RectDriverApplication( ) {
		super ( "Rect Driver Application" );
		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( 
			new ActionListener( ) {
				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 );
				}
			}
		);
	
		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 );
	}

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

	public static void main( String args[ ] ) {
		RectDriverApplication w = new RectDriverApplication( );

		w.addWindowListener(
			new WindowAdapter( ) {
				public void windowClosing( WindowEvent e ) {
					System.exit( 0 );
				}
			}
		);

		w.setSize( 280, 200 );
		w.show( );
	}
}