// Calculate Energy Levels of a Bound Electrom In an Infinite
// Potential Well. (Java 1.1 or later version)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.String.*;
import java.lang.Double.*;

public class BoundEELevels extends Applet implements ActionListener
{

   TextField nField, aField;
   String message = ""; // String containing system messages
   int n; 		// Energy Level Number
   int n_max;		// Maximum Energy Level Number
   double a;		// Width of infinite potential well
   double h = 1.054E-34;// Planck's Constant
   double pi = 3.14159;	// pI
   double m = 9.11E-31;	// Electron mass
   double q = 1.6E-19;	// Charge on an electron
   double energy;	// Energy of energy level.
   double energy_eV;	//Energy of energy level in eV.

   public void init()
   {
	Label nLabel = new Label( "Energy Level Number: ", Label.RIGHT );
	Label aLabel = new Label( "Infinite Potential Well Width(Angstrom): ", Label.RIGHT );

	nField = new TextField( 12 );
	aField = new TextField( 12 );


	// Add controls
	add( nLabel );
	add( nField );
	add( aLabel );
	add( aField );


	// Register ActionListeners to receive action events.
	nField.addActionListener( this );
	aField.addActionListener( this );

   }

   // ActionListeners.
   public void actionPerformed( ActionEvent ae )
   {
	repaint();
   }

   public void paint( Graphics g )
   {

	message = "";

	try
	{
		n_max = 1 * (int)Long.parseLong( nField.getText() );
	}
	catch( NumberFormatException exceptionText )
	{
		message = "\"n_max\" field must contain a number: " + exceptionText;
		n_max = 0;
	}
	try
	{
		a = 1E-10 * (double)Long.parseLong( aField.getText() );	
	}
	catch( NumberFormatException exceptionText )
	{
		message = "\"a\" field must contain a number: " + exceptionText;
		a = 1.0;
	}
	
	
	
	// Calculate energy in Joules of energy level n.
	energy = ( h * h * n_max * n_max * pi * pi ) / ( 2 * m * a * a );

	// Calculate energy in electron volts.
	energy_eV = energy / q;


	// Draw Ground State
	g.drawLine( 350, 390, 390, 390 );

	// Draw Energy Level
	g.drawLine( 350, (int)(390 - energy_eV * 10), 390, (int)(390 - energy_eV * 10) );

	g.drawString( "Energy Level(n_max): " + n_max, 6 ,200 );
	g.drawString( "width(m): " + a, 6, 225 );
	g.drawString( "Energy of level " + n + "(J) = " + energy, 6, 275 );
	g.drawString( "Energy of level " + n + "(eV) = " + energy_eV, 6, 300 );

	// Draw Energy Levels
	for ( n = 1; n <= n_max; n++ )
	{
		energy_eV = ( h * h * n * n * pi * pi ) / ( 2 * m * a * a * q );
		g.drawString( "n=" + n + " :E=" + energy_eV + "(eV)", 320, (int)(390 - energy_eV * 10) );
		g.drawLine( 350, (int)(390 - energy_eV * 10), (int)(350 + a * 1E+11), (int)(390 - energy_eV * 10) );
	}

	g.drawString( "Message: " + message, 6, 325 );
   }

   // main() is the entry point when called as an application.
   // main() is not used when run as an applet.
   public static void main ( String args[] )
   {
	Frame mainWindow = new Frame ( "BoundEELevels" );
	BoundEELevels application = new BoundEELevels ();

	mainWindow.setSize ( 400, 400 );
	mainWindow.add ( "Center", application );
	mainWindow.show ();
	application.init();
   }

}
