// Demonstrate MOSFET. (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 MOSFET extends Applet implements ActionListener
{

   TextField vGSField, vDSField, vTnField, knField, wField, lField;
   String message = ""; // String containing system messages
   double vGS; 	// Voltage Gate Source
   double vDS;	// Voltage Drain Source
   double vTn;	// Threshold Voltgae n-channel
   double bn;	// Device transconductance n-channel
   double kn;	// Process transconductance n-channel
   double width;// Transistor width
   double length;//Transistor length
   double iDn;  // Drain current n-channel

   public void init()
   {
	Label vGSLabel = new Label( "VGS(mV): ", Label.RIGHT );
	Label vDSLabel = new Label( "VDS(mV): ", Label.RIGHT );
	Label vTnLabel = new Label( "VTn(mV): ", Label.RIGHT );
	Label knLabel = new Label( "Kn(uA/V^2): ", Label.RIGHT );
	Label wLabel = new Label( "W(um): ", Label.RIGHT );
	Label lLabel = new Label( "L(um): ", Label.RIGHT );

	vGSField = new TextField( 12 );
	vDSField = new TextField( 12 );
	vTnField = new TextField( 12 );
	knField = new TextField( 12 );
	wField = new TextField( 12 );
	lField = new TextField( 12 );


	// Add controls
	add( vGSLabel );
	add( vGSField );
	add( vDSLabel );
	add( vDSField );
	add( vTnLabel );
	add( vTnField );
	add( knLabel );
	add( knField );
	add( wLabel );
	add( wField );
	add( lLabel );
	add( lField );


	// Register ActionListeners to receive action events.
	vGSField.addActionListener( this );
	vDSField.addActionListener( this );
	vTnField.addActionListener( this );
	knField.addActionListener( this );
	wField.addActionListener( this );
	lField.addActionListener( this );

   }

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

   public void paint( Graphics g )
   {

	message = "";

	try
	{
		vGS = 0.001 * (double)Long.parseLong( vGSField.getText() );
	}
	catch( NumberFormatException exceptionText )
	{
		message = "\"VGS\" field must contain a number: " + exceptionText;
		vGS = 0.0;
	}
	try
	{
		vDS = 0.001 * (double)Long.parseLong( vDSField.getText() );	
	}
	catch( NumberFormatException exceptionText )
	{
		message = "\"VDS\" field must contain a number: " + exceptionText;
		vDS = 0.0;
	}
	try
	{
		vTn = 0.001 * (double)Long.parseLong( vTnField.getText() );	
	}
	catch( NumberFormatException exceptionText )
	{
		message = "\"VTn\" field must contain a number: " + exceptionText;
		vTn = 0.0;
	}
	try
	{
		kn = 0.000001 * (double)Long.parseLong( knField.getText() );	
	}
	catch( NumberFormatException exceptionText )
	{
		message = "\"kn\" field must contain a number: " + exceptionText;
		kn = 0.0;
	}
	try
	{
		width = 0.000001 * (double)Long.parseLong( wField.getText() );	
	}
	catch( NumberFormatException exceptionText )
	{
		message = "\"W\" field must contain a number: " + exceptionText;
		width = 0.0;
	}
	try
	{
		length = 0.000001 * (double)Long.parseLong( lField.getText() );	
	}
	catch( NumberFormatException exceptionText )
	{
		message = "\"L\" field must contain a number: " + exceptionText;
		length = 1.0;
	}
	
	
	
	// Calculate Drain Current for transistor
	bn = ( kn * ( width / length ) );
	if( vDS <= ( vGS - vTn ) )
	{
		iDn = bn / 2 * ( 2 * ( vGS - vTn ) * vDS - ( vDS * vDS ) );
		// Square-Law model non-saturated mode current flow
	}
	else
	{
		iDn = bn / 2 * ( vGS - vTn ) * ( vGS - vTn );
		// Square-Law model saturated mode current flow
		// Assuming lambda = 0
	}

	
	g.drawString( "VGS(V): " + vGS, 6 ,200 );
	g.drawString( "VDS(V): " + vDS, 6, 225 );
	g.drawString( "Vtn(V): " + vTn, 6, 250 );
	g.drawString( "Bn(A/V^2):  " + bn, 6, 275 );
	g.drawString( "IDn(A) = " + iDn, 6, 300 );


	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 ( "MOSFET" );
	MOSFET application = new MOSFET ();

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

}
