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

/*
**************************************************************************
**
**                      Applet dipresonances
**
**************************************************************************
**    Copyright (C) 
**
**************************************************************************
*
* This applet uses 
*
*************************************************************************/

public class dipresonances 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 MASSinput            = new TextField(10);      // Mass
      TextField RTIMEinput           = new TextField(10);      // RelTime
      TextField YMODULinput          = new TextField(10);      // Young Modul
      Button plot                    = new Button("Plot It!"); // Button to plot it.





      public void init() {
      	 Label title            = new Label("Dipole Resonance",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")); 
         MASSinput.setText(getParameter("MASS")); 
         RTIMEinput.setText(getParameter("RTIME")); 
         YMODULinput.setText(getParameter("YMODUL")); 
         
         
         panel.setLayout(gridbag);

 
         Label Plabel           = new Label( "Number of Points" );  
         Label MASSlabel        = new Label(" MASS [u]");
         Label RTIMElabel       = new Label(" Rel. Time [1/Omega0]");
         Label YMODULlabel      = new Label(" Young Modul [GPa]");
         
         

         Plabel.setFont(font);
         MASSlabel.setFont(font);
         RTIMElabel.setFont(font);
         YMODULlabel.setFont(font);
         
         Pinput.setFont(font);
         Pinput.setBackground(Color.lightGray);
         MASSinput.setFont(font);
         MASSinput.setBackground(Color.lightGray);
         RTIMEinput.setFont(font);
         RTIMEinput.setBackground(Color.lightGray);
         YMODULinput.setFont(font);
         YMODULinput.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);

//    	 MASS INPUT

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

//	 RelTime INPUT

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


//	 YOUNG MODUL INPUT

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

         panel.add(graph1);
         panel.add(graph2);
         panel.add(plot);
         panel.add(Plabel);
         panel.add(Pinput);
         panel.add(MASSlabel);
         panel.add(MASSinput);
         panel.add(RTIMElabel);
         panel.add(RTIMEinput);
         panel.add(YMODULlabel);
         panel.add(YMODULinput);
         
//	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 	DENS = 5e21;
         double 	lnOMEGA0;
         double 	KF;
         double         MASS;
	 double         OMEGA0;
	 double         YMODUL;
	 double         VV;
         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;
         }
         
//	MASS

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

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

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

         this.showStatus("Calculating points!");
 	 xminimum1 = -6.0;
	 xmaximum1 = +6.0; 
         OMEGA0    = YMODUL * 3.0e26 / (MASS *1.66);
         OMEGA0    = Math.sqrt(OMEGA0);
         lnOMEGA0  = Math.log(OMEGA0)/2.303;
         VV = DENS/(MASS*OMEGA0*OMEGA0)*1.73e+7; 
         for(int i=0; i<Points; i++) {
             x1= xminimum1 + i*(xmaximum1-xminimum1)/(Points-1);
             d1[count] = x1+lnOMEGA0;
             d2[count] = x1+lnOMEGA0;
             try {
             x1  = Math.exp(2.303*x1);
             de1 = (1-x1*x1);
             de2 = de1*de1+KF*KF*x1*x1;
//               d1[count+1] = 1 + VV * de1/de2  ;
               d1[count+1] = 1+ VV * de1/de2  ;

               d2[count+1] = VV * KF * x1/de2;
                  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'/E0");
         yaxis2.setTitleText("E' '/E0");

         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;
       }




}




