// Hypotenuse.java
// Computes hypotenuse given two double sides
// ExerciseD6.14
// APPLET CODE="Hypotenuse" HEIGHT=300 WIDTH=150
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class Hypotenuse extends JApplet implements ActionListener {
	// GUI Components
	Container c;
	JLabel labelS1, labelS2, labelH;
	JTextField fieldS1, fieldS2, fieldH;

	double s1, s2, h;
	DecimalFormat twoPlaces;

	public void init( ) {
		// GUI 
		c = getContentPane( );
		c.setLayout( new FlowLayout( ) );

		labelS1 = new JLabel( "Enter the first side" );
		c.add( labelS1 );
		fieldS1 = new JTextField( 10 );
		c.add( fieldS1 );
		labelS2 = new JLabel( "Enter the second side" );
		c.add( labelS2 );
		fieldS2 = new JTextField( 10 );
		fieldS2.addActionListener( this );	// listen for events in fieldS2
		c.add( fieldS2 );
		labelH = new JLabel( "Hypotenuse" );
		c.add( labelH );
		fieldH = new JTextField( 10 );
		c.add( fieldH );
		
		twoPlaces = new DecimalFormat( "0.00" );
	}

	public void actionPerformed( ActionEvent e ) {
		s1 = Double.parseDouble( fieldS1.getText( ) );
		s2 = Double.parseDouble( fieldS2.getText( ) );
		h = calcHypot( s1, s2 );
		fieldH.setText( twoPlaces.format( h ) );
	}

	double calcHypot( double s1, double s2 ) {
		return Math.sqrt( s1 * s1 + s2 * s2 );
	}
}