import java.awt.*;
import java.util.*;
import java.applet.*;
import graph.*;

/*
**************************************************************************
**
**                      Applet MHR 1
**
**************************************************************************
**    Copyright (C) 
**
**************************************************************************
*
* This applet uses 
*
*************************************************************************/

public class defekt_2 extends Applet {

      G2Dint graph1        = new G2Dint();   // Graph class to do the plotting
      G2Dint graph2        = new G2Dint();
      G2Dint graph3        = new G2Dint();               
      Axis xaxis1;
      Axis yaxis1;
      Axis xaxis2;
      Axis yaxis2;
      Axis xaxis3;
      Axis yaxis3;
      DataSet data1;
      DataSet data2;
      DataSet data3;

      /*TextField pinput      = new TextField(5);       // Number of points */
      /*TextField xmininput   = new TextField(10);      // Minimum x value input*/
      /*TextField xmaxinput   = new TextField(10);      // Maximum x value input*/
      /*TextField tempinput   = new TextField(10);      // Temperatur*/
      /*TextField relninput   = new TextField(10);      // N/N'*/
      /*TextField hsinput     = new TextField(10);        // Hs*/

      TextField Pinput               = new TextField(5);       // Number of points 
/*    TextField Debyelaengeinput     = new TextField(15);      //Debeyelaenge */
      TextField ACinput              = new TextField(10);      // Atom Concentration
      TextField EPSSinput            = new TextField(10);      // Dielectic Number
      TextField Tempinput            = new TextField(10);      // Temperatur
      TextField HBinput              = new TextField(10);      // Hb
      TextField HPinput              = new TextField(10);      // H+
      TextField HMinput              = new TextField(10);      // H-
      Button plot                    = new Button("Plot It!"); // Button to plot it.
      Label Debyewertlabel           = new Label("1e-13");





      public void init() {
      	 Label title            = new Label("Cation and Anion Concentration",Label.CENTER);
      	 Panel panel            = new Panel();
         GridBagLayout gridbag  = new GridBagLayout();
         GridBagConstraints  c  = new GridBagConstraints();
         Font font              = new Font("TimesRoman",Font.PLAIN,15);

         title.setFont(new Font("TimesRoman",Font.PLAIN,25));
          
         
         setLayout(new BorderLayout() );
         add("North",title);
         add("Center",panel);

         Pinput.setText(getParameter("POINTS")); 
/*       Debyelaengeinput.setText(getParameter("Debeyelaenge")); */
         ACinput.setText(getParameter("AC")); 
         EPSSinput.setText(getParameter("EPSS")); 
         Tempinput.setText(getParameter("TEMP")); 
         HBinput.setText(getParameter("HB")); 
         HPinput.setText(getParameter("HP")); 
         HMinput.setText(getParameter("HM")); 
         
         
         panel.setLayout(gridbag);

         
         Label Plabel           = new Label( "Number of Points" );  
         Label Debyelaengelabel = new Label("Debye length [nm]");
         Label AClabel          = new Label("Atom Concentration [cm^-3]");
         Label EPSSlabel        = new Label("Dielectric Number");
         Label Templabel        = new Label("Temperature °C");
         Label HBlabel          = new Label("Hb [e V]");
         Label HPlabel          = new Label("H+ [e V]");
         Label HMlabel          = new Label("H- [e V]");
         

         Plabel.setFont(font);
         Debyelaengelabel.setFont(font);
         Debyewertlabel.setFont(font);
	 AClabel.setFont(font);
         EPSSlabel.setFont(font);
         Templabel.setFont(font);
         HBlabel.setFont(font);
         HPlabel.setFont(font);
         HMlabel.setFont(font);
/*       pinput.setFont(font);
         pinput.setBackground(Color.lightGray);
         xmininput.setFont(font);
         xmininput.setBackground(Color.lightGray);
         xmaxinput.setFont(font);
         xmaxinput.setBackground(Color.lightGray);
         tempinput.setFont(font);
         tempinput.setBackground(Color.lightGray);
         relninput.setFont(font);
         relninput.setBackground(Color.lightGray);
         hsinput.setFont(font);
         hsinput.setBackground(Color.lightGray);
         plot.setFont(font);
         plot.setBackground(Color.green);*/

         Pinput.setFont(font);
         Pinput.setBackground(Color.lightGray);
/*       Debyelaengeinput.setFont(font);*/
/*       Debyelaengeinput.setBackground(Color.lightGray);*/
         ACinput.setFont(font);
         ACinput.setBackground(Color.lightGray);
         EPSSinput.setFont(font);
         EPSSinput.setBackground(Color.lightGray);
         Tempinput.setFont(font);
         Tempinput.setBackground(Color.lightGray);
         HBinput.setFont(font);
         HBinput.setBackground(Color.lightGray);
         HPinput.setFont(font);
         HPinput.setBackground(Color.lightGray);
         HMinput.setFont(font);
         HMinput.setBackground(Color.lightGray);

         plot.setFont(font);
         plot.setBackground(Color.green);

//	GRAPH

         c.weightx = 1.0;
         c.weighty = 1.0;
         c.gridwidth = 3;
         c.gridwidth=GridBagConstraints.REMAINDER;
         c.fill  =  GridBagConstraints.BOTH;
         gridbag.setConstraints(graph1,c);
         gridbag.setConstraints(graph2,c);
         gridbag.setConstraints(graph3,c);

// 	PLOT

         c.fill  =  GridBagConstraints.NONE;
         c.weightx=0.0;
         c.weighty=0.0;
         c.gridheight=1;
         c.anchor = GridBagConstraints.CENTER;
         gridbag.setConstraints(plot,c);
         
//	PINPUT

/*       c.gridwidth=1;
         c.anchor = GridBagConstraints.EAST;
         gridbag.setConstraints(plabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;
         c.gridwidth=GridBagConstraints.REMAINDER;
         gridbag.setConstraints(pinput,c);*/

//	X INPUT

/*       c.gridwidth=1;
         c.anchor = GridBagConstraints.EAST;
         gridbag.setConstraints(xminlabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;*/
//         c.gridwidth=GridBagConstraints.REMAINDER;
/*       gridbag.setConstraints(xmininput,c);

         c.gridwidth=1;
         c.anchor = GridBagConstraints.EAST;
         gridbag.setConstraints(xmaxlabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;
         c.gridwidth=GridBagConstraints.REMAINDER;
         gridbag.setConstraints(xmaxinput,c);*/

//	Y INPUT

/*       c.gridwidth=1;
         c.anchor = GridBagConstraints.EAST;
         gridbag.setConstraints(templabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;*/
//         c.gridwidth=GridBagConstraints.REMAINDER;
/*         gridbag.setConstraints(tempinput,c);

         c.gridwidth=1;
         c.anchor = GridBagConstraints.EAST;
         gridbag.setConstraints(relnlabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;
         c.gridwidth=GridBagConstraints.REMAINDER;
         gridbag.setConstraints(relninput,c);*/

//	Z INPUT

/*       c.gridwidth=1;
         c.anchor = GridBagConstraints.EAST;
         gridbag.setConstraints(hslabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;*/
/*//         c.gridwidth=GridBagConstraints.REMAINDER;*/
/*         gridbag.setConstraints(hsinput,c);*/

 
//	PINPUT
         c.gridwidth=1;
         c.anchor = GridBagConstraints.EAST;
         gridbag.setConstraints(Plabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;
//         c.gridwidth=GridBagConstraints.REMAINDER;
         gridbag.setConstraints(Pinput,c);

//	DebyelaengeINPUT
         c.gridwidth=1;
         c.anchor = GridBagConstraints.EAST;
         gridbag.setConstraints(Debyelaengelabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;
         c.gridwidth=GridBagConstraints.REMAINDER;
         gridbag.setConstraints(Debyewertlabel,c);

//    	 Atom INPUT

         c.gridwidth=1;
         c.anchor = GridBagConstraints.EAST;
         gridbag.setConstraints(AClabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;
//         c.gridwidth=GridBagConstraints.REMAINDER;
         gridbag.setConstraints(ACinput,c);

//       Dielectric Input

         c.gridwidth=1;
         c.anchor = GridBagConstraints.EAST;
         gridbag.setConstraints(EPSSlabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;
         c.gridwidth=GridBagConstraints.REMAINDER;
         gridbag.setConstraints(EPSSinput,c);

//	 Temperature INPUT

         c.gridwidth=1;
         c.anchor = GridBagConstraints.EAST;
         gridbag.setConstraints(Templabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;
//         c.gridwidth=GridBagConstraints.REMAINDER;
         gridbag.setConstraints(Tempinput,c);

//       Hb Input
         c.gridwidth=1;
         c.anchor = GridBagConstraints.EAST;
         gridbag.setConstraints(HBlabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;
         c.gridwidth=GridBagConstraints.REMAINDER;
         gridbag.setConstraints(HBinput,c);

//	 H+ INPUT

         c.gridwidth=1;
         c.anchor = GridBagConstraints.EAST;
         gridbag.setConstraints(HPlabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;
//         c.gridwidth=GridBagConstraints.REMAINDER;
         gridbag.setConstraints(HPinput,c);

//	 H-INPUT

         c.gridwidth=1;
         c.anchor = GridBagConstraints.EAST;
         gridbag.setConstraints(HMlabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;
         c.gridwidth=GridBagConstraints.REMAINDER;
         gridbag.setConstraints(HMinput,c);

                 
/*       panel.add(graph1);
         panel.add(graph2);
         panel.add(graph3);
         panel.add(plot);
         panel.add(plabel);
         panel.add(pinput);
         panel.add(xminlabel);
         panel.add(xmininput);
         panel.add(xmaxlabel);
         panel.add(xmaxinput);
         panel.add(templabel);
         panel.add(tempinput);
         panel.add(relnlabel);
         panel.add(relninput);
         panel.add(hslabel);
         panel.add(hsinput);*/
 
         panel.add(graph1);
         panel.add(graph2);
         panel.add(graph3);
         panel.add(plot);
         panel.add(Plabel);
         panel.add(Pinput);
         panel.add(Debyelaengelabel);
         panel.add(Debyewertlabel);
         panel.add(AClabel);
         panel.add(ACinput);
         panel.add(EPSSlabel);
         panel.add(EPSSinput);
         panel.add(Templabel);
         panel.add(Tempinput);
         panel.add(HBlabel);
         panel.add(HBinput);
         panel.add(HPlabel);
         panel.add(HPinput);
         panel.add(HMlabel);
         panel.add(HMinput);
         
//	Graph Plot 1

         xaxis1 = graph1.createXAxis();
      /* xaxis1.setTitleText("h_{fp}/h_{s}"); */
         xaxis1.setTitleText("X/Debye length");
         yaxis1 = graph1.createYAxis();


         data1 = new DataSet();

         xaxis1.attachDataSet(data1);
         yaxis1.attachDataSet(data1);
         graph1.attachDataSet(data1);


         graph1.setDataBackground(new Color(255,255,255));
         graph1.setBackground(new Color(240,240,240));

//      Graph Plot 2

         xaxis2 = graph2.createXAxis();
/*       xaxis2.setTitleText("h_{fp}/h_{s}"); */
         xaxis2.setTitleText("X/Debye length");
        
         yaxis2 = graph2.createYAxis();
 
 
         data2 = new DataSet();
 
         xaxis2.attachDataSet(data2);
         yaxis2.attachDataSet(data2);
         graph2.attachDataSet(data2);
 
 
         graph2.setDataBackground(new Color(255,255,255));
         graph2.setBackground(new Color(240,240,240));

//      Graph Plot 3

         xaxis3 = graph3.createXAxis();
/*       xaxis3.setTitleText("h_{fp}/h_{s}"); */
         xaxis3.setTitleText("X/Debye length"); 

         yaxis3 = graph3.createYAxis();
 
 
         data3 = new DataSet();
 
         xaxis3.attachDataSet(data3);
         yaxis3.attachDataSet(data3);
         graph3.attachDataSet(data3);
 
 
         graph3.setDataBackground(new Color(255,255,255));
         graph3.setBackground(new Color(240,240,240));



         plot();

      }



      void plot() {
         int 		Points;
         double         Debyelaenge;
         double 	AC;
         double 	EPSS;
         double 	Temp;
         double 	HB;
         double 	HP;
         double         HM;
         double 	x;
         double         xmaximum;
         double         xminimum;
         double         ht,hv0,hv,hnp,hnm,hnplast,hnmlast,de1,de2,de3,h1v,h2v,h3v,h4v,h1p,h2p,h3p,h1n,h2n,h3n;
         int 		count = 0;
         boolean 	error = false;
//	Point value

       try {
              Points   = Integer.parseInt(Pinput.getText());
         } catch(Exception e) {
              this.showStatus("Error with number of points!");
              System.out.println("Number of points error "+e.getMessage());
              return;
         }
         
       try {
              EPSS   = Double.valueOf(EPSSinput.getText()).doubleValue();
         } catch(Exception e) {
              this.showStatus("Error with EPSS!");
              System.out.println("EPSS error "+e.getMessage());
              return;
         }


//	Temperature

       try {
            Temp = Double.valueOf(Tempinput.getText()).doubleValue();
         } catch(Exception e) {
              this.showStatus("Error with Temerature value!");
              System.out.println("Temperature error "+e.getMessage());
              return;
         }
 
         try {
            AC = Double.valueOf(ACinput.getText()).doubleValue();
         } catch(Exception e) {
              this.showStatus("Error with AC value!");
              System.out.println("AC error "+e.getMessage());
              return;
         } 
 

//	Z max - min value
 
       try {
            HB = Double.valueOf(HBinput.getText()).doubleValue();
         } catch(Exception e) {
              this.showStatus("Error HB value!");
              System.out.println("HB error "+e.getMessage());
              return;
         } 
 
//	HP,M value

         try {
              HP   = Double.valueOf(HPinput.getText()).doubleValue();
         } catch(Exception e) {
              this.showStatus("Error with HP!");
              System.out.println("HP error "+e.getMessage());
              return;
         }

         try {
              HM   = Double.valueOf(HMinput.getText()).doubleValue();
         } catch(Exception e) {
              this.showStatus("Error with HM!");
              System.out.println("HM error "+e.getMessage());
              return;
         }
   
 
         double d1[] = new double[2*Points];
         double d2[] = new double[2*Points];
         double d3[] = new double[2*Points];

         this.showStatus("Calculating points!");
         if (Math.abs(HP-HM)<1e-8) {
           HM = HM+1e-8; }
         ht  = 0.0257*(273+Temp)/298;
         de1 = ((AC*1.6E-19)/(1E12*ht*2*3.14*EPSS*8.8542E-12))*Math.exp(-0.5*(HP+HM)/ht);
         de2 = Math.sqrt(de1);  
         Debyelaenge = 1/de2;  //Debyelaenge
         Debyewertlabel.setText(Double.toString(Debyelaenge));   
	 xminimum = 0;
	 xmaximum = 4; 
         h1v = 2*ht*0.5*(HP-HM);
         h3v = 0.5*(HP-HM);
         for(int i=0; i<Points; i++) {
             x = xminimum + i*(xmaximum-xminimum)/(Points-1);
             d1[count] = x;
             d2[count] = x;
             d3[count] = x;
             try {
                  h2v = Math.exp(-x);
                  hv  = h3v*(-h2v+1);             //V
                  hnplast = ((-HP+hv)/ht);
                  hnmlast = ((-HM-hv)/ht);  
                  d1[count+1] = hnplast/2.303;
                  d2[count+1] = hnmlast/2.303;
                  d3[count+1] = hv;
                  count += 2;
             } catch(Exception e) { error = true; }

         }
 

         if(count <= 2) {
             this.showStatus("Error NO POINTS to PLOT!"); 
             System.out.println("Error NO POINTS to PLOT!");
             return;
         } else
         if( error ) {
             this.showStatus("Error while Calculating points!"); 
             System.out.println("Error while calculating points!");
	 }


         yaxis1.setTitleText("Log(n+/N)");
         yaxis2.setTitleText("Log(n-/N)");
         yaxis3.setTitleText("V [Volt]");

         data1.deleteData();
         data2.deleteData();
         data3.deleteData();

         try {
               data1.append(d1,count/2);
               data2.append(d2,count/2);
               data3.append(d3,count/2);
         } catch(Exception e) {
             this.showStatus("Error while appending data!"); 
             System.out.println("Error while appending data!");
             return;
	 }
              

         graph1.repaint();
         graph2.repaint();
         graph3.repaint();
      }



     public boolean action(Event e, Object a) {

         if(e.target instanceof Button) {
             if( plot.equals(e.target) ) {
                  plot();
                  return true;
             }
         }


         return false;
       }




}




