/*
 * @(#)Flight.java 1.3 03/09/17
 *
 *  Project Name: Flight
 *  Author: Bryan B.
 *
 */
import java.io.*;
import java.util.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/* NOTICE NOTICE NOTICE */
// The following libraries were taken from the NetBeans GUI Extensions
import org.netbeans.lib.awtextra.AbsoluteConstraints;
import org.netbeans.lib.awtextra.AbsoluteLayout;
/* NOTICE NOTICE NOTICE */

public class Flight
{
	// Initilize our variables
	private static double altitude;
	private static double a, b, hyp;
	private static double radius = 3690;
	private static int viewable_distance;
	
	// Frames
	private JFrame frame = new JFrame("Air Flight Distance");
	private JFrame result_frame = new JFrame("Air Flight Distance - Result");
	
	// Labels
	private JLabel program_title = new JLabel();
	private JLabel enter_alt = new JLabel();
	private JLabel result = new JLabel();
	
	// Textfield(s)
	private JTextField alt = new JTextField();
    
    // Buttons
    private JButton submit = new JButton();
    private JButton exit = new JButton();
    private JButton close = new JButton();
    
    /** [START] Creates new form(s) for Flight **/
    public Flight() 
    {
        initComponents();
    }

	private void initComponents()
	{
		frame.getContentPane().setLayout(new AbsoluteLayout());
		
		// setup our program title
		program_title.setText("Air Flight Distance - Written by: Bryan B.");
		frame.getContentPane().add( program_title, 
			new AbsoluteConstraints(10, 0, 590, 20) );
		
		// setup our text field desc and text field
	    enter_alt.setText("Enter altitude:");
        frame.getContentPane().add(enter_alt, 
        	new AbsoluteConstraints(10, 30, -1, 20));

        alt.setText("0");
        frame.getContentPane().add(alt, 
        	new AbsoluteConstraints(100, 30, 110, -1));
        	
        // setup our button
        submit.setText("Submit Altitude");
        submit.addActionListener(
        	new ActionListener() 
        	{
	            public void actionPerformed(ActionEvent evt) {
                	altitudeSubmitted(evt);
            	}
        	});
	 	frame.getContentPane().add(submit, 
	 		new AbsoluteConstraints(20, 60, -1, -1));
	 		
	 	// setup exit button
        exit.setText("Exit");
        exit.addActionListener(
        	new ActionListener() 
        	{
            	public void actionPerformed(ActionEvent evt) 
            	{
	                PreformExit(evt);
            	}
        	});
        frame.getContentPane().add(exit, 
        	new AbsoluteConstraints(150, 60, -1, -1));

		// output frame
		frame.setSize(250,125);
		frame.setLocation(300,300);
		frame.setResizable( false );
		frame.show();
	}
		
    private void altitudeSubmitted(ActionEvent evt) 
    {
		altitude = Double.valueOf(alt.getText()).doubleValue();

		// ok lets compute out our variables
		radius *= 5280;
		// compute hypotenuse
		hyp = altitude + radius;
		// set side b
		b = radius;
		// compute side a
		a = Math.sqrt(Math.pow(hyp,2) - Math.pow(b,2));
		a /= 5280;
		viewable_distance = (int)a;

		// output our result
		System.out.println("The viewable distance (at an altitude of " + 
						   altitude + " feet) is: " + viewable_distance +
						   " miles");
		
		result_frame.getContentPane().setLayout(new AbsoluteLayout());

		// setup our result for output
		result.setText("The viewable distance (at an altitude of " + 
					altitude + " feet) is: " + viewable_distance +
					" miles");
		result_frame.getContentPane().add( result, 
			new AbsoluteConstraints(10, 0, 590, 20) );

        // setup our button
        close.setText("Close Window");
        close.addActionListener(
        	new ActionListener() 
        	{
	            public void actionPerformed(ActionEvent evt) {
					result_frame.hide();
            	}
        	});
	 	result_frame.getContentPane().add(close, 
	 		new AbsoluteConstraints(160, 30, -1, -1));

		// output frame
		result_frame.setSize(425,95);
		result_frame.setLocation(210,320);
		result_frame.setResizable( false );
		result_frame.show();
		
		// ok, reset all variables
		radius = 3690;
		a = b = hyp = viewable_distance = 0;
    }
    
    private void PreformExit(ActionEvent evt) 
    {
		System.exit(0);
    }    
    /** [END] Creates new form for Flight **/
    
	/** [START] Main program body **/
	public static void main(String args[])
	{
		new Flight();
		
		System.out.println("Air Flight Distance - Written by: Bryan B.");
	}
	/** [END] Main program body	**/
}
