//NewGuideInputPanel.java
/* A Java class for
 * TwoWire.java
 * Electromagnetic Transmission Line Applet
 * Applet without Smith Chart - Prepared by Umberto Ravaioli 
 * for 6th edition of Fundamentals of Applied Electromagnetics Book
 * May 2009 - 2019 All Rights Reserved
 */   

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import javax.swing.*;
import javax.swing.event.*;

//public class NewGuideInputPanel extends Panel implements ActionListener, AdjustmentListener{
public class NewGuideInputPanel extends Panel implements ActionListener, ChangeListener{
    private NewGuide_State state;
    private static final Color bgcolor = new Color(236,236,236);
    
    private Font labfont;
    private Font normalfont;
    private Font smallfont;
    private Font smallerfont;
    
    public int ScrollMax = 10000;
    private Label titlelabel;
    private Label lab1, lab2, lab3, lab4, lab5, lab6, lab7;
    private Label ulab1, ulab2, ulab3, ulab4, ulab5, ulab6;
    public TextField text1, text2, text3, text4, text5, text6, text7, text8;
    //public Scrollbar slider1, slider2, slider3, slider4;//, slider5;
    public JSlider slider1, slider2, slider3, slider4;
    //public Checkbox ch1, ch2, ch3;
    // Gauge check buttons
    //public Checkbox cg1, cg2, cg3, cg4, cg5, cg6;
    
    public double diameter_mm[], diameter_mil[];
    
    //public CheckboxGroup cgrp, cgauge;
    public  Button b1, b2, b3, b5;
    public  Button bupdate, binstr;
    public Panel ps1, ps2, ps5, ps6;
  
    public NewGuideInputPanel(NewGuide_State state){
	super();
	setLayout(null);
	setBackground(bgcolor);
	this.state = state;

        if (MaestroG.useBiggerFont) {
            labfont = state.sanSerifFont.deriveFont((float)state.font14);
            normalfont = state.sanSerifFont.deriveFont((float)state.font16);
            smallfont = state.sanSerifFont.deriveFont((float)state.font14);
            smallerfont = state.sanSerifFont.deriveFont((float)state.font13);
        } 
        else {
            labfont = state.sanSerifFont.deriveFont((float)state.font12);
            normalfont = state.sanSerifFont.deriveFont((float)state.font14);
            smallfont = state.sanSerifFont.deriveFont((float)state.font12);
            smallerfont = state.sanSerifFont.deriveFont((float)state.font11);
        }

        setFont(labfont);

	titlelabel = new Label("Input",Label.CENTER);
	if (MaestroG.useBiggerFont) {
            titlelabel.setFont(state.sanSerifFont.deriveFont(Font.BOLD,(float)state.font18));
        } else {
            titlelabel.setFont(state.sanSerifFont.deriveFont(Font.BOLD,(float)state.font16));
        }
			
	lab1 = new Label("Centers distance  D =  ",Label.RIGHT);
	lab2 = new Label("AWG = ",Label.LEFT);
	lab2.setFont(labfont);
	lab2.setForeground(Color.red);
	lab3 = new Label("Relative \u03bc = ",Label.RIGHT);
	lab3.setFont(labfont);
	lab4 = new Label("Frequency   f  =  ",Label.RIGHT);
        lab4.setFont(labfont);
	lab5 = new Label("Strip thickness t  =  ",Label.RIGHT);
        lab5.setFont(labfont);
	lab6 = new Label("Wire Diameter  d =  ",Label.RIGHT);
        lab6.setFont(labfont);
	lab7 = new Label("Line Length  L  =  ",Label.RIGHT);
        lab7.setFont(labfont);
		
	text1 = new TextField(""+state.a,8);
	text3 = new TextField(""+state.epsilon_r,8);
	text4 = new TextField(""+state.frequency,8);
	text5 = new TextField(""+state.lineLength,8);
	text6 = new TextField(""+state.w,8);
	text7 = new TextField(""+state.sigma2,8);
	//text7 = new TextField(""+state.loss_tangent,8);
	//text8 = new TextField(""+state.rho_metal);
        DecimalFormat dec = new DecimalFormat("0.###E0");
        // NOTE: sigma_metal is 1/rho_metal
	text8 = new TextField(""+dec.format(state.sigma_metal),8);
        text1.setFont(labfont);
        text3.setFont(labfont);
        text4.setFont(labfont);
        text5.setFont(labfont);
        text6.setFont(labfont);
        text7.setFont(labfont);
        text8.setFont(labfont);
	
	ulab1 = new Label(" [ mm ]",Label.LEFT); // not used
	ulab2 = new Label(" [ m ]",Label.LEFT);  // not used
	ulab3 = new Label(" [ mm ]",Label.LEFT); // not used
	ulab4 = new Label(" [ Hz ]",Label.LEFT);
	ulab5 = new Label(" [ S/m ]",Label.LEFT);
	ulab6 = new Label(" [ S/m ]",Label.LEFT);
	//ulab6 = new Label(" [ \u03a9 m ]",Label.LEFT);
	ulab4.setFont(labfont);
	ulab5.setFont(labfont);
	ulab6.setFont(labfont);

	slider1 = new JSlider(0,ScrollMax); //changes a
	slider2 = new JSlider(0,ScrollMax); //changes frequency
	slider3 = new JSlider(0,ScrollMax); //changes w
        slider4 = new JSlider(0,52); //changes w
	/*
	slider1 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes a
	slider2 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes frequency
	slider3 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes w
        slider4 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,52);//changes w
	*/
        
	//slider5 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes lineLength
	
	slider1.setValue((int)((state.a-state.a_minimum)*ScrollMax/(state.a_maximum-state.a_minimum)));
	slider2.setValue((int)((state.frequency-state.frequency_minimum)*ScrollMax/(state.frequency_maximum-state.frequency_minimum)));
	slider3.setValue((int)((state.w-state.w_minimum)*ScrollMax/(state.w_maximum-state.w_minimum)));
        slider4.setValue(25);

	/*
        slider1.setBackground(Color.white);
        slider2.setBackground(Color.white);
        slider3.setBackground(Color.white);
        slider4.setBackground(Color.white);
	*/
        
        //slider1.setForeground(Color.blue);
        //slider2.setForeground(Color.blue);
        //slider3.setForeground(Color.blue);
        //slider4.setForeground(Color.blue);
	//slider5.setValue((int)((state.lineLength-state.lineLengthMIN)*ScrollMax/(state.lineLengthMAX-state.lineLengthMIN)));
	
        FontMetrics fmSmall = getFontMetrics(smallfont);
        int buttonWidth = fmSmall.stringWidth("XRangeX");
	b1 = new Button("Range");
        b1.setFont(smallerfont);
	b2 = new Button("Range");
	b2.setFont(smallerfont);
        b3 = new Button("Range");
	b3.setFont(smallerfont);
        
        //b5 = new Button("Range");
	bupdate = new Button("Update");
        
        b1.setBackground(Color.white);
        b2.setBackground(Color.white);
        b3.setBackground(Color.white);
        bupdate.setBackground(Color.white);
        
        // update button
	binstr = new Button("Instructions");
        binstr.setBackground(Color.white);
	add(binstr);
        int buttonx = state.s175;
        int buttony = state.s10;
        int buttonwide = state.s100;
        int buttonheight = state.s22;
	 
        binstr.setBounds(buttonx,buttony,buttonwide,buttonheight);	
        Panel pin = new Panel();
	    pin.setBackground(Color.black);
	    add(pin);
	    pin.setBounds(buttonx-1,buttony-1,buttonwide+2,buttonheight+2);
        
	add(titlelabel);
	add(lab1);
	add(lab2);
	add(lab3);
	add(lab4);
	//add(lab5);
	add(lab6);
	//add(lab7);
	add(text1);
	//add(text2);
	add(text3);
	add(text4);
	add(text5);
	add(text6);
	add(text7);
	add(text8);
	
	add(ulab4);
	add(ulab5);
	add(ulab6);
        //add(ulab7);
	add(slider1);
	add(slider2);
	add(slider3);
        add(slider4);
	//add(slider5);
	add(b1);
	add(b2);
	add(b3);
	//add(b5);
	add(bupdate);
	
	titlelabel.setBounds(state.s5,state.s5,state.s50,state.s20);
	
	int vPosInit = state.s35, vPos, dvPos = state.s7, vHeight = state.s15; 
	vPos = vPosInit + state.s50; 
	lab6.setBounds(state.s20,vPos-state.s23,state.s145,vHeight);
	text6.setBounds(state.s170,vPos-state.s25,state.s80,vHeight+state.s3);
	Panel ptext6 = new Panel();
	    ptext6.setBackground(Color.black);
	    add(ptext6);
	    ptext6.setBounds(state.s170-1,vPos-state.s25-1,state.s80+2,vHeight+state.s3+2);
	
	// slider for  inner radius
	slider3.setBounds(state.s80,vPos,state.s196,vHeight);
	/*
	    ps1 = new Panel();
	    ps1.setBackground(Color.cyan);
	    add(ps1);
	    ps1.setBounds(state.s80-1,vPos-1,state.s196+2,vHeight+2);
	    
	    ps2 = new Panel();
	    ps2.setBackground(Color.black);
	    add(ps2);
	    ps2.setBounds(state.s80-2,vPos-2,state.s196+4,vHeight+4);
	*/
	    
	    lab2.setBounds(state.s7,vPos-state.s2,state.s40,state.s16);
            b3.setBounds(state.s10,vPos-state.s2,state.s60,state.s20); // range button
            Panel pb3 = new Panel();
            pb3.setBackground(Color.black);
	    add(pb3);
	    pb3.setBounds(state.s10-1,vPos-state.s2-1,state.s60+2,state.s20+2);
            
            //Wire gauge slider
            slider4.setBounds(state.s80,vPos,state.s196,vHeight);
	    /*
	    ps5 = new Panel();
	    ps5.setBackground(Color.cyan);
	    add(ps5);
	    ps5.setBounds(state.s80-1,vPos-1,state.s196+2,vHeight+2);
	    
	    ps6 = new Panel();
	    ps6.setBackground(Color.black);
	    add(ps6);
	    ps6.setBounds(state.s80-2,vPos-2,state.s196+4,vHeight+4);
	    */
            
	    lab2.setVisible(false);
            slider4.setVisible(false);
            //ps5.setVisible(false);
            //ps6.setVisible(false);
            
	// band III
	int y_sub = state.s135;   
	lab1.setBounds(state.s20,y_sub-state.s23,state.s145,vHeight);    
	text1.setBounds(state.s170,y_sub-state.s25,state.s80,vHeight+state.s3);
	Panel ptext1 = new Panel();
	    ptext1.setBackground(Color.black);
	    add(ptext1);
	    ptext1.setBounds(state.s170-1,y_sub-state.s25-1,state.s80+2,vHeight+state.s3+2);
        
	slider1.setBounds(state.s80,y_sub,state.s196,vHeight);
	/*
	Panel ps9 = new Panel();
	    ps9.setBackground(Color.cyan);
	    add(ps9);
	    ps9.setBounds(state.s80-1,y_sub-1,state.s196+2,vHeight+2);
	    
	Panel ps10 = new Panel();
	    ps10.setBackground(Color.black);
	    add(ps10);
	    ps10.setBounds(state.s80-2,y_sub-2,state.s196+4,vHeight+4);
	*/
	
        b1.setBounds(state.s10,y_sub-state.s2,state.s60,state.s20);
        Panel pb1 = new Panel();
            pb1.setBackground(Color.black);
	    add(pb1);
	    pb1.setBounds(state.s10-1,y_sub-state.s2-1,state.s60+2,state.s20+2);
            
	// band IV
	int y_frequency = state.s185;
	lab4.setBounds(state.s20,y_frequency-state.s23,state.s145,vHeight);
	text4.setBounds(state.s170,y_frequency-state.s25,state.s80,vHeight+state.s3);
        Panel ptext4 = new Panel();
	    ptext4.setBackground(Color.black);
	    add(ptext4);
	    ptext4.setBounds(state.s170-1,y_frequency-state.s25-1,state.s80+2,vHeight+state.s3+2);
	ulab4.setBounds(state.s252,y_frequency-state.s25,state.s40,vHeight);   
	    
	slider2.setBounds(state.s80,y_frequency,state.s196,vHeight);
	/*
	Panel ps5 = new Panel();
	    ps5.setBackground(Color.cyan);
	    add(ps5);
	    ps5.setBounds(state.s80-1,y_frequency-1,state.s196+2,vHeight+2);
	    
	Panel ps6 = new Panel();
	    ps6.setBackground(Color.black);
	    add(ps6);
	    ps6.setBounds(state.s80-2,y_frequency-2,state.s196+4,vHeight+4);
	*/
	
        b2.setBounds(state.s10,y_frequency-state.s2,state.s60,state.s20); // range button
        Panel pb2 = new Panel();
            pb2.setBackground(Color.black);
	    add(pb2);
	    pb2.setBounds(state.s10-1,y_frequency-state.s2-1,state.s60+2,state.s20+2);
	
	// band V
	int y_material = state.s227;
        text3.setBounds(state.s20,y_material,state.s70,vHeight+state.s3);
	text7.setBounds(state.s105,y_material,state.s80,vHeight+state.s3);
	text8.setBounds(state.s200,y_material,state.s80,vHeight+state.s3);
        
        Panel ptext3 = new Panel();
	    ptext3.setBackground(Color.black);
	    add(ptext3);
	    ptext3.setBounds(state.s20-1,y_material-1,state.s70+2,vHeight+state.s3+2);
	Panel ptext7 = new Panel();
	    ptext7.setBackground(Color.black);
	    add(ptext7);
	    ptext7.setBounds(state.s105-1,y_material-1,state.s80+2,vHeight+state.s3+2);
        Panel ptext8 = new Panel();
	    ptext8.setBackground(Color.black);
	    add(ptext8);
	    ptext8.setBounds(state.s200-1,y_material-1,state.s80+2,vHeight+state.s3+2);
        
	ulab5.setBounds(state.s133,y_material-state.s18,state.s50,vHeight);
	ulab6.setBounds(state.s230,y_material-state.s18,state.s50,vHeight);
	
	//  band VI
	int y_update = state.s258;
	bupdate.setBounds(state.s20,y_update,state.s260,state.s20);	
	Panel ps7 = new Panel();
	    ps7.setBackground(Color.cyan);
	    add(ps7);
	    ps7.setBounds(state.s20-1,y_update-1,state.s260+2,state.s20+2);
	    
	Panel ps8 = new Panel();
	    ps8.setBackground(Color.black);
	    add(ps8);
	    ps8.setBounds(state.s20-2,y_update-2,state.s260+4,state.s20+4);

	//Listeners
	bupdate.addActionListener(this);

	slider1.addChangeListener(this);
	slider2.addChangeListener(this);
	slider3.addChangeListener(this);
        slider4.addChangeListener(this);
	/*
	slider1.addAdjustmentListener(this);
	slider2.addAdjustmentListener(this);
	slider3.addAdjustmentListener(this);
        slider4.addAdjustmentListener(this);
	*/
        //slider5.addAdjustmentListener(this);
	        
        diameter_mm = new double[53];
        diameter_mil = new double[53];
        double test = 0.0;
        
        // Calculates diameters for AWG 1/0 to 6/0
            
            test = 5.0* Math.pow(92.0,(36.0+5)/39);
	    diameter_mil[0] = MaestroA.rounder(test,6);  // AWG 6/0
	    diameter_mm[0] = MaestroA.rounder(test*0.0254,6);

            test = 5.0 * Math.pow(92.0,(36.0+4)/39);  
            diameter_mil[1] = MaestroA.rounder(test,6);  // AWG 5/0
	    diameter_mm[1] = MaestroA.rounder(test*0.0254,6);
            
            test = 5.0 * Math.pow(92.0,(36.0+3)/39);  
            diameter_mil[2] = MaestroA.rounder(test,6);  // AWG 4/0
	    diameter_mm[2] = MaestroA.rounder(test*0.0254,6);
            
            test = 5.0 * Math.pow(92.0,(36.0+2)/39);
            diameter_mil[3] = MaestroA.rounder(test,6);  // AWG 3/0
	    diameter_mm[3] = MaestroA.rounder(test*0.0254,6);
            
            test = 5.0 * Math.pow(92.0,(36.0+1)/39);
            diameter_mil[4] = MaestroA.rounder(test,6);  // AWG 2/0
	    diameter_mm[4] = MaestroA.rounder(test*0.0254,6);
            
            test = 5.0 * Math.pow(92.0,(36.0)/39);
	    diameter_mil[5] = MaestroA.rounder(test,6);  // AWG 1/0
	    diameter_mm[5] = MaestroA.rounder(test*0.0254,6);
            
        // Caluclate diameters for AWG 1 - 32
            
        for(int i=0;i<32;i++){
            test = 5.0 * Math.pow(92.0,(36.0-(i+1))/39);
	    diameter_mil[i+6] = MaestroA.rounder(test,6);
	    diameter_mm[i+6] = MaestroA.rounder(test*0.0254,6);
	}

                test = 5.0 * Math.pow(92.0,(36.0-(40))/39);
                diameter_mil[51] = MaestroA.rounder(test,6);
                diameter_mm[51] = MaestroA.rounder(test*0.0254,6);
                //diameter_mil[51] = 3.1;// AWG 40
                //diameter_mm[51] = 0.07874;
        	
                test = 5.0 * Math.pow(92.0,(36.0-(39))/39);
                diameter_mil[50] = MaestroA.rounder(test,6);
                diameter_mm[50] = MaestroA.rounder(test*0.0254,6);
                //diameter_mil[50]= 3.5;// AWG 39
                //diameter_mm[50] = 0.0889;
                
                diameter_mil[49]= 3.94;// Metric 1.0
                diameter_mm[49] = 0.1;
            
                test = 5.0 * Math.pow(92.0,(36.0-(38))/39);
                diameter_mil[48] = MaestroA.rounder(test,6);
                diameter_mm[48] = MaestroA.rounder(test*0.0254,6);
                //diameter_mil[48] = 4.0;// AWG 38
                //diameter_mm[48] = 0.1016;
                
                diameter_mil[47] = 4.41;// Metric 1.12
                diameter_mm[47] = 0.112;
            
                test = 5.0 * Math.pow(92.0,(36.0-(37))/39);
                diameter_mil[46] = MaestroA.rounder(test,6);
                diameter_mm[46] = MaestroA.rounder(test*0.0254,6);
                //diameter_mil[46] = 4.5;// AWG 37
                //diameter_mm[46] = 0.1143;
            
                diameter_mil[45] = 4.92;// Metric 1.25
                diameter_mm[45] = 0.125;
            
                test = 5.0 * Math.pow(92.0,(36.0-(36))/39);
                diameter_mil[44] = MaestroA.rounder(test,6);
                diameter_mm[44] = MaestroA.rounder(test*0.0254,6);
                //diameter_mil[44] = 5.0;// AWG 36
                //diameter_mm[44] = 0.127;
            
                diameter_mil[43] = 5.51;// Metric 1.4
                diameter_mm[43] = 0.140;
            
                test = 5.0 * Math.pow(92.0,(36.0-(35))/39);
                diameter_mil[42] = MaestroA.rounder(test,6);
                diameter_mm[42] = MaestroA.rounder(test*0.0254,6);
                //diameter_mil[42] = 5.6;// AWG 35
                //diameter_mm[42] = 0.14224;
                
                diameter_mil[41] = 6.3;// Metric 1.6 and AWG 34
                diameter_mm[41] = 0.16002;
            
                diameter_mil[40] = 7.09;// Metric 1.8
                diameter_mm[40] = 0.18;
            
                diameter_mil[39] = 5.0 * Math.pow(92.0,(36.0-(33))/39);// AWG 33
                diameter_mm[39] = MaestroA.rounder((diameter_mil[39]*0.0254),8);
                
                diameter_mil[38] = 7.87;// Metric 2.0
                diameter_mm[38] = 0.2;    
}
    
    public void paint(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g.clearRect(0,0,getSize().width-1,getSize().height-1);
        
        g.setFont(normalfont);
	FontMetrics fm;
        fm = g.getFontMetrics();
        
	g.setColor(bgcolor.darker());
	g.fillRect(0,getSize().height-2,getSize().width,2);
	g.fillRect(getSize().width-2,0,2,getSize().height);
	g.setColor(bgcolor.brighter());
	g.fillRect(0,0,2,getSize().height-1);
	g.fillRect(0,0,getSize().width-2,2);
        
        g.setColor(Color.black);
        g.drawRect(0,0,getSize().width-1,getSize().height-1);
        
	int vPosInit = state.s30, vPos, dvPos = state.s2, vHeight = state.s20; 
	vPos = vPosInit+9*vHeight+6*dvPos;
	g.setColor(Color.red);    
	
	MaestroG.subscripterSymFirst2("\u03b5","r","",g,state.font14,state.s32,vPos-state.s7);	
	MaestroG.subscripterSymFirst2("\u03c3","","",g,state.font14,state.s120,vPos-state.s7);	
	MaestroG.subscripterSymFirst2("\u03c3","c","",g,state.font14,state.s210,vPos-state.s7);

        g.setColor(Color.black);    
        MaestroG.subscripter("[mm]","","",g,state.font12,state.s255,state.s72);
        MaestroG.subscripter("[mm]","","",g,state.font12,state.s255,state.s123);
	
        int x1 = state.s120; int y1 = state.s97; int xoffset = state.s30;
            
        //g.setColor(Color.red.darker());
        //g.drawLine(10,y1+5,getSize().width-10,y1+5);
	slider1.requestFocusInWindow();
	slider2.requestFocusInWindow();
	slider3.requestFocusInWindow();
	slider4.requestFocusInWindow();
    }


    
    public void actionPerformed(ActionEvent evt){
        if(evt.getSource()==bupdate){
            //*********
            try{ if(Double.valueOf(text1.getText()).doubleValue()<=state.a_maximum &&
                    Double.valueOf(text1.getText()).doubleValue()>=state.a_minimum){
                    state.a=Double.valueOf(text1.getText()).doubleValue();
                    slider1.setValue((int)(ScrollMax*(state.a-state.a_minimum)
                                           /(state.a_maximum-state.a_minimum)));
                }
                else if(Double.valueOf(text1.getText()).doubleValue()>state.a_maximum){
                    state.a = state.a_maximum;
                    text1.setText(""+state.a_maximum);
                    slider1.setValue(ScrollMax);
                }
                else if(Double.valueOf(text1.getText()).doubleValue()<state.a_minimum){
                    state.a = state.a_minimum;
                    text1.setText(""+state.a_minimum);
                    slider1.setValue(0);
                }
                else if(Double.valueOf(text1.getText()).doubleValue()<state.w){
                    state.a =state.w;
                    text1.setText(""+state.a);
                    slider1.setValue((int)(ScrollMax*(state.a-state.a_minimum)
                                           /(state.a_maximum-state.a_minimum)));
                }
            }
            catch(NumberFormatException e){
                state.a =  0.0;
            } 
            
            try{if(Double.valueOf(text6.getText()).doubleValue()<=state.w_maximum &&
                   Double.valueOf(text6.getText()).doubleValue()>=state.w_minimum){
                    state.w=Double.valueOf(text6.getText()).doubleValue();
                    slider3.setValue((int)(ScrollMax*(state.w-state.w_minimum)/
                                           (state.w_maximum-state.w_minimum)));
                }
                else if(Double.valueOf(text6.getText()).doubleValue()>state.w_maximum){
                    state.w = state.w_maximum;
                    text6.setText(""+state.w_maximum);
                    slider3.setValue(ScrollMax);
                }
                else if(Double.valueOf(text6.getText()).doubleValue()<state.w_minimum){
                    state.w = state.w_minimum;
                    text6.setText(""+state.w_minimum);
                    slider3.setValue(0);
                }
                
            }
            catch(NumberFormatException e){
                state.w = 0.0;
                
            }
            	    
	    try{if(Double.valueOf(text3.getText()).doubleValue()>=1.0){
		    state.epsilon_r=Double.valueOf(text3.getText()).doubleValue();
		}
		else{
		    state.epsilon_r=1.0;
		    text3.setText(""+state.epsilon_r);
		}
	    }
	    catch(NumberFormatException e){
                state.epsilon_r = 1.0;
	    }  
	    	    
	    try{if(Double.valueOf(text7.getText()).doubleValue()>=0.0){
		    state.sigma2=Double.valueOf(text7.getText()).doubleValue();
		    //state.loss_tangent=Double.valueOf(text7.getText()).doubleValue();
		}
		else{
		    state.sigma2=0.0;
		    text7.setText(""+state.sigma2);
		    //state.loss_tangent=0.0;
		    //text7.setText(""+state.loss_tangent);
		}
	    }
	    catch(NumberFormatException e){
                state.sigma2 = 0.0;
                //state.loss_tangent = 0.0;
	    }  
	    try{if(Double.valueOf(text8.getText()).doubleValue()>=0.0){
		    //state.rho_metal=Double.valueOf(text8.getText()).doubleValue();
		    state.sigma_metal=Double.valueOf(text8.getText()).doubleValue();
                    state.rho_metal = 1.0/state.sigma_metal;
		}
		else{
		    //state.rho_metal=0.0;
		    //text8.setText(""+state.rho_metal);
		    state.sigma_metal=1.0E-99;
                    state.rho_metal=1.0/state.sigma_metal;
		}
	    }
	    catch(NumberFormatException e){
                //state.rho_metal = 0.0;
                state.sigma_metal = 0.0;
	    }
            
	    try{if(Double.valueOf(text4.getText()).doubleValue()<=state.frequency_maximum &&
                   Double.valueOf(text4.getText()).doubleValue()>=state.frequency_minimum){
		    state.frequency=Double.valueOf(text4.getText()).doubleValue();
		    slider2.setValue((int)(ScrollMax*(state.frequency-state.frequency_minimum)/
                                           (state.frequency_maximum-state.frequency_minimum)));
		}
		else if(Double.valueOf(text4.getText()).doubleValue()>state.frequency_maximum){
		    state.frequency = state.frequency_maximum;
		    text4.setText(""+state.frequency_maximum);
		    slider2.setValue(ScrollMax);
		}
		else if(Double.valueOf(text4.getText()).doubleValue()<state.frequency_minimum){
		    state.frequency = state.frequency_minimum;
		    text4.setText(""+state.frequency_minimum);
		    slider2.setValue(0);
		}
	    }
	    catch(NumberFormatException e){
                state.frequency = 0.0; 
	    } 
        }
        state.ignition();
    }//End of actionPerformed()


    
    public void stateChanged(ChangeEvent evt){
	if(evt.getSource()==slider2){
	    if(slider2.getValue()>=ScrollMax){
		state.frequency=state.frequency_maximum;
		slider2.setValue((int)(ScrollMax*(state.frequency-state.frequency_minimum)
						    /(state.frequency_maximum-state.frequency_minimum)));
	    }
	    else{
		state.frequency = state.frequency_minimum + 
		    (state.frequency_maximum-state.frequency_minimum)*slider2.getValue()/ScrollMax;
	    }
	    
	    text4.setText(""+state.frequency);
	    state.ignition();
	    
	}
	//-----------------------------------------
	else if(evt.getSource()==slider1){
	  if(!state.IsMilsOn){
	    if(slider1.getValue()>=ScrollMax){
		    state.a=state.a_maximum;
		    slider1.setValue((int)(ScrollMax*(state.a-state.a_minimum)
						    /(state.a_maximum-state.a_minimum)));
	    }
	    else{
	    state.a  = state.a_minimum + 
		(state.a_maximum-state.a_minimum)*slider1.getValue()/ScrollMax;
	    }
	    text1.setText(""+MaestroA.rounder(state.a,7));
	    state.ignition();
	    
	    
	    if(state.a <= state.w){
		    state.a =state.w;
		    
		    slider1.setValue((int)(ScrollMax*(state.a-state.a_minimum)
						    /(state.a_maximum-state.a_minimum)));
		    text1.setText(""+MaestroA.rounder(state.a,7));
		    state.ignition();
		    
	    }
	
	    
	  }
	  else{
	    if(slider1.getValue()>=ScrollMax){
		    state.a_mil=state.a_mil_maximum;
		    slider1.setValue((int)(ScrollMax*(state.a_mil-state.a_mil_minimum)
						    /(state.a_mil_maximum-state.a_mil_minimum)));
	    }
	    else{
	    state.a_mil  = state.a_mil_minimum + 
		(state.a_mil_maximum-state.a_mil_minimum)*slider1.getValue()/ScrollMax;
	    }
	    text1.setText(""+MaestroA.rounder(state.a_mil,7));
	    state.ignition();
	    
	    
	    if(state.a_mil<=state.w_mil){
		    state.a_mil =state.w_mil;
		    
		    slider1.setValue((int)(ScrollMax*(state.a_mil-state.a_mil_minimum)
						    /(state.a_mil_maximum-state.a_mil_minimum)));
		    text1.setText(""+MaestroA.rounder(state.a_mil,7));
		    state.ignition();
		    
	    }
	  }
	}
	//----------------------------------------------------------------------
	
	else if(evt.getSource()==slider3){
	    state.WG = 0;
	  if(!state.IsMilsOn){
	    if(slider3.getValue()>=ScrollMax){
		    state.w=state.w_maximum;
		    slider3.setValue((int)(ScrollMax*(state.w-state.w_minimum)
						    /(state.w_maximum-state.w_minimum)));
	    }
	    else{
		state.w = state.w_minimum + 
		    (state.w_maximum-state.w_minimum)*slider3.getValue()/ScrollMax;
	    }
	
	    text6.setText(""+MaestroA.rounder(state.w,7));
	    state.ignition();
	    
	    if(state.w > state.a){
		    state.w =state.a;
		    
		    slider3.setValue((int)(ScrollMax*(state.w-state.w_minimum)
						    /(state.w_maximum-state.w_minimum)));
		    text6.setText(""+MaestroA.rounder(state.w,7));
		    state.ignition();
		    
	    }
	    
	  }
	  else{
	    if(slider3.getValue()>=ScrollMax){
		    state.w_mil=state.w_mil_maximum;
		    slider3.setValue((int)(ScrollMax*(state.w_mil-state.w_mil_minimum)
						    /(state.w_mil_maximum-state.w_mil_minimum)));
	    }
	    else{
		state.w_mil = state.w_mil_minimum + 
		    (state.w_mil_maximum-state.w_mil_minimum)*slider3.getValue()/ScrollMax;
	    }
	
	    text6.setText(""+MaestroA.rounder(state.w_mil,7));
	    state.ignition();
	    
	    if(state.w_mil > state.a_mil){
		    state.w_mil =state.a_mil;
		    
		    slider3.setValue((int)(ScrollMax*(state.w_mil-state.w_mil_minimum)
						    /(state.w_mil_maximum-state.w_mil_minimum)));
		    text6.setText(""+MaestroA.rounder(state.w_mil,7));
		    state.ignition();
		    
	    }
	  }
	}
        //------------------------------------------------------------------------------
        
        else if(evt.getSource()==slider4){
            int slider_ref ;
            slider_ref = slider4.getValue();
            
            state.w_mil = diameter_mil[slider4.getValue()];
            state.w = diameter_mm[slider4.getValue()];
            state.WG = slider4.getValue()+1;
            
            while(state.w > state.a){
                slider_ref = slider_ref + 1;
                slider4.setValue(slider_ref);
                state.w_mil = diameter_mil[slider4.getValue()];
                state.w = diameter_mm[slider4.getValue()];
                state.WG = slider4.getValue()+1;
            }
            
            if(state.IsMilsOn){  
                text6.setText(""+MaestroA.rounder(state.w_mil,7));
            }
            else{
                text6.setText(""+MaestroA.rounder(state.w,7));
            }
	}
        
        //---------------------------------------------
	/*
        else if(evt.getSource()==slider5){
	  if(!state.IsMilsOn){
	    if(slider5.getValue()>=ScrollMax){
		    state.lineLength=state.lineLengthMAX;
		    slider5.setValue((int)(ScrollMax*(state.lineLength-state.lineLengthMIN)
						    /(state.lineLengthMAX-state.lineLengthMIN)));
	    }
	    else{
		state.lineLength = state.lineLengthMIN + 
		    (state.lineLengthMAX - state.lineLengthMIN)*slider5.getValue()/ScrollMax;
	    }
	
	    text5.setText(""+MaestroA.rounder(state.lineLength,7));
	    state.ignition();	
	    
	  }
	  else{
	    if(slider5.getValue()>=ScrollMax){
		    state.lineLength_mil=state.lineLengthMAX_mil;
		    slider5.setValue((int)(ScrollMax*(state.lineLength_mil-state.lineLengthMIN_mil)
						    /(state.lineLengthMAX_mil-state.lineLengthMIN_mil)));
	    }
	    else{
		state.lineLength_mil = state.lineLengthMIN_mil + 
		    (state.lineLengthMAX_mil - state.lineLengthMIN_mil)*slider5.getValue()/ScrollMax;
	    }
	    text5.setText(""+MaestroA.rounder(state.lineLength_mil,7));
	    state.ignition();
	    
	  }
	}
	*/
    }
}
