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

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

public class diprelaxation extends Applet {

      G2Dint graph1        = new G2Dint();   // Graph class to do the plotting
      G2Dint graph2        = new G2Dint();
      Axis xaxis1;
      Axis yaxis1;
      Axis xaxis2;
      Axis yaxis2;
      DataSet data1a;
      DataSet data2a;
      DataSet data1b;
      DataSet data2b;
      int     Change_a  = 1;

      TextField Pinput               = new TextField(5);       // Number of points 
      TextField EPSSinput              = new TextField(10);      // Defektenkonzentration
      TextField EPSEinput              = new TextField(10);      // Temperatur
      Button plot                    = new Button("Plot It!"); // Button to plot it.





      public void init() {
      	 Label title            = new Label("Dipole Relaxation",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")); 
         EPSSinput.setText(getParameter("EPSS")); 
         EPSEinput.setText(getParameter("EPSE")); 
         
         
         panel.setLayout(gridbag);

 
         Label Plabel           = new Label( "Number of Points" );  
         Label EPSSlabel          = new Label(" Epsilon start");
         Label EPSElabel          = new Label("Epsilon end");
         
         

         Plabel.setFont(font);
         EPSSlabel.setFont(font);
         EPSElabel.setFont(font);
         
         Pinput.setFont(font);
         Pinput.setBackground(Color.lightGray);
         EPSSinput.setFont(font);
         EPSSinput.setBackground(Color.lightGray);
         EPSEinput.setFont(font);
         EPSEinput.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);
         
// 	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);

//    	 Defdekt Konzentration 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(EPSElabel,c);
         c.gridwidth=2;
         c.anchor = GridBagConstraints.WEST;
//         c.gridwidth=GridBagConstraints.REMAINDER;
         gridbag.setConstraints(EPSEinput,c);

         panel.add(graph1);
         panel.add(graph2);
         panel.add(plot);
         panel.add(Plabel);
         panel.add(Pinput);
         panel.add(EPSSlabel);
         panel.add(EPSSinput);
         panel.add(EPSElabel);
         panel.add(EPSEinput);
         
//	Graph Plot 1

         xaxis1 = graph1.createXAxis();
         xaxis1.setTitleText("log(omega)");
         yaxis1 = graph1.createYAxis();


         data1a = new DataSet();

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

         data1b = new DataSet();

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


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

//      Graph Plot 2

         xaxis2 = graph2.createXAxis();
         xaxis2.setTitleText("log(omega)");
        
         yaxis2 = graph2.createYAxis();
 
 
         data2a = new DataSet();
 
         xaxis2.attachDataSet(data2a);
         yaxis2.attachDataSet(data2a);
         graph2.attachDataSet(data2a);

         data2b = new DataSet();
 
         xaxis2.attachDataSet(data2b);
         yaxis2.attachDataSet(data2b);
         graph2.attachDataSet(data2b);
 
 
         graph2.setDataBackground(new Color(255,255,255));
         graph2.setBackground(new Color(240,240,240));


         plot();

      }



      void plot() {
         int 		Points;
         double 	EPSS;
         double 	EPSE;
         double 	x1;
         double         xmaximum1;
         double         xminimum1;
         double         y1,y2,de1,de2;
         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;
         }
         
//	Temperature

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

         this.showStatus("Calculating points!");
 	 xminimum1= -5;
	 xmaximum1= +5; 
         for(int i=0; i<Points; i++) {
             x1= xminimum1 + i*(xmaximum1-xminimum1)/(Points-1);
             d1[count] = x1;
             d2[count] = x1;
             try {
             x1  = 2.303*Math.exp(x1);
             de1 = ((EPSS-EPSE)/(1+(x1*x1)));           
             de2 = EPSE+de1;
               d1[count+1] = de2  ;
               d2[count+1] = x1*de1;
                  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("E'");
         yaxis2.setTitleText("E''");

         Change_a = (~ Change_a);
 
         if( Change_a==1) { 
                        data1a.deleteData();
                        data2a.deleteData();

                        try {
                             data1a.append(d1,count/2);
                             data1a.linecolor = Color.red;  
                             data2a.append(d2,count/2);
                             data2a.linecolor = Color.red;  
                            } catch(Exception e) {
                        this.showStatus("Error while appending data!"); 
                        System.out.println("Error while appending data!");
                        return;
	                }
                        }
                   else { 
                        data1b.deleteData();
                        data2b.deleteData();

                        try {
                             data1b.append(d1,count/2);
                             data1b.linecolor = Color.blue;  
                             data2b.append(d2,count/2);
                             data2b.linecolor = Color.blue;  
                            } catch(Exception e) {
                        this.showStatus("Error while appending data!"); 
                        System.out.println("Error while appending data!");
                        return;
	                }
                        }

try{
         graph1.repaint();
         graph2.repaint();
           } catch(Exception e) {
                        this.showStatus("Error while drawing data!"); 
                        System.out.println("Error while drawing data!");
                        return;
	                }
      }



     public boolean action(Event e, Object a) {

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


         return false;
       }




}




