import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class GreenhouseApplet extends Applet implements ActionListener
{
	private TextField personHeightField;
	private TextField lengthField;
	private TextField budgetField;
	private TextField costField;
	private Label widthLabel;
	private Label heightLabel;
	private Label angleLabel;
	private Label usableSpaceLabel;
	
	public void init()
	{
		setLayout(new BorderLayout());
		
		URL imageURL;
		Image image;
		ImageCanvas imageCanvas = null;
		try
		{
			imageURL = new URL(getCodeBase(), "greenhouse-small.gif");
			image = getImage(imageURL);
			prepareImage(image, null);
			imageCanvas = new ImageCanvas(image);
		}
		catch(MalformedURLException e)
		{
			e.printStackTrace();
		}

		Panel mainPanel = new Panel(new GridLayout(2,1));

		Panel inputPanel = new Panel(new GridLayout(0,2));
		
		personHeightField = new TextField("6");
		inputPanel.add(new Label("P - height of tallest person (ft):"));
		inputPanel.add(personHeightField);
		
		lengthField = new TextField("100");
		inputPanel.add(new Label("L - length of wall (ft):"));
		inputPanel.add(lengthField);

		budgetField = new TextField("4000");
		inputPanel.add(new Label("B - budget for glass slab ($):"));
		inputPanel.add(budgetField);
		
		costField = new TextField("1");
		inputPanel.add(new Label("C - cost of glass ($ per square ft):"));
		inputPanel.add(costField);

		Panel outputPanel = new Panel(new GridLayout(0,2));

		widthLabel = new Label("0");
		outputPanel.add(new Label("W - width of glass slab (ft):"));
		outputPanel.add(widthLabel);

		heightLabel = new Label("0");
		outputPanel.add(new Label("H - height of greenhouse at wall (ft):"));
		outputPanel.add(heightLabel);

		usableSpaceLabel = new Label("0");
		outputPanel.add(new Label("Usable floor space (square ft):"));
		outputPanel.add(usableSpaceLabel);

		angleLabel = new Label("0");
		outputPanel.add(new Label("@ - angle of glass slab to ground (r):"));
		outputPanel.add(angleLabel);
		
		Button calcButton = new Button("Calculate");
		calcButton.addActionListener(this);
		outputPanel.add(calcButton);

		mainPanel.add(inputPanel);
		mainPanel.add(outputPanel);
		
		if(imageCanvas != null)
			add("Center", imageCanvas);
		add("South", mainPanel);
	}

	public void actionPerformed(ActionEvent ae)
	{
		double personHeight = Double.parseDouble(personHeightField.getText());
		double length = Double.parseDouble(lengthField.getText());
		double budget = Double.parseDouble(budgetField.getText());
		double cost = Double.parseDouble(costField.getText());
		double width = budget / (length * cost);
		double angle = Math.asin(Math.pow(personHeight / width, (1.0/3.0)));
		double totalSpace = length * width * Math.cos(angle);
		double unusableSpace = personHeight * length / Math.tan(angle);
		double usableSpace = totalSpace - unusableSpace;
		double height = width * Math.sin(angle);

		widthLabel.setText("" + width);
		heightLabel.setText("" + height);
		angleLabel.setText("" + angle);
		usableSpaceLabel.setText("" + usableSpace);
	}
}
