import java.awt.*;
import java.awt.event.*;
//import java.applet.*;
//import java.lang.*;

public class ObliqueCanvas extends Canvas implements MouseListener{
    private static final Color bgcolor = new Color(200,200,200);
    private static final Color medium1Color = new Color(250,250,250);
    private static final Color medium2Color = new Color(200,220,220);
    
    private int LeftMargin, RightMargin, TopMargin, BottomMargin;
    private static final Font labfont = new Font("SanSerif",Font.PLAIN,10);
    private static final Font axisfont = new Font("Serif",Font.ITALIC,18);
    public static final Font normalfont = new Font("Sanserif",Font.PLAIN,12);
    public static final Font normalfontB = new Font("Sanserif",Font.BOLD,12);
    public static final Font smallfont = new Font("Sanserif",Font.PLAIN,10);
    public static final Font symbfont = new Font("Serif",Font.PLAIN,12);
    private static final Font labfontbig=new Font("SanSerif",Font.PLAIN,14);
    private static final Font labfontbigI=new Font("Serif",Font.ITALIC,14);
    
    private int HGap, VGap;//for vector fields.
    private Image im;
    private Graphics buf;
    private double extra = 0.1;
    private Oblique_State state;
    private int radius, radius_power, radius_powernew, scale_radius;
    private boolean IsFocusOn, IsPrintOn;
    public boolean Field_Out, Field_Refl_Out, ShowTR, ShowREF;
    private Color incident = Color.red; 
    
    public ObliqueCanvas(Oblique_State state){
	super();
	this.state = state;
        LeftMargin = state.s10; RightMargin = state.s10; 
        TopMargin = state.s10; BottomMargin = state.s10;
        HGap = state.s10;
        VGap = state.s10;
        scale_radius = 150;
	IsFocusOn = false;
        IsPrintOn = false;
        Field_Out = false;
        Field_Refl_Out = true;
        ShowTR = false;
        ShowREF = false;        
	setBackground(bgcolor);
	radius = getSize().height/2-TopMargin-scale_radius*VGap/20;
        radius_power = radius * 6/10;
        //Listeners
	this.addMouseListener(this);
    }

    public void drawCanvas(Graphics g){
	g.setColor(Color.black);
	g.draw3DRect(0,0,getSize().width-1,getSize().height-1,false);
	drawMedium(g);
	drawAxis(g);
        //if(!IsFocusOn){drawY(g);}
        drawLabels(g);
	drawBackLabels(g);
        drawWaves2(g);
        if(IsFocusOn){drawScaleRefTrans2(g, Color.red, 6);}
        //else{drawY(g);}
    }
    
    private void drawMedium(Graphics g){/// OK WITH ULABY's BOOK CONVENTIONS
	g.setColor(medium1Color);
	g.fillRect(1,1,getSize().width/2-1,getSize().height-2);
	g.setColor(medium2Color);
	g.fillRect(getSize().width/2,1,getSize().width/2-1,getSize().height-2);
    }
    
    private void drawAxis(Graphics g){/// OK WITH ULABY's BOOK CONVENTIONS
	g.setColor(Color.black);
	//horizontal axis - z
	g.drawLine(LeftMargin,getSize().height/2,getSize().width-RightMargin - state.s10,getSize().height/2);
	MaestroG.drawArrow( getSize().width-RightMargin - state.s10, getSize().height/2, 7, g);
        //vertical axis - x (pointing up in Ulaby's book)
	g.drawLine(getSize().width/2,TopMargin,getSize().width/2,getSize().height-BottomMargin - state.s10);
        MaestroG.drawArrow(getSize().width/2, TopMargin + 10, 5, g);
    }

    private void drawY(Graphics g){/// OK WITH ULABY's BOOK CONVENTIONS
	double x1, y1, radiusy, radiusX, radiusdot; 
        x1 = (double)(getSize().width/2);
        y1 = (double)(getSize().height/2);
        radiusy = (double)state.s7;
        radiusdot = (double)state.s2;
        radiusX = (double)state.s8;
        Color colorX = new Color(100,100,100);
               
        int rule; float alpha;
        rule = AlphaComposite.SRC_OVER;
        alpha = 1.0f;
        Graphics2D g2d = (Graphics2D)g;
        g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        MaestroG.fillCircleThick(g, x1, y1,radiusy,state.s1,colorX);
        MaestroG.fillCircleThick(g, x1, y1,radiusy - state.s1,state.s1,Color.white);
        MaestroG.fillCircleThick(g, x1, y1,radiusdot,state.s1,colorX);      
        g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
    }
    
    private void drawLabels(Graphics g){
	//g.setFont(axisfont);
        g.setFont(new Font("Serif",Font.ITALIC,state.font18));
        FontMetrics fm = g.getFontMetrics();
	String tmp;
	radius = getSize().height/2-TopMargin-scale_radius*VGap/20;
        radius_power = radius*6/10;
        
        Graphics2D g2d = (Graphics2D)g;
        g.setColor(Color.blue.darker());
	
	tmp = "x";
	g.drawString(tmp,getSize().width/2-2*fm.stringWidth(tmp) - state.s5,fm.getHeight());
	tmp = "z";
	g.drawString(tmp,getSize().width-RightMargin-fm.stringWidth(tmp) - state.s5,getSize().height/2+fm.getHeight());
	tmp = "y";
	g.drawString(tmp,(getSize().width/2+fm.stringWidth(tmp)),getSize().height/2+fm.getHeight());
	
        //g.setFont(normalfontB);
        g.setFont(new Font("Sanserif",Font.BOLD,state.font12));
        g.setColor(Color.red.darker());
	tmp =  "medium 1";
	g.drawString(tmp,LeftMargin +state.s10,getSize().height-7*VGap);
	g.setColor(Color.blue.darker());
	tmp  = "medium 2";
	g.drawString(tmp,getSize().width-state.s95,getSize().height-7*VGap);
	
        //int x0 = getSize().width/4;
        //int x1 = getSize().width/2 + state.s20;
        //int y0 = getSize().height - state.s15;
	//int y1 = getSize().height - state.s30;
        
        int x0 = getSize().width/4;
        int x1 = getSize().width/2 + state.s70;
        int y0 = getSize().height - state.s20;
	int y1 = getSize().height - state.s30;
        int y3 = getSize().height - state.s5;
        //g.setFont(smallfont);
        g.setFont(new Font("Sanserif",Font.PLAIN,state.s10));
        g.setColor(Color.red.darker());
        
        if(IsPrintOn){    
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            if(IsFocusOn){
                g.drawString("Click to Hide",x1-state.s50,y3);
            }
            else{
                g.drawString("Click to Visualize Power Flow",x1-state.s50,y3);
            }
        }
        if(IsFocusOn){
                //g.setFont(normalfontB);
                g.setFont(new Font("Sanserif",Font.BOLD,state.font12));
                g.setColor(Color.red);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g.drawString("Power Flow",x1+state.s40,y3);
                MaestroG.drawLineThick(g, x1+state.s15, y3-state.s5, x1+state.s35, y3-state.s5, 3, Color.red);
        }
        
        g.setColor(Color.black);
        //g.setFont(labfontbig);
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        
        //frequency
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		
                double f_normalized;
		double frequency = state.frequency;
	
		if(frequency < 1.0E3){
		    f_normalized = frequency;
		    g.drawString("    =  "+MaestroA.rounder(f_normalized,6)+"  Hz",x0,y0);	
		}
		else if(frequency < 1.0E6 && frequency >= 1.0E3  ){
		    f_normalized = frequency/1.0E3;
		    g.drawString("    =  "+MaestroA.rounder(f_normalized,6)+"  kHz",x0,y0);	
		}
		else if(frequency < 1.0E9 && frequency >= 1.0E6 ){
		    f_normalized = frequency/1.0E6;
		    g.drawString("    =  "+MaestroA.rounder(f_normalized,6)+"  MHz",x0,y0);	
		}
		else if(frequency < 1.0E12 && frequency >= 1.0E9 ){
		    f_normalized = frequency/1.0E9;
		    g.drawString("    =  "+MaestroA.rounder(f_normalized,6)+"  GHz",x0,y0);	
		}
		else if(frequency < 1.0E15 && frequency >=1.0E12 ){
		    f_normalized = frequency/1.0E12;
		    g.drawString("   =  "+MaestroA.rounder(f_normalized,6)+"  THz",x0,y0);	
		}
		else{
		    f_normalized = frequency/1.0E12;
		    g.drawString("    =  "+MaestroA.rounder(f_normalized,6)+"  THz",x0,y0);
		}
                //g.setFont(labfontbigI);
                g.setFont(new Font("Serif",Font.ITALIC,state.font14));
		g.drawString("f",x0-state.s2,y0);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    }
    
    private void drawBackLabels(Graphics g){/// DONE
	Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        int mediumX, mediumY;
	radius = getSize().height/2-TopMargin-scale_radius*VGap/20;
	FontMetrics fm = g.getFontMetrics();
	//For medium 1
	mediumX = LeftMargin + state.s5;
	mediumY = getSize().height-5*VGap;
	
        g.setColor(Color.red.darker());
	MaestroG.subscripter("\u03b5","r 1"," = "+state.epsilon_r1,g,state.font13,mediumX+state.s7,mediumY); 
	//g.setFont(normalfont);
        g.setFont(new Font("Sanserif",Font.PLAIN,state.font12));
        fm = g.getFontMetrics();
        
	mediumY += 4*fm.getHeight()/3;
        
	MaestroG.subscripter("\u03bc","r 1"," = "+state.mu_r1,g,state.font13,mediumX + state.s5,mediumY);
	//g.setFont(normalfont);
	g.setFont(new Font("Sanserif",Font.PLAIN,state.font12));
        fm = g.getFontMetrics();
        
	mediumY += fm.getHeight();
        //MaestroG.subscripter("  \u03c3","1"," = "+state.conductivity1,g,state.font13,mediumX + state.s5,mediumY);  
	//g.setFont(normalfontB);
	g.setFont(new Font("Sanserif",Font.BOLD,state.font12));
        fm = g.getFontMetrics();
        
        mediumY = fm.getHeight();
        
        int Xpolar = state.s100+state.s50;
        if(state.isPolarizationParallel){
            g.setColor(Color.blue);
            g.drawString("     Parallel",Xpolar,mediumY);
            mediumY += fm.getHeight();
            g.drawString("  Polarization",Xpolar,mediumY);
            mediumY += fm.getHeight();
            g.drawString("       (TM)",Xpolar,mediumY);
        }
        else{
            g.setColor(Color.magenta.darker());
            g.drawString("Perpendicular",Xpolar,mediumY);
            mediumY += fm.getHeight();
            g.drawString("  Polarization",Xpolar,mediumY);
            mediumY += fm.getHeight();
            g.drawString("       (TE)",Xpolar,mediumY);
        }
        
	//For medium 2
	g.setColor(Color.blue.darker());
	mediumX = getSize().width - state.s100;
        mediumY = getSize().height - 5*VGap;
        
        MaestroG.subscripter("\u03b5","r 2"," = "+state.epsilon_r2,g,state.font13,mediumX + state.s7,mediumY);    
	//g.setFont(normalfont);
        g.setFont(new Font("Sanserif",Font.PLAIN,state.font12));
        fm = g.getFontMetrics();
        
	mediumY += 4*fm.getHeight()/3;
	MaestroG.subscripter("\u03bc","r 2"," = "+state.mu_r2,g,state.font13,mediumX + state.s5,mediumY);
	//g.setFont(normalfont);
	g.setFont(new Font("Sanserif",Font.PLAIN,state.font12));
        fm = g.getFontMetrics();
        
	mediumY += fm.getHeight();
	//MaestroG.subscripter("  \u03c3","2"," = "+state.conductivity2,g,state.font13,mediumX + state.s5,mediumY);  
	g.setFont(normalfont); 
	fm = g.getFontMetrics();
    }
   
    //-------------------------------------------------------------------------- 
        private void drawWaves2(Graphics g){
        double x1, y1, x2, y2;
        int thickness = state.s5;
        int lab_pos = state.s28;
        
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
	boolean Scaled_Waves = true;
	radius = getSize().height/2-TopMargin-scale_radius*VGap/20;
        radius_powernew = 93*radius/233;
        
	g.setColor(incident);
	//incident ray
	
        double Len = (double)state.s15;
        double radiusV, fieldL, x2_power, y2_power;
        int thicknessV, thicknessV2, thicknessV3, radiusH;
        double angolo, angolo2, angolobeta, angolobeta2, angolobetaT, angolobetaT2,x2V, y2V, x2E, y2E;
        Color colorV = Color.black;
        Color colorE = Color.magenta.darker();
        Color colorH = Color.blue;
        
        angolobeta2 = (-state.theta1+Math.PI)+23.0*Math.PI/180.0; 
        angolobeta = (-state.theta1+Math.PI)-23.0*Math.PI/180.0;  
        angolobetaT2 = (-state.theta2+Math.PI)+23.0*Math.PI/180.0; 
        angolobetaT = (-state.theta2+Math.PI)-23.0*Math.PI/180.0;  
        
        angolo2 = (state.theta1+Math.PI)+23.0*Math.PI/180.0; 
        angolo = (state.theta1+Math.PI)-23.0*Math.PI/180.0;  
        
        radiusV = radius * 0.4;
        fieldL = 1.1*(radius * 0.4);
        thicknessV = state.s3;
        thicknessV2 = state.s2;
        thicknessV3 = 0;
        radiusH = state.s8;
        
        
        // center coordinates
          x1 = (double)(getSize().width/2);
          y1 = (double)(getSize().height/2);
        
        // coordinates of beginning of incident line
          x2 = (double)(x1+(int)(radius*Math.cos(Math.PI - state.theta1)));
          y2 = (double)(y1+(int)(radius*Math.sin(Math.PI - state.theta1)));
          
          x2_power = (double)(x1-(int)(radius_powernew*Math.cos(state.theta1)));
          y2_power = (double)(y1+(int)(radius_powernew*Math.sin(state.theta1)));
          
          double xo, yo, ango, ango2, ango3, rado;
          xo = x1 - (double)state.s15;
          yo = y1 - (double)state.s50;
          //rado = Math.sqrt(15.0*15.0 + 50.0*50.0)+5;
          // CHECK THIS LATER AGAIN
          rado = state.sfactor*(Math.sqrt(15.0*15.0 + 50.0*50.0)+5);
          ango = Math.PI + state.theta1*0.5;
          ango3 = Math.PI - state.theta1*0.5;
          ango2 = state.theta2*0.5;
          
          // Represent angles
          // incident wave
          MaestroG.FillArcTransp(g,x1-(double)state.s40,y1-(double)state.s40,(double)state.s80,180.0,180.0/Math.PI*state.theta1, Color.orange, 0.2);
          
          if(180.0/Math.PI*state.theta1 < 0.99 || 180.0/Math.PI*state.theta1 > 1.01 ){// to remove a little numerical glitch at 1.0 degree
                  MaestroG.drawArcThick(g, x1-(double)state.s40, y1-(double)state.s40, (double)state.s80, 180.0, 180.0/Math.PI*state.theta1, state.s3, Color.orange.darker());
              }
          if(180.0/Math.PI*state.theta1 >= 20.0){
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter("\u03b8","i","",g,state.font15,(int)(x1+rado*Math.cos(ango))-state.s5, (int)(y1+state.s2-rado*Math.sin(ango)));
          }
          else{
              //MaestroG.drawArcThick(g, x1-40.0, y1-40.0, 80.0, 180.0, 180.0/Math.PI*state.theta1, 3, Color.orange.darker());
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter("\u03b8","i","",g,state.font15,(int)x1-(int)(50.0*Math.cos(state.theta1+Math.PI/7.0)),
                                                            (int)y1+(int)(50.0*Math.sin(state.theta1+Math.PI/7.0)));
          }
          if(state.theta1_deg < (state.total_angle+0.00005) && state.theta1_deg > (state.total_angle-0.00005) && state.isPolarizationParallel){
                g.setColor(Color.red.darker());
              
                if(state.wavelength1 == state.wavelength2){
                }
                else{
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    MaestroG.subscripter("\u03b8","i"," = Brewster Angle",g,state.font15,state.s300+state.s30,state.s20);
                    MaestroG.subscripter("(Total Transmission)","","",g,state.font14,state.s300+state.s30,state.s40);
                }
          }
          
          else
          {
            if(state.wavelength1 == state.wavelength2){
            }
            else{
                    // reflected wave
                      MaestroG.FillArcTransp(g,x1-(double)state.s40,y1-(double)state.s40,(double)state.s80,180.0,-180.0/Math.PI*state.theta1, Color.cyan, 0.1);
                      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                      if(180.0/Math.PI*state.theta1 >= 20.0){
                          MaestroG.drawArcThick(g, x1-(double)state.s40, y1-(double)state.s40, (double)state.s80, 180.0, -180.0/Math.PI*state.theta1, state.s3, Color.cyan.darker());
                          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                          MaestroG.subscripter("\u03b8","r","",g,state.font15,(int)(x1+rado*Math.cos(ango3))-state.s5, (int)(y1+state.s6-rado*Math.sin(ango3)));
                      }
                      else{
                          MaestroG.drawArcThick(g, x1-(double)state.s40, y1-(double)state.s40, (double)state.s80, 180.0, -180.0/Math.PI*state.theta1, state.s3, Color.cyan.darker());
                          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                          MaestroG.subscripter("\u03b8","r","",g,state.font15,(int)x1-(int)(50.0*Math.cos(state.theta1+Math.PI/7.0)),
                                                        (int)y1+state.s5-(int)(50.0*Math.sin(state.theta1+Math.PI/7.0)));
                      }
            }
          }
          
          if(state.theta1_deg < 90.0){
              if(state.Reflection_Coef.Imaginary() == 0.0 ){
                  // transmitted wave
                  MaestroG.FillArcTransp(g,x1-(double)state.s40,y1-(double)state.s40,(double)state.s80,0.0,180.0/Math.PI*state.theta2, Color.yellow, 0.5);
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  if(180.0/Math.PI*state.theta2 >= 20.0){
                      MaestroG.drawArcThick(g, x1-(double)state.s40, y1-(double)state.s40, (double)state.s80, 0.0, 180.0/Math.PI*state.theta2, state.s3, Color.yellow); 
                      g.setColor(Color.black);
                      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                      MaestroG.subscripter("\u03b8","t","",g,state.font15,(int)(x1+rado*Math.cos(ango2))-state.s10, (int)(y1+state.s5-rado*Math.sin(ango2)));
                  }
                  else if(180.0/Math.PI*state.theta2 < 20.0 && state.Reflection_Coef.Imaginary() == 0.0){
                      MaestroG.drawArcThick(g, x1-(double)state.s40, y1-(double)state.s40, (double)state.s80, 0.0, 180.0/Math.PI*state.theta2, state.s3, Color.yellow);
                      g.setColor(Color.black);
                      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                      MaestroG.subscripter("\u03b8","t","",g,state.font15,(int)x1-state.s10+(int)(50.0*Math.cos(state.theta2+Math.PI/7.0)),
                                                                (int)y1+state.s5-(int)(50.0*Math.sin(state.theta2+Math.PI/7.0)));
                  }
              }

              else{ // Total Reflection
                  g.setColor(Color.red.darker());
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  MaestroG.subscripter("\u03b8","i"," > Critical Angle",g,state.font14,state.s300+state.s30,state.s20);
                  MaestroG.subscripter("(Total Reflection)","","",g,state.font14,state.s300+state.s30,state.s40);

                  MaestroG.FillArcTransp(g,x1-(double)state.s40, y1-(double)state.s40, (double)state.s80, 0.0, 90.0, Color.yellow, 0.5);
                  MaestroG.drawArcThick(g, x1-(double)state.s40, y1-(double)state.s40, (double)state.s80, 0.0, 90.0, state.s3, Color.yellow); 
                  g.setColor(Color.black);
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  MaestroG.subscripter("90","","\u00ba",g,state.font14,(int)(x1+rado*Math.sin(Math.PI*0.25))-state.s5, (int)(y1-rado*Math.cos(Math.PI*0.25)));
              
                  if(state.isPolarizationParallel){
                      g.setColor(Color.magenta.darker());
                  }
                  else{
                      g.setColor(Color.blue.darker());
                  }
                  MaestroG.subscripter("Transmitted","","",g,state.font11,state.s400+state.s90,state.s40);
                  MaestroG.subscripter("field details","","",g,state.font11,state.s400+state.s90,state.s55);
                  
                  MaestroG.subscripter("Reflected","","",g,state.font11,state.s20,state.s40);
                  MaestroG.subscripter("field details","","",g,state.font11,state.s20,state.s55);
                  
                  g.setColor(Color.black);
                  MaestroG.subscripter("Reflection  and  transmission coefficients","","",g,state.font12,state.s300+state.s30,state.s400+state.s10);
                  MaestroG.subscripter("are complex.   To review the fields, select:","","",g,state.font12,state.s300+state.s30,state.s400+state.s30);
                  g.setColor(Color.blue.darker());
                  MaestroG.subscripter("     Output \u21aa Field Phasors at (0,0,0)","","",g,state.font12,state.s300+state.s30,state.s400+state.s50);
              }
          }
          
          // coordinates of incident vector tip
          x2V = (double)(x1+(int)(radiusV*Math.cos(Math.PI - state.theta1)));
          y2V = (double)(y1+(int)(radiusV*Math.sin(Math.PI - state.theta1)));
          
          drawAxis(g);
          
          //vectors
          //---------------------------------------------------------------
          // incident beta vector
            // arrow tip
          double Len2 = (double)state.s15;
          MaestroG.drawLineThick(g,x2V,y2V,x2V+Len2*Math.cos(angolobeta),y2V+Len2*Math.sin(angolobeta),thicknessV,colorV);
          MaestroG.drawLineThick(g,x2V,y2V,x2V+Len2*Math.cos(angolobeta2),y2V+Len2*Math.sin(angolobeta2),thicknessV,colorV);
            // stem
          MaestroG.drawLineThick(g,x1,y1,x2,y2,state.s1,Color.gray);
          MaestroG.drawLineThick(g,x2,y2,x2V,y2V,thicknessV,colorV);
          //MaestroG.subscripter("\u03b2","i","",g,18,(int)(x2+x2V)/2-20,(int)(y2+y2V)/2-15);
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
          //MaestroG.subscripterBHat("k","i","",g,18,(int)(x2+x2V)/2-20,(int)(y2+y2V)/2-15); // Ulaby's book notation
          MaestroG.subscripterB("k","i","",g,state.font18,(int)(x2+x2V)/2-state.s20,(int)(y2+y2V)/2-state.s15); // Ulaby's book notation
          
          // LEAVE LINES BELOW THIN
          // z-component
          MaestroG.drawLineThick(g,x2,y2,x2V,y2,1,colorV);
          MaestroG.drawArrow((int)x2V-8,(int)(y2),7,g);
          // x-component
          MaestroG.drawLineThick(g,x2-state.s1,y2,x2-state.s1,y2V,1,colorV);
          MaestroG.drawArrow((int)(x2-state.s1),(int)y2V+8,5,g);
          //---------------------------------------------------------------
          
          if(IsFocusOn){
                if(state.theta1 > Math.PI/60.0){
                    MaestroG.drawLineThick(g, x1, y1, x2_power, y2_power, state.s6, incident);
                }
                else{
                    MaestroG.drawLineThick(g, x1, y1+state.s2, x2_power, y2_power+state.s2, state.s6, incident);
                }    
          }
          
        {
	    drawRefTrans2(g, incident, thickness);  
	}
	
                
    	if(state.isPolarizationParallel){ // Parallel Polarization - Incident Wave
                x2 = (double)(x1+(int)(radius*Math.cos(Math.PI - state.theta1)));
                y2 = (double)(y1+(int)(radius*Math.sin(Math.PI - state.theta1)));
                
                 if(Field_Out){
                    x2E = x1+(radius*Math.cos(Math.PI - state.theta1) - fieldL * Math.sin(state.theta1));
                    y2E = y1+(radius*Math.sin(Math.PI - state.theta1) - fieldL * Math.cos(state.theta1));

                    // E field vector
                    MaestroG.drawLineThick(g,x2, y2, x2E, y2E,thicknessV, colorE);

                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo+(Math.PI/2.0)),y2E-Len*Math.sin(angolo+(Math.PI/2.0)),thicknessV,colorE);
                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo2+(Math.PI/2.0)),y2E-Len*Math.sin(angolo2+(Math.PI/2.0)),thicknessV,colorE);
                    
                }
                else{
                    x2E = x1+(radius*Math.cos(Math.PI - state.theta1) + fieldL * Math.sin(state.theta1));
                    y2E = y1+(radius*Math.sin(Math.PI - state.theta1) + fieldL * Math.cos(state.theta1));

                    // E field vector
                    MaestroG.drawLineThick(g,x2,y2, x2E,y2E,thicknessV, colorE);

                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo+(3.0*Math.PI/2.0)),y2E-Len*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV,colorE);
                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2E-Len*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV,colorE);
                    
                }
                //An oval for magnetic field
                if(Field_Out){
                        MaestroG.fillCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),
                                    y1+(radius*Math.sin(Math.PI - state.theta1)),(double)radiusH+state.s1,state.s2,colorH);
                        MaestroG.fillCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),
                                    y1+(radius*Math.sin(Math.PI - state.theta1)),(double)(radiusH)-state.s1, state.s2, Color.white);
                        MaestroG.fillCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),
                                    y1+(radius*Math.sin(Math.PI - state.theta1)),(double)state.s3,state.s4,colorH);
                        
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup2("H","//","i","",g,state.font16,
                                                    (int)(x1 + (radius*Math.cos(Math.PI - state.theta1))-lab_pos), 
                                                    (int)(y1 + (radius*Math.sin(Math.PI - state.theta1)))+lab_pos);
                                                     
                        
                        //g.setColor(colorE);                   
                        //MaestroG.subscripterBsubsup2("E","//","i","",g,16,(int)(x2E)-25,(int)(y2E+25));
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","i","",g,state.font16,
                                                    (int)(x2E-Math.sin(Math.PI*0.5-state.theta1)*lab_pos),
                                                    (int)(y2E+Math.cos(Math.PI*0.5-state.theta1)*lab_pos));
                }
                else{
                        MaestroG.fillCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),
                                    y1+(radius*Math.sin(Math.PI - state.theta1)),(double)radiusH, state.s2, Color.white);
                
                        MaestroG.drawLineThick(g, x1+(radius*Math.cos(Math.PI - state.theta1))-(double)(radiusH/2),
                                    y1+(radius*Math.sin(Math.PI - state.theta1))-(double)(radiusH/2),
                                    x1+(radius*Math.cos(Math.PI - state.theta1)) + (double)(radiusH/2),
                                    y1+(radius*Math.sin(Math.PI - state.theta1)) + (double)(radiusH/2), state.s2,colorH);
                        MaestroG.drawLineThick(g, x1+(radius*Math.cos(Math.PI - state.theta1))+(double)(radiusH/2),
                                    y1+(radius*Math.sin(Math.PI - state.theta1))-(double)(radiusH/2),
                                    x1+(radius*Math.cos(Math.PI - state.theta1)) - (double)(radiusH/2),
                                    y1+(radius*Math.sin(Math.PI - state.theta1)) + (double)(radiusH/2), state.s2,colorH);
                        MaestroG.drawCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),
                                    y1+(radius*Math.sin(Math.PI - state.theta1)),(double)radiusH,state.s2,colorH);
                
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup2("H","//","i","",g,state.font16,
                                                    (int)(x1 + (radius*Math.cos(Math.PI - state.theta1))-lab_pos), 
                                                    (int)(y1 + (radius*Math.sin(Math.PI - state.theta1)))+lab_pos);
                        
                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","i","",g,state.font16,
                                                    (int)(x2E-Math.sin(Math.PI*0.5-state.theta1)*lab_pos),
                                                    (int)(y2E+Math.cos(Math.PI*0.5-state.theta1)*lab_pos));
             }
        }
	else{ // Perpendicular Polarization - Incident Wave
                x2 = (double)(x1+(int)(radius*Math.cos(Math.PI - state.theta1)));
                y2 = (double)(y1+(int)(radius*Math.sin(Math.PI - state.theta1)));
                
                if(Field_Out){
                    g.setColor(Color.blue);
                    x2E = x1+(radius*Math.cos(Math.PI - state.theta1) + fieldL * Math.sin(state.theta1)); 
                    y2E = y1+(radius*Math.sin(Math.PI - state.theta1) + fieldL * Math.cos(state.theta1));

                    // H field vector
                    MaestroG.drawLineThick(g,x2,y2,x2E,y2E,thicknessV, colorH);

                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo+(3.0*Math.PI/2.0)),y2E-Len*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV,colorH);
                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2E-Len*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV,colorH);
                }
                else{
                    x2E = x1+(radius*Math.cos(Math.PI - state.theta1) - fieldL * Math.sin(state.theta1));
                    y2E = y1+(radius*Math.sin(Math.PI - state.theta1) - fieldL * Math.cos(state.theta1));
                    
                    // H field vector
                    MaestroG.drawLineThick(g,x2,y2,x2E, y2E, thicknessV, colorH);

                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo+(Math.PI/2.0)),y2E-Len*Math.sin(angolo+(Math.PI/2.0)),thicknessV,colorH);
                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo2+(Math.PI/2.0)),y2E-Len*Math.sin(angolo2+(Math.PI/2.0)),thicknessV,colorH);
                }
                //An oval for electric field
                if(Field_Out){
                        MaestroG.fillCircleThick(g, x2, y2,(double)radiusH+state.s1,state.s2,colorE);
                        MaestroG.fillCircleThick(g, x2,y2,(double)(radiusH-state.s1), state.s2, Color.white);            
                        MaestroG.fillCircleThick(g, x2,y2,(double)state.s3,state.s4,colorE);
                        
                        g.setColor(colorE);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        MaestroG.subscripterBsubsup("E","\u22a5","i","",g,state.font16,
                                                    (int)(x1 + (radius*Math.cos(Math.PI - state.theta1)))-lab_pos, 
                                                    (int)(y1 + (radius*Math.sin(Math.PI - state.theta1)))+lab_pos);
                                                     
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup("H","\u22a5","i","",g,state.font16,
                                                    (int)(x2E-Math.sin(Math.PI*0.5-state.theta1)*lab_pos),
                                                    (int)(y2E+Math.cos(Math.PI*0.5-state.theta1)*lab_pos));
                }
                else{
                        MaestroG.fillCircleThick(g, x2,y2,(double)radiusH, state.s2, Color.white);
                                    
                        MaestroG.drawLineThick(g, x2-(double)(radiusH/2),
                                    y2-(double)(radiusH/2),x2 + (double)(radiusH/2),
                                    y2 + (double)(radiusH/2), state.s2,colorE);
                        MaestroG.drawLineThick(g, x2+(double)(radiusH/2),
                                    y2-(double)(radiusH/2),x2 - (double)(radiusH/2),
                                    y2 + (double)(radiusH/2), state.s2,colorE);
                        MaestroG.drawCircleThick(g, x2, y2,(double)radiusH,state.s2,colorE);
                
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(colorE);
                        MaestroG.subscripterBsubsup("E","\u22a5","i","",g,state.font16,
                                                    (int)(x1 + (radius*Math.cos(Math.PI - state.theta1))-lab_pos), 
                                                    (int)(y1 + (radius*Math.sin(Math.PI - state.theta1)))+lab_pos);
                        
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup("H","\u22a5","i","",g,state.font16,
                                                    (int)(x2E-Math.sin(Math.PI*0.5-state.theta1)*lab_pos),
                                                    (int)(y2E+Math.cos(Math.PI*0.5-state.theta1)*lab_pos));
                        
                }
        }
    }
    
    //-------------------------------------------------------------------------------------------
    
    public void paint(Graphics g){
	    if(im == null){
		im = createImage(getSize().width,getSize().height);
		buf = im.getGraphics();
		drawCanvas(buf);
	    }
	    else{
		drawCanvas(buf);
	    }
	    g.drawImage(im,0,0,null);
    }
    
    //------------------------------------------------------------------------------------------------
    
        private void drawRefTrans2(Graphics g, Color colore, int thickness){
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        int lab_pos = state.s28;
        double x1, y1, x2, y2, x2R, y2R, x2T, y2T;
        double angolobetaT, angolobetaT2;
        angolobetaT2 = (-state.theta2+Math.PI)+23.0*Math.PI/180.0; // THIS IS GOOD!!!
        angolobetaT = (-state.theta2+Math.PI)-23.0*Math.PI/180.0;  //     "   "
        
        radius = getSize().height/2-TopMargin-scale_radius*VGap/20;
        //radius_powernew = radius*40/100;
        radius_powernew = 93*radius/233;
        
        double radius_powernew2;
        radius_powernew2 = radius_powernew + state.s13;
        
	g.setColor(incident);
	double Len = (double)state.s15;
        double LenT = (double)state.s15;
        //double LenR = 15.0 * Math.pow(Math.sin(state.Reflection_Coef.Magnitude()*Math.PI/2.0),0.5);
        double LenR = Len * Math.pow(Math.sin(state.Reflection_Coef.Magnitude()*Math.PI/2.0),0.5);
        if(state.Transmission_Coef.Magnitude()>1.0){
            LenT = (double)state.s15;
        }
        else{
            //LenT = 15.0 * Math.pow(Math.sin(state.Transmission_Coef.Magnitude()*Math.PI/2.0),0.5);
            LenT = Len * Math.pow(Math.sin(state.Transmission_Coef.Magnitude()*Math.PI/2.0),0.5);
        }
        double LenTH = (double)state.s15;
        if(state.Transmission_CoefH.Magnitude()>1.0){
            LenTH = (double)state.s15;
        }
        else{
            //LenTH = 15.0 * Math.pow(Math.sin(state.Transmission_CoefH.Magnitude()*Math.PI/2.0),0.5);
            LenTH = Len * Math.pow(Math.sin(state.Transmission_CoefH.Magnitude()*Math.PI/2.0),0.5);
        }
        
        double radiusV, fieldL, x2_power, y2_power;
        int thicknessV, thicknessV2, thicknessV3, radiusH;
        double angolo, angolo2, angoloT, angoloT2, x2V, y2V, x2E, y2E;
        Color colorV = Color.black;
        Color colorE = Color.magenta.darker();
        Color colorH = Color.blue;
        
        angolo2 = (-state.theta1+Math.PI)+23.0*Math.PI/180.0;
        angolo = (-state.theta1+Math.PI)-23.0*Math.PI/180.0;
        
        angoloT2 = (state.theta2+Math.PI*0.5)+23.0*Math.PI/180.0;
        angoloT = (state.theta2+Math.PI*0.5)-23.0*Math.PI/180.0;
        
        radiusV = radius * 0.6;
        fieldL = radius * 0.4;
        thicknessV = state.s3;
        thicknessV2 = state.s2;
        thicknessV3 = 0;
        radiusH = state.s8;
        
        // Find Direction of normal field for the Reflected Component
        if(state.isPolarizationParallel){// Parallel Polarization
          
                if(Field_Out){
                    if(state.Reflection_Coef.Real() < 0.0){
                        Field_Refl_Out = true;
                    }
                    else{
                        Field_Refl_Out = false;
                    }
                }
                else{
                    if(state.Reflection_Coef.Real() < 0.0){
                        Field_Refl_Out = false;
                    }
                    else{
                        Field_Refl_Out = true;
                    }
                }
        }
        else{// Perpendicular Polarization
            
                if(Field_Out){
                    if(state.Reflection_Coef.Real() > 0.0){
                        Field_Refl_Out = true;
                    }
                    else{
                        Field_Refl_Out = false;
                    }
                }
                else{
                    if(state.Reflection_Coef.Real() < 0.0){
                        Field_Refl_Out = true;
                    }
                    else{
                        Field_Refl_Out = false;
                    }
                } 
        }
	  //reflected ray
	    
          //center coordinates
          x1 = (double)(getSize().width/2);
          y1 = (double)(getSize().height/2);
          
          // coordinates of beta vector root
          x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(Math.PI-state.theta1)));
          y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI-state.theta1)));
          
          // coordinates of beta vector tip
          x2R = (double)(getSize().width/2 +(int)((radius)*Math.cos(Math.PI-state.theta1)));
          y2R = (double)(getSize().height/2-(int)((radius)*Math.sin(Math.PI-state.theta1)));
          
          double vscaled;
          vscaled = state.beta_ratio*((radius+10.0) - radius_powernew2);
          
          if(state.theta1_deg < (state.total_angle+0.00005) && state.theta1_deg > (state.total_angle-0.00005) && state.isPolarizationParallel){// don't draw if not total transmission
          }
          else if(state.wavelength1 == state.wavelength2){
          }
          else{
              // beta - reflected ray
              // Arrow head of vector, thick
              MaestroG.drawLineThick(g,x2R,y2R,x2R-Len*Math.cos(angolo),y2R+Len*Math.sin(angolo),thicknessV,colorV);
              MaestroG.drawLineThick(g,x2R,y2R,x2R-Len*Math.cos(angolo2),y2R+Len*Math.sin(angolo2),thicknessV,colorV);
                // stem
              MaestroG.drawLineThick(g,x1,y1,x2R,y2R,state.s1,Color.gray); // thin line to center
              MaestroG.drawLineThick(g,x2,y2,x2R,y2R,thicknessV,colorV);// vector line, thick
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripterB("k","r","",g,state.font18,(int)(x2+x2R)/2 + state.s15,(int)(y2+y2R)/2 - state.s15); // Ulaby's book notation
              // z-component
              MaestroG.drawLineThick(g,x2,y2,x2R,y2,1,colorV); // keep it thin
              MaestroG.drawArrow((int)x2R + 8,(int)y2,8,g);
              // x-component
              MaestroG.drawLineThick(g,x2,y2,x2,y2R,1,colorV); // keep it thin
              MaestroG.drawArrow((int)x2,(int)y2R + 8,5,g);
              
          //-------------------------------------------
          }  
	  //transmitted ray
          if(state.theta1_deg < 90.0){
              if(state.Reflection_Coef.Imaginary() == 0.0){  // No or Before Total Reflection

                x1 = (double)(getSize().width/2);
                y1 = (double)(getSize().height/2);
                x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(state.theta2)));
                y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(state.theta2)));
                x2T = x1+((radius_powernew2+vscaled)*Math.cos(state.theta2));
                y2T = y1-((radius_powernew2+vscaled)*Math.sin(state.theta2));

                // beta - transmitted ray
                // Arrow head of vector, thick
                MaestroG.drawLineThick(g,x2T,y2T,x2T+Len*Math.cos(angolobetaT),y2T+Len*Math.sin(angolobetaT),thicknessV,colorV);
                MaestroG.drawLineThick(g,x2T,y2T,x2T+Len*Math.cos(angolobetaT2),y2T+Len*Math.sin(angolobetaT2),thicknessV,colorV);
                // stem
                MaestroG.drawLineThick(g,x1,y1,x2,y2,state.s1,Color.gray); // thin line to center
                MaestroG.drawLineThick(g,x2,y2,x2T,y2T,thicknessV,colorV); // vector line, thick
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                MaestroG.subscripterB("k","t","",g,state.font18,(int)(x2+x2T)/2-state.s25,(int)(y2+y2T)/2-state.s15); // Ulaby's book notation
                // z-component
                MaestroG.drawLineThick(g,x2,y2,x2T,y2,1,colorV);
                MaestroG.drawArrow((int)x2T-9,(int)(y2),7,g);
                // x-component
                MaestroG.drawLineThick(g,x2,y2,x2,y2T,1,colorV);
                MaestroG.drawArrow((int)x2,(int)y2T+10,5,g);

              }
              else{ // Total Reflection - transmitted wave vector

                  if(state.theta1_deg < 90.0){
                      angoloT2 = 23.0*Math.PI/180.0;
                      angoloT = -23.0*Math.PI/180.0;

                      x1 = (double)(getSize().width/2); //
                      y1 = (double)(getSize().height/2); //
                      x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(Math.PI*0.5)));//
                      y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI*0.5)));//
                      x2T = (double)(getSize().width/2 +(int)((radius)*Math.cos(Math.PI*0.5)));
                      y2T = (double)(getSize().height/2-(int)((radius)*Math.sin(Math.PI*0.5)));
                      x2R = (double)(getSize().width/2 +(int)((radius)*Math.cos(Math.PI - state.theta1)));
                      y2R = (double)(getSize().height/2-(int)((radius)*Math.sin(Math.PI - state.theta1)));

                      double yshifto;
                      yshifto = radius_powernew-radius_powernew*Math.sin(Math.PI - state.theta1);

                      //arrow head for wave vector
                      MaestroG.drawLineThick(g,x1,y2R-yshifto,x1-Len*Math.sin(angoloT),y2R-yshifto+Len*Math.cos(angoloT),thicknessV,colorV);
                      MaestroG.drawLineThick(g,x1,y2R-yshifto,x1-Len*Math.sin(angoloT2),y2R-yshifto+Len*Math.cos(angoloT2),thicknessV,colorV);
                      // stem
                      MaestroG.drawLineThick(g,x1,y2,x1,y2R-yshifto,thicknessV,colorV);

                      //MaestroG.subscripter("\u03b2","t","",g,18,(int)(x2+x2T)/2-18,(int)(y2+y2T)/2+18);
                      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                      //MaestroG.subscripterBHat("k","t","",g,18,(int)(x2+x2T)/2-22,(int)(y2+y2T)/2+18); // Ulaby's book notation        
                      MaestroG.subscripterB("k","t","",g,state.font18,(int)(x2+x2T)/2-state.s22,(int)(y2+y2T)/2+state.s18); // Ulaby's book notation        
                      g.setColor(Color.red.darker());
                      MaestroG.subscripterB("surface wave","","",g,state.s12,(int)(x2)+state.s30,state.s80);

                  }
              }
          }
          
          //--------------------------------------------------------------------
          if(!IsFocusOn){drawY(g);}
          //--------------------------------------------------------------------
          
          
          //------------------------------------------------------------------------------------
          x2E = x1+(radius_powernew*Math.sin(state.theta1) - fieldL * Math.cos(state.theta1));
          y2E = y1-(radius_powernew*Math.cos(state.theta1) + fieldL * Math.sin(state.theta1));
                
          if(state.isPolarizationParallel){ // Parallel Polarization
                double x2EE, y2EE;
              
                if(state.Reflection_Coef.Imaginary() == 0.0){
                    if(state.theta1_deg < 90.0){
                    // ------------------------ Transmitted Wave
                    if(Field_Out){
                        x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(state.theta2)));
                        y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(state.theta2)));
                        x2T = (double)(getSize().width/2 +(int)((radius+state.s10)*Math.cos(state.theta2)));
                        y2T = (double)(getSize().height/2-(int)((radius+state.s10)*Math.sin(state.theta2))-state.s1);

                        x2EE = x2 - fieldL*(state.Transmission_Coef.Magnitude()+extra) * Math.sin(state.theta2);
                        y2EE = y2 - fieldL*(state.Transmission_Coef.Magnitude()+extra) * Math.cos(state.theta2);

                        // E field vector
                        MaestroG.drawLineThick(g,x2,y2,x2EE,y2EE,thicknessV, colorE);

                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT*Math.cos(angoloT),y2EE+LenT*Math.sin(angoloT),thicknessV,colorE);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT*Math.cos(angoloT2),y2EE+LenT*Math.sin(angoloT2),thicknessV,colorE);
                        
                        //An oval for magnetic field
                        MaestroG.fillCircleThick(g, x2, y2, (double)radiusH+state.s1,state.s2,colorH);
                        MaestroG.fillCircleThick(g, x2, y2, (double)(radiusH-state.s1), state.s2, Color.white);
                        MaestroG.fillCircleThick(g, x2, y2, (double)state.s3,state.s4,colorH);
                        
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup2("H","//","t","",g,state.font16,(int)x2+lab_pos/2, (int)y2+lab_pos);

                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","t","",g,state.font16,
                                                    (int)(x2EE-Math.sin(Math.PI/2-state.theta2)*lab_pos),
                                                    (int)(y2EE+Math.cos(Math.PI/2-state.theta2)*lab_pos));
                    }
                    else{
                        x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(state.theta2)));
                        y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(state.theta2)));
                        x2T = (double)(getSize().width/2 +(int)((radius+state.s10)*Math.cos(state.theta2)));
                        y2T = (double)(getSize().height/2-(int)((radius+state.s10)*Math.sin(state.theta2))-state.s1);

                        x2EE = x2 + fieldL*(state.Transmission_Coef.Magnitude()+extra) * Math.sin(state.theta2);
                        y2EE = y2 + fieldL*(state.Transmission_Coef.Magnitude()+extra) * Math.cos(state.theta2);
                        
                        // E field vector
                        MaestroG.drawLineThick(g,x2,y2,x2EE,y2EE,thicknessV, colorE);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT*Math.cos(angoloT),
                                                 y2EE-LenT*Math.sin(angoloT),thicknessV,colorE);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT*Math.cos(angoloT2),
                                                 y2EE-LenT*Math.sin(angoloT2),thicknessV,colorE);
                        
                        //An oval for magnetic field

                        MaestroG.fillCircleThick(g, x2, y2, (double)radiusH, state.s2, Color.white);

                        MaestroG.drawLineThick(g, x2-(double)(radiusH/2), y2 -(double)(radiusH/2),
                                        x2 + (double)(radiusH/2), y2 + (double)(radiusH/2), state.s2,colorH);
                        MaestroG.drawLineThick(g, x2 +(double)(radiusH/2), y2 -(double)(radiusH/2),
                                        x2 - (double)(radiusH/2), y2 + (double)(radiusH/2), state.s2,colorH);

                        MaestroG.drawCircleThick(g, x2, y2, (double)radiusH,state.s2,colorH);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup2("H","//","t","",g,state.font16,(int)x2-lab_pos,(int)y2-lab_pos/2);

                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","t","",g,state.font16,
                                                    (int)(x2EE+Math.sin(Math.PI*0.5-state.theta2)*lab_pos/2),
                                                    (int)(y2EE+Math.cos(Math.PI*0.5-state.theta2)*lab_pos));

                    }
                    }
                }
                else{ // Total Reflection
                    // ------------------------ Transmitted Wave
                    if(state.theta1_deg < 90.0){
                        if(ShowTR){//  Legend
                            MaestroG.drawLineThickB(g,state.s400+state.s90,state.s70,state.s500+state.s30,state.s70, thicknessV2, state.s3, state.s3, Color.magenta.darker());
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup2("Re(E","//","t",")",g,state.font14,state.s500+state.s35,state.s75);
                            MaestroG.drawLineThickB(g,state.s400+state.s90,state.s95,state.s500+state.s30,state.s95, thicknessV2, state.s3, state.s3, Color.red);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup2("Im(E","//","t",")",g,state.font14,state.s500+state.s35,state.s100);
                        }
                        
                        if(Field_Out){
                            double angolonew;
                            Complex testfieldy, testfieldx;
                            
                            x2 = (double)(getSize().width/2);
                            y2 = (double)(getSize().height/2-(int)(radius_powernew));
                            x2T = (double)(getSize().width/2 +(int)((radius)*Math.cos(Math.PI*0.5)));
                            y2T = (double)(getSize().height/2-(int)((radius)*Math.sin(Math.PI*0.5)));
                            
                            testfieldx = Complex.Multiply(Math.cos(state.theta1),Complex.Add(1.0,state.Reflection_Coef));
                            //testfieldy = Complex.Multiply(state.epsilon_r1/state.epsilon_r2,state.Transmission_Coef);
                            testfieldy = Complex.Multiply(1.0,state.Transmission_Coef);

                            //--------------------------------------REAL
                            // Display real component of transmitted vector
                            y2EE = y2 - fieldL * testfieldx.Real();
                            x2EE = x1 - fieldL * testfieldy.Real();
                            angolonew = Math.PI + Math.atan(fieldL*(-testfieldy.Real())/(fieldL*testfieldx.Real()));   

                            // E field vector
                            if(ShowTR){
                                MaestroG.drawLineThickB(g,x1,y2,x2EE,y2EE, thicknessV2, state.s3, state.s3, colorE);
                                //vector arrow head
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT*Math.sin(angoloT+angolonew),y2EE-LenT*Math.cos(angoloT+angolonew),thicknessV2,colorE);
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT*Math.sin(angoloT2+angolonew),y2EE-LenT*Math.cos(angoloT2+angolonew),thicknessV2,colorE);
                            }
                            //--------------------------------------IMAGINARY
                            // Display imaginary component of transmitted vector
                            y2EE = y2 - fieldL * testfieldx.Imaginary();
                            x2EE = x1 - fieldL * testfieldy.Imaginary();
                            
                            double LenT_im; 
                            if(Math.abs(state.Transmission_Coef.Imaginary()) > 1.0){
                                LenT_im = (double)state.s15;
                            }
                            else{
                                LenT_im = ((double)state.s15) * Math.pow(Math.sin(Math.abs(state.Transmission_Coef.Imaginary())*Math.PI/2.0),0.5);
                            }
                            
                            angolonew = Math.atan(fieldL*(-testfieldy.Imaginary())/(fieldL*testfieldx.Imaginary()));   

                            // E field vector
                            if(ShowTR){
                                MaestroG.drawLineThickB(g,x1,y2,x2EE,y2EE, thicknessV2, state.s3, state.s3, Color.red);
                                //vector arrow head
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT_im*Math.sin(angoloT+angolonew),y2EE-LenT_im*Math.cos(angoloT+angolonew),thicknessV2,Color.red);
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT_im*Math.sin(angoloT2+angolonew),y2EE-LenT_im*Math.cos(angoloT2+angolonew),thicknessV2,Color.red);
                            }
                            //--------------------------------------------------
                            //An oval for magnetic field
                            MaestroG.fillCircleThick(g, x1, y2,(double)radiusH+state.s1,2,colorH);
                            MaestroG.fillCircleThick(g, x1,y2,(double)(radiusH-state.s1), state.s2, Color.white);
                            MaestroG.fillCircleThick(g, x1, y2,(double)state.s2,state.s4,colorH);
                            
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            g.setColor(Color.blue);                   
                            MaestroG.subscripterBsubsup2("H","//","t","",g,state.font16,(int)(x1)+state.s5, (int)(y2)+state.s25);
                        }
                        else{
                            double angolonew;
                            Complex testfieldy, testfieldx;
                            
                            x2 = (double)(getSize().width/2);
                            y2 = (double)(getSize().height/2-(int)(radius_powernew));
                            x2T = (double)(getSize().width/2 +(int)((radius)*Math.cos(Math.PI*0.5)));
                            y2T = (double)(getSize().height/2-(int)((radius)*Math.sin(Math.PI*0.5)));
                            testfieldx = Complex.Multiply(Math.cos(state.theta1),Complex.Add(1.0,state.Reflection_Coef));
                            testfieldy = Complex.Multiply(1.0,state.Transmission_Coef);
                            //----------------------------------------Real
                            y2EE = y2 + fieldL*testfieldx.Real();
                            x2EE = x1 + fieldL*(testfieldy.Real());
                            angolonew = Math.PI + Math.atan(fieldL*(-testfieldy.Real())/(fieldL*testfieldx.Real()));
                            // E field vector
                            if(ShowTR){
                                MaestroG.drawLineThickB(g,x2,y2,x2EE, y2EE,thicknessV2, state.s3, state.s3, colorE);
                                // arrow head
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT*Math.sin(angoloT+angolonew),y2EE+LenT*Math.cos(angoloT+angolonew),thicknessV2,colorE);
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT*Math.sin(angoloT2+angolonew),y2EE+LenT*Math.cos(angoloT2+angolonew),thicknessV2,colorE);
                            }
                            //----------------------------------------Imaginary
                            y2EE = y2 + fieldL*testfieldx.Imaginary();
                            x2EE = x1 + fieldL*(testfieldy.Imaginary());
                            angolonew = Math.atan(fieldL*(-testfieldy.Imaginary())/(fieldL*testfieldx.Imaginary()));
                            double LenT_im; 
                            if(Math.abs(state.Transmission_Coef.Imaginary()) > 1.0){
                                LenT_im = (double)state.s15;
                            }
                            else{
                                LenT_im = (double)state.s15 * Math.pow(Math.sin(Math.abs(state.Transmission_Coef.Imaginary())*Math.PI/2.0),0.5);
                            }
                            
                            // E field vector
                            if(ShowTR){
                                MaestroG.drawLineThickB(g,x2,y2,x2EE, y2EE,thicknessV2, state.s3, state.s3, Color.red);
                                // arrow head
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT_im*Math.sin(angoloT+angolonew),y2EE+LenT_im*Math.cos(angoloT+angolonew),thicknessV2,Color.red);
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT_im*Math.sin(angoloT2+angolonew),y2EE+LenT_im*Math.cos(angoloT2+angolonew),thicknessV2,Color.red);
                            }
                            //--------------------------------------------------
                            //An oval for magnetic field
                             MaestroG.fillCircleThick(g, x2, y2, (double)radiusH, state.s2, Color.white);
                             MaestroG.drawLineThick(g, x2-(double)(radiusH/2), y2 -(double)(radiusH/2),
                                            x2 + (double)(radiusH/2), y2 + (double)(radiusH/2), state.s2,colorH);
                             MaestroG.drawLineThick(g, x2 +(double)(radiusH/2), y2 -(double)(radiusH/2),
                                            x2 - (double)(radiusH/2), y2 + (double)(radiusH/2), state.s2,colorH);
                             MaestroG.drawCircleThick(g, x2, y2, (double)radiusH,state.s2,colorH);
                             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                             g.setColor(colorH);                   
                             MaestroG.subscripterBsubsup2("H","//","t","",g,state.font16,(int)(x1)+state.s5, (int)(y2)+state.s25);
                        }
                    }
                }
                
                if(state.theta1_deg < (state.total_angle+0.00005) && state.theta1_deg > (state.total_angle-0.00005) ){
                }
                else if(state.wavelength1 == state.wavelength2){
                }
                else
                {  // REFLECTED WAVE STARTS HERE  *************************
                if(Field_Refl_Out){ // reflected wave - Field_out
                    if(state.Reflection_Coef.Imaginary() == 0.0){
                        double x22, y22;
                        x22 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(Math.PI-state.theta1)));
                        y22 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI-state.theta1)));
                        x2EE = x22 - fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.sin(state.theta1);
                        y2EE = y22 + fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.cos(state.theta1);

                        // E field vector
                        MaestroG.drawLineThick(g, x22, y22, x2EE, y2EE, thicknessV, colorE);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE-LenR*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV,colorE);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE-LenR*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV,colorE);

                        //An oval for magnetic field
                        MaestroG.fillCircleThick(g, x22, y22, (double)radiusH+state.s1,state.s2,colorH);
                        MaestroG.fillCircleThick(g, x22, y22, (double)(radiusH-state.s1), state.s2, Color.white);
                        MaestroG.fillCircleThick(g, x22, y22, (double)state.s3,state.s4,colorH);
                        
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup2("H","//","r","",g,state.font16,(int)(x22)+lab_pos/2, (int)(y22)-lab_pos/2);
                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","r","",g,state.font16,
                                                    (int)(x2EE-Math.sin(state.theta1)*lab_pos),
                                                    (int)(y2EE+Math.cos(state.theta1)*lab_pos));                    
                    }
                    else{//Total Reflection - Field OUT Parallel Polarization    
                        if(ShowREF && state.theta1_deg < 90.0 ){// Legend
                            MaestroG.drawLineThickB(g,state.s20,state.s70,state.s60,state.s70, thicknessV2, state.s3, state.s3, Color.magenta.darker());
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup2("Re(E","//","r",")",g,state.font14,state.s65,state.s75);
                            MaestroG.drawLineThickB(g,state.s20,state.s95,state.s60,state.s95, thicknessV2, state.s3, state.s3, Color.red);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup2("Im(E","//","r",")",g,state.font14,state.s65,state.s100);
                        }
                    double x22, y22;
                    x22 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(Math.PI-state.theta1)));
                    y22 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI-state.theta1)));
                    //----------------------------------------------------- REAL  
                    double LenR_re; 
                    if(Math.abs(state.Reflection_Coef.Real()) > 1.0){
                         LenR_re = (double)state.s15;
                    }
                    else{
                         LenR_re = ((double)state.s15) * Math.pow(Math.sin(Math.abs(state.Reflection_Coef.Real())*Math.PI/2.0),0.5);
                    }
                    if(state.Reflection_Coef.Real() >= 0.0){
                        x2EE = x22 - fieldL*(state.Reflection_Coef.Real()+extra) * Math.sin(state.theta1);
                        y2EE = y22 + fieldL*(state.Reflection_Coef.Real()+extra) * Math.cos(state.theta1);
                        // E field vector
                        if(ShowREF){
                            MaestroG.drawLineThickB(g, x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, colorE);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR_re*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE-LenR_re*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV2,colorE);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR_re*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE-LenR_re*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV2,colorE);
                        }
                    }
                    else{
                        x2EE = x22 + fieldL*(state.Reflection_Coef.Real()+extra) * Math.sin(state.theta1);
                        y2EE = y22 - fieldL*(state.Reflection_Coef.Real()+extra) * Math.cos(state.theta1);
                        // E field vector
                        if(ShowREF){
                            MaestroG.drawLineThickB(g, x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, colorE);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR_re*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE-LenR_re*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV2,colorE);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR_re*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE-LenR_re*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV2,colorE);
                        }
                    }
                    //------------------------------------------------ IMAGINARY
                    double LenR_im; 
                    if(Math.abs(state.Reflection_Coef.Imaginary()) > 1.0){
                         LenR_im = (double)state.s15;
                    }
                    else{
                         LenR_im = ((double)state.s15) * Math.pow(Math.sin(Math.abs(state.Reflection_Coef.Imaginary())*Math.PI/2.0),0.5);
                    }
                    if(Field_Out){
                        x2EE = x22 + fieldL*(state.Reflection_Coef.Imaginary()+extra) * Math.sin(state.theta1);
                        y2EE = y22 - fieldL*(state.Reflection_Coef.Imaginary()+extra) * Math.cos(state.theta1);
                        // E field vector
                        if(ShowREF){
                            MaestroG.drawLineThickB(g,x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, Color.red);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_im*Math.cos(angolo+(Math.PI/2.0)),y2EE+LenR_im*Math.sin(angolo+(Math.PI/2.0)),thicknessV2,Color.red);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_im*Math.cos(angolo2+(Math.PI/2.0)),y2EE+LenR_im*Math.sin(angolo2+(Math.PI/2.0)),thicknessV2,Color.red);
                        }    
                    }
                    else{
                        x2EE = x22 - fieldL*(state.Reflection_Coef.Imaginary()+extra) * Math.sin(state.theta1);
                        y2EE = y22 + fieldL*(state.Reflection_Coef.Imaginary()+extra) * Math.cos(state.theta1);
                        // E field vector
                        if(ShowREF){
                            MaestroG.drawLineThickB(g,x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, Color.red);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_im*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE+LenR_im*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV2,Color.red);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_im*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE+LenR_im*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV2,Color.red);
                        }
                    }
                    //An oval for magnetic field
                    MaestroG.fillCircleThick(g, x22, y22, (double)radiusH+state.s1,state.s2,colorH);
                    MaestroG.fillCircleThick(g, x22, y22, (double)(radiusH-state.s1), state.s2, Color.white);
                    MaestroG.fillCircleThick(g, x22, y22, (double)state.s3,state.s4,colorH);
                    
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    g.setColor(Color.blue);                   
                    MaestroG.subscripterBsubsup2("H","//","r","",g,state.font16,(int)(x22)+lab_pos/2, (int)(y22)-lab_pos/2);
                    }
                }
                else{// reflected wave - Field_In
                    if(state.Reflection_Coef.Imaginary() == 0.0){ // no total reflection
                        double x22, y22;
                        x22 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(Math.PI-state.theta1)));
                        y22 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI-state.theta1)));
                        x2EE = x22 + fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.sin(state.theta1);
                        y2EE = y22 - fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.cos(state.theta1);
                        // E field vector
                        MaestroG.drawLineThick(g,x22, y22, x2EE, y2EE, thicknessV, colorE);
                        // arrow head
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE+LenR*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV,colorE);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE+LenR*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV,colorE);
                        //An oval for magnetic field
                        MaestroG.fillCircleThick(g, x22, y22, (double)radiusH, state.s2, Color.white);
                        MaestroG.drawLineThick(g, x22 - (double)(radiusH/2), y22-(double)(radiusH/2),
                                        x22 + (double)(radiusH/2), y22 + (double)(radiusH/2), state.s2,colorH);
                        MaestroG.drawLineThick(g, x22 +(double)(radiusH/2), y22 -(double)(radiusH/2),
                                        x22 - (double)(radiusH/2), y22 + (double)(radiusH/2), state.s2,colorH);
                        MaestroG.drawCircleThick(g, x22,y22,(double)radiusH,state.s2,colorH);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(colorH);                   
                        MaestroG.subscripterBsubsup2("H","//","r","",g,state.font16,(int)x22-lab_pos,(int)y22+lab_pos);
                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","r","",g,state.font16,
                                                    (int)(x2EE+Math.sin(state.theta1)*lab_pos/2),
                                                    (int)(y2EE-Math.cos(state.theta1)*lab_pos/2));
                    }
                    else{// Total Reflection
                        if(ShowREF && state.theta1_deg < 90.0 ){// Legend
                            MaestroG.drawLineThickB(g,state.s20,state.s70,state.s60,state.s70, thicknessV2, state.s3, state.s3, Color.magenta.darker());
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup2("Re(E","//","r",")",g,state.font14,state.s65,state.s75);
                            MaestroG.drawLineThickB(g,state.s20,state.s95,state.s60,state.s95, thicknessV2, state.s3, state.s3, Color.red);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup2("Im(E","//","r",")",g,state.font14,state.s65,state.s100);
                        }
                    double x22, y22;
                    x22 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(Math.PI-state.theta1)));
                    y22 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI-state.theta1)));
                    //----------------------------------------------------- REAL
                    double LenR_re; 
                    if(Math.abs(state.Reflection_Coef.Real()) > 1.0){
                         LenR_re = (double)state.s15;
                    }
                    else{
                         LenR_re = ((double)state.s15) * Math.pow(Math.sin(Math.abs(state.Reflection_Coef.Real())*Math.PI/2.0),0.5);
                    }
                    if(state.Reflection_Coef.Real() < 0.0){
                        x2EE = x22 - fieldL*(state.Reflection_Coef.Real()+extra) * Math.sin(state.theta1);
                        y2EE = y22 + fieldL*(state.Reflection_Coef.Real()+extra) * Math.cos(state.theta1);
                        // E field vector
                        if(ShowREF){
                            MaestroG.drawLineThickB(g,x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, colorE);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_re*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE+LenR_re*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV2,colorE);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_re*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE+LenR_re*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV2,colorE);
                        }
                    }
                    else{
                        x2EE = x22 + fieldL*(state.Reflection_Coef.Real()+extra) * Math.sin(state.theta1);
                        y2EE = y22 - fieldL*(state.Reflection_Coef.Real()+extra) * Math.cos(state.theta1);
                        // E field vector
                        if(ShowREF){
                            MaestroG.drawLineThickB(g,x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, colorE);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_re*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE+LenR_re*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV2,colorE);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_re*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE+LenR_re*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV2,colorE);
                        }
                    }
                    //------------------------------------------------ IMAGINARY
                    double LenR_im; 
                    if(Math.abs(state.Reflection_Coef.Imaginary()) > 1.0){
                         LenR_im = (double)state.s15;
                    }
                    else{
                         LenR_im = ((double)state.s15) * Math.pow(Math.sin(Math.abs(state.Reflection_Coef.Imaginary())*Math.PI/2.0),0.5);
                    }
                    if(Field_Out){
                        x2EE = x22 + fieldL*(state.Reflection_Coef.Imaginary()+extra) * Math.sin(state.theta1);
                        y2EE = y22 - fieldL*(state.Reflection_Coef.Imaginary()+extra) * Math.cos(state.theta1);
                        // E field vector
                        if(ShowREF){
                            MaestroG.drawLineThickB(g,x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, Color.red);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_im*Math.cos(angolo+(Math.PI/2.0)),y2EE+LenR_im*Math.sin(angolo+(Math.PI/2.0)),thicknessV2,Color.red);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_im*Math.cos(angolo2+(Math.PI/2.0)),y2EE+LenR_im*Math.sin(angolo2+(Math.PI/2.0)),thicknessV2,Color.red);
                        }
                    }
                    else{
                        x2EE = x22 - fieldL*(state.Reflection_Coef.Imaginary()+extra) * Math.sin(state.theta1);
                        y2EE = y22 + fieldL*(state.Reflection_Coef.Imaginary()+extra) * Math.cos(state.theta1);
                        // E field vector
                        if(ShowREF){
                            MaestroG.drawLineThickB(g,x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, Color.red);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_im*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE+LenR_im*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV2,Color.red);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_im*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE+LenR_im*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV2,Color.red);
                        }    
                    }
                    //----------------------------------------------------------
                    //An oval for magnetic field
                    MaestroG.fillCircleThick(g, x22, y22, (double)radiusH, state.s2, Color.white);
                    MaestroG.drawLineThick(g, x22 - (double)(radiusH/2), y22-(double)(radiusH/2),
                                    x22 + (double)(radiusH/2), y22 + (double)(radiusH/2), state.s2,colorH);
                    MaestroG.drawLineThick(g, x22 +(double)(radiusH/2), y22 -(double)(radiusH/2),
                                    x22 - (double)(radiusH/2), y22 + (double)(radiusH/2), state.s2,colorH);
                    MaestroG.drawCircleThick(g, x22,y22,(double)radiusH,state.s2,colorH);
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    g.setColor(colorH);                   
                    MaestroG.subscripterBsubsup2("H","//","r","",g,state.font16,(int)x22-lab_pos,(int)y22+lab_pos);
                    }
                }
            }
        }
	else{  // Perpendicular Polarization       
                double x2EE, y2EE;
                if(state.Reflection_Coef.Imaginary() == 0.0){
                //------------------ transmitted field
                  if(state.theta1_deg < 90.0){                        
                    if(Field_Out){
                        x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(state.theta2)));
                        y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(state.theta2)));
                        x2T = (double)(getSize().width/2 +(int)((radius+state.s10)*Math.cos(state.theta2)));
                        y2T = (double)(getSize().height/2-(int)((radius+state.s10)*Math.sin(state.theta2))-state.s1);
                        x2EE = x2 + fieldL*(state.Transmission_CoefH.Magnitude()+extra) * Math.sin(state.theta2);
                        y2EE = y2 + fieldL*(state.Transmission_CoefH.Magnitude()+extra) * Math.cos(state.theta2);
                        // H field vector
                        MaestroG.drawLineThick(g,x2,y2,x2EE,y2EE,thicknessV, colorH);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT*Math.cos(angoloT),
                                                 y2EE-LenT*Math.sin(angoloT),thicknessV,colorH);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT*Math.cos(angoloT2),
                                                 y2EE-LenT*Math.sin(angoloT2),thicknessV,colorH);
                        //An oval for electric field
                        MaestroG.fillCircleThick(g, x2, y2, (double)radiusH+state.s1,state.s2,colorE);
                        MaestroG.fillCircleThick(g, x2, y2, (double)(radiusH-state.s1), state.s2, Color.white);
                        MaestroG.fillCircleThick(g, x2, y2, (double)state.s3,state.s4,colorE);
                        
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup("E","\u22a5","t","",g,state.font16,(int)x2-lab_pos,(int)y2-lab_pos/2);
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup("H","\u22a5","t","",g,state.font16,
                                                    (int)(x2EE+Math.sin(Math.PI*0.5-state.theta2)*lab_pos/2),
                                                    (int)(y2EE+Math.cos(Math.PI*0.5-state.theta2)*lab_pos));
                    }
                    else{
                        x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(state.theta2)));
                        y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(state.theta2)));
                        x2T = (double)(getSize().width/2 +(int)((radius+state.s10)*Math.cos(state.theta2)));
                        y2T = (double)(getSize().height/2-(int)((radius+state.s10)*Math.sin(state.theta2))-state.s1);
                        x2EE = x2 - fieldL*(state.Transmission_CoefH.Magnitude()+extra) * Math.sin(state.theta2);
                        y2EE = y2 - fieldL*(state.Transmission_CoefH.Magnitude()+extra) * Math.cos(state.theta2);
                        // H field vector
                        MaestroG.drawLineThick(g,x2,y2,x2EE,y2EE,thicknessV, colorH);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT*Math.cos(angoloT),y2EE+LenT*Math.sin(angoloT),thicknessV,colorH);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT*Math.cos(angoloT2),y2EE+LenT*Math.sin(angoloT2),thicknessV,colorH);                        
                        //An oval for electric field
                        MaestroG.fillCircleThick(g, x2, y2, (double)radiusH, state.s2, Color.white);
                        MaestroG.drawLineThick(g, x2 -(double)(radiusH/2), y2 -(double)(radiusH/2),
                                        x2 + (double)(radiusH/2), y2 + (double)(radiusH/2), state.s2,colorE);
                        MaestroG.drawLineThick(g, x2 +(double)(radiusH/2), y2 -(double)(radiusH/2),
                                        x2 - (double)(radiusH/2), y2 + (double)(radiusH/2), state.s2,colorE);
                        MaestroG.drawCircleThick(g, x2, y2, (double)radiusH,state.s2,colorE);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup("E","\u22a5","t","",g,state.font16,(int)x2+lab_pos/2,(int)y2+lab_pos);
                        g.setColor(colorH);                   
                        MaestroG.subscripterBsubsup("H","\u22a5","t","",g,state.font16,
                                                    (int)(x2EE-Math.sin(Math.PI*0.5-state.theta2)*lab_pos),
                                                    (int)(y2EE+Math.cos(Math.PI*0.5-state.theta2)*lab_pos));
                    }
                  }
                }
                else{ // TOTAL REFLECTION
                    //------------------ transmitted field
                    if(state.theta1_deg < 90.0){
                        if(ShowTR && state.theta1_deg < 90.0 ){// Legend
                            MaestroG.drawLineThickB(g,state.s400+state.s90,state.s70,state.s500+state.s30,state.s70, thicknessV2, state.s3, state.s3, Color.blue);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup("Re(H","\u22a5","t",")",g,state.font14,state.s500+state.s35,state.s75);
                            MaestroG.drawLineThickB(g,state.s400+state.s90,state.s95,state.s500+state.s30,state.s95, thicknessV2, state.s3, state.s3, Color.green.darker());
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup("Im(H","\u22a5","t",")",g,state.font14,state.s500+state.s35,state.s100);
                        }
                        if(Field_Out){
                            double angolonew;
                            Complex testfieldy, testfieldx;
                            x2 = (double)(getSize().width/2);
                            y2 = (double)(getSize().height/2-(int)(radius_powernew));
                            x2T = (double)(getSize().width/2 +(int)((radius)*Math.cos(Math.PI*0.5)));
                            y2T = (double)(getSize().height/2-(int)((radius)*Math.sin(Math.PI*0.5)));
                            testfieldx = Complex.Multiply(Math.cos(state.theta1),Complex.Add(1.0,state.Reflection_CoefH));
                            testfieldy = state.Transmission_CoefH;
                            //-------------------------------------------Real
                            y2EE = y2 + fieldL*testfieldx.Real();
                            x2EE = x1 + fieldL*testfieldy.Real();
                            double LenT_re; 
                            if(Math.abs(state.Transmission_CoefH.Real()) > 1.0){
                                LenT_re = (double)state.s15;
                            }
                            else{
                                LenT_re = ((double)state.s15) * Math.pow(Math.sin(Math.abs(state.Transmission_CoefH.Real())*Math.PI/2.0),0.5);
                            }
                            if(ShowTR){
                                angolonew = Math.PI + Math.atan(fieldL*(-testfieldy.Real())/(fieldL*testfieldx.Real()));   
                                MaestroG.drawLineThickB(g,x2,y2,x2EE,y2EE, thicknessV2, state.s3, state.s3, colorH);
                                
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT_re*Math.sin(angoloT+angolonew),y2EE+LenT_re*Math.cos(angoloT+angolonew),thicknessV2,colorH);
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT_re*Math.sin(angoloT2+angolonew),y2EE+LenT_re*Math.cos(angoloT2+angolonew),thicknessV2,colorH);
                            }
                            //-----------------------------------------Imaginary
                            y2EE = y2 + fieldL*testfieldx.Imaginary();
                            x2EE = x1 + fieldL*testfieldy.Imaginary();
                            double LenT_im; 
                            if(Math.abs(state.Transmission_CoefH.Imaginary()) > 1.0){
                                LenT_im = (double)state.s15;
                            }
                            else{
                                LenT_im = ((double)state.s15) * Math.pow(Math.sin(Math.abs(state.Transmission_CoefH.Imaginary())*Math.PI/2.0),0.5);
                            }
                            angolonew = Math.atan(fieldL*(-testfieldy.Imaginary())/(fieldL*testfieldx.Imaginary()));   
                            if(ShowTR){
                                MaestroG.drawLineThickB(g,x2,y2,x2EE,y2EE, thicknessV2, state.s3, state.s3, Color.GREEN.darker());
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT_im*Math.sin(angoloT+angolonew),y2EE+LenT_im*Math.cos(angoloT+angolonew),thicknessV2,Color.green.darker());
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT_im*Math.sin(angoloT2+angolonew),y2EE+LenT_im*Math.cos(angoloT2+angolonew),thicknessV2,colorE.green.darker());
                            }
                            //-------------------------------------------------
                            //An oval for electric field
                            MaestroG.fillCircleThick(g, x2, y2, (double)radiusH+state.s1,state.s2,colorE);
                            MaestroG.fillCircleThick(g, x2, y2, (double)(radiusH-state.s1), state.s2, Color.white);
                            MaestroG.fillCircleThick(g, x2, y2, (double)state.s3,state.s4,colorE);
                            
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            g.setColor(colorE);                   
                            MaestroG.subscripterBsubsup("E","\u22a5","t","",g,state.font16, (int)x2 + state.s5, (int)y2 + state.s25);
                        }
                        else{
                            double angolonew;
                            Complex testfieldy, testfieldx;    
                            x2 = (double)(getSize().width/2);
                            y2 = (double)(getSize().height/2-(int)(radius_powernew));
                            x2T = (double)(getSize().width/2 +(int)((radius)*Math.cos(Math.PI*0.5)));
                            y2T = (double)(getSize().height/2-(int)((radius)*Math.sin(Math.PI*0.5)));
                            //----------------------------------------------Real
                            testfieldx = Complex.Multiply(Math.cos(state.theta1),Complex.Add(1.0,state.Reflection_CoefH));
                            testfieldy = state.Transmission_CoefH;
                            y2EE = y2 - fieldL * testfieldx.Real();
                            x2EE = x1 - fieldL * testfieldy.Real();
                            angolonew = Math.PI + Math.atan(fieldL*(-testfieldy.Real())/(fieldL*testfieldx.Real()));   
                            double LenT_re; 
                            if(Math.abs(state.Transmission_CoefH.Real()) > 1.0){
                                LenT_re = (double)state.s15;
                            }
                            else{
                                LenT_re = ((double)state.s15) * Math.pow(Math.sin(Math.abs(state.Transmission_CoefH.Real())*Math.PI/2.0),0.5);
                            }
                            if(ShowTR){
                                MaestroG.drawLineThickB(g,x2,y2,x2EE,y2EE, thicknessV2, state.s3, state.s3, colorH);   
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT_re*Math.sin(angoloT+angolonew),y2EE-LenT_re*Math.cos(angoloT+angolonew),thicknessV2,colorH);
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT_re*Math.sin(angoloT2+angolonew),y2EE-LenT_re*Math.cos(angoloT2+angolonew),thicknessV2,colorH);
                            }
                            //-----------------------------------------Imaginary
                            testfieldx = Complex.Multiply(Math.cos(state.theta1),Complex.Add(1.0,state.Reflection_CoefH));
                            testfieldy = state.Transmission_CoefH;
                            y2EE = y2 - fieldL * testfieldx.Imaginary();
                            x2EE = x1 - fieldL * testfieldy.Imaginary();
                            angolonew = Math.atan(fieldL*(-testfieldy.Imaginary())/(fieldL*testfieldx.Imaginary()));   
                            double LenT_im; 
                            if(Math.abs(state.Transmission_CoefH.Imaginary()) > 1.0){
                                LenT_im = (double)state.s15;
                            }
                            else{
                                LenT_im = ((double)state.s15) * Math.pow(Math.sin(Math.abs(state.Transmission_CoefH.Imaginary())*Math.PI/2.0),0.5);
                            }
                            if(ShowTR){
                                MaestroG.drawLineThickB(g,x2,y2,x2EE,y2EE, thicknessV2, state.s3, state.s3, Color.GREEN.darker());
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT_im*Math.sin(angoloT+angolonew),y2EE-LenT_im*Math.cos(angoloT+angolonew),thicknessV2,Color.green.darker());
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT_im*Math.sin(angoloT2+angolonew),y2EE-LenT_im*Math.cos(angoloT2+angolonew),thicknessV2,Color.green.darker());
                            }
                            //--------------------------------------------------
                            //An oval for electric field
                             MaestroG.fillCircleThick(g, x2, y2, (double)radiusH, state.s2, Color.white);
                             MaestroG.drawLineThick(g, x2 - (double)(radiusH/2), y2-(double)(radiusH/2),
                                            x2 + (double)(radiusH/2), y2 + (double)(radiusH/2), state.s2,colorE);
                             MaestroG.drawLineThick(g, x2+(double)(radiusH/2), y2-(double)(radiusH/2),
                                            x2 - (double)(radiusH/2), y2 + (double)(radiusH/2), state.s2,colorE);
                             MaestroG.drawCircleThick(g, x2, y2, (double)radiusH,state.s2,colorE);
                             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                             g.setColor(colorE);                   
                             MaestroG.subscripterBsubsup("E","\u22a5","t","",g,state.font16, (int)x2 + state.s5, (int)y2 + state.s25);
                        }
                    }
                }
                if(Field_Refl_Out){ // reflected wave    
                    if(ShowREF && state.theta1_deg < 90.0 ){// Legend
                        MaestroG.drawLineThickB(g,state.s20,state.s70,state.s60,state.s70, thicknessV2, state.s3, state.s3, Color.blue);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        MaestroG.subscripterBsubsup("Re(H","\u22a5","r",")",g,state.font14,state.s65,state.s75);
                        MaestroG.drawLineThickB(g,state.s20,state.s95,state.s60,state.s95, thicknessV2, state.s3, state.s3, Color.green.darker());
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        MaestroG.subscripterBsubsup("Im(H","\u22a5","r",")",g,state.font14,state.s65,state.s100);
                    }
                    double x22, y22;
                    x22 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(Math.PI-state.theta1)));
                    y22 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI-state.theta1)));
                    if(state.Reflection_CoefH.Imaginary() == 0.0){
                        x2EE = x22 + fieldL*(state.Reflection_CoefH.Magnitude()+extra) * Math.sin(Math.PI-state.theta1);
                        y2EE = y22 + fieldL*(state.Reflection_CoefH.Magnitude()+extra) * Math.cos(Math.PI-state.theta1);
                        // H field vector
                        MaestroG.drawLineThick(g,x22,y22,x2EE, y2EE,thicknessV, colorH);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR*Math.cos(angolo+3.0*Math.PI/2.0),y2EE+LenR*Math.sin(angolo+3.0*Math.PI/2.0),thicknessV,colorH);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR*Math.cos(angolo2+3.0*Math.PI/2.0),y2EE+LenR*Math.sin(angolo2+3.0*Math.PI/2.0),thicknessV,colorH);
                        g.setColor(colorH);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        MaestroG.subscripterBsubsup("H","\u22a5","r","",g,state.font16,
                                                (int)(x2EE - Math.cos(state.theta1)*lab_pos),
                                                (int)(y2EE - Math.sin(state.theta1)*lab_pos/2));
                    }
                    else{
                        //----------------------------------------------------- REAL  
                        double LenR_re; 
                        if(Math.abs(state.Reflection_CoefH.Real()) > 1.0){
                             LenR_re = (double)state.s15;
                        }
                        else{
                             LenR_re = ((double)state.s15) * Math.pow(Math.sin(Math.abs(state.Reflection_CoefH.Real())*Math.PI/2.0),0.5);
                        }
                        if(state.Reflection_CoefH.Real() >= 0.0){
                            x2EE = x22 + fieldL*(state.Reflection_CoefH.Real()+extra) * Math.sin(state.theta1);
                            y2EE = y22 - fieldL*(state.Reflection_CoefH.Real()+extra) * Math.cos(state.theta1);
                            // H field vector
                            if(ShowREF){
                                MaestroG.drawLineThickB(g, x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, colorH);
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_re*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE+LenR_re*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV2,colorH);
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_re*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE+LenR_re*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV2,colorH);
                            }
                        }
                        else{
                            x2EE = x22 - fieldL*(state.Reflection_CoefH.Real()+extra) * Math.sin(state.theta1);
                            y2EE = y22 + fieldL*(state.Reflection_CoefH.Real()+extra) * Math.cos(state.theta1);
                            if(ShowREF){
                                // H field vector
                                MaestroG.drawLineThickB(g, x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, colorH);
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_re*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE+LenR_re*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV2,colorH);
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_re*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE+LenR_re*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV2,colorH);
                            }
                        }
                        //------------------------------------------------ IMAGINARY
                        double LenR_im; 
                        if(Math.abs(state.Reflection_CoefH.Imaginary()) > 1.0){
                             LenR_im = (double)state.s15;
                        }
                        else{
                             LenR_im = ((double)state.s15) * Math.pow(Math.sin(Math.abs(state.Reflection_CoefH.Imaginary())*Math.PI/2.0),0.5);
                        }
                        if(Field_Out){
                            x2EE = x22 - fieldL*(state.Reflection_CoefH.Imaginary()+extra) * Math.sin(state.theta1);
                            y2EE = y22 + fieldL*(state.Reflection_CoefH.Imaginary()+extra) * Math.cos(state.theta1);
                            // H field vector
                            if(ShowREF){
                                MaestroG.drawLineThickB(g,x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, Color.GREEN.darker());
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_im*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE+LenR_im*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV2,Color.green.darker());
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_im*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE+LenR_im*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV2,Color.green.darker());
                            }
                        }
                        else{
                            x2EE = x22 + fieldL*(state.Reflection_CoefH.Imaginary()+extra) * Math.sin(state.theta1);
                            y2EE = y22 - fieldL*(state.Reflection_CoefH.Imaginary()+extra) * Math.cos(state.theta1);
                            // H field vector
                            if(ShowREF){
                                MaestroG.drawLineThickB(g,x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, Color.green.darker());
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR_im*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE-LenR_im*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV2,Color.green.darker());
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR_im*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE-LenR_im*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV2,Color.green.darker());
                            }
                        }
                    }                    
                    //An oval for electric field
                    MaestroG.fillCircleThick(g, x22,y22,(double)radiusH+state.s1,state.s2,colorE);
                    MaestroG.fillCircleThick(g, x22,y22,(double)(radiusH-state.s1), state.s2, Color.white);
                    MaestroG.fillCircleThick(g, x22,y22,(double)state.s3,state.s4,colorE);
                    
                    g.setColor(colorE);                   
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    MaestroG.subscripterBsubsup("E","\u22a5","r","",g,state.font16,(int)x22-lab_pos,(int)y22+lab_pos);
                }
                else{ //Field IN
                    if(ShowREF && state.theta1_deg < 90.0 ){// Legend
                        MaestroG.drawLineThickB(g,state.s20,state.s70,state.s60,state.s70, thicknessV2, state.s3, state.s3, Color.blue);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        MaestroG.subscripterBsubsup("Re(H","\u22a5","r",")",g,state.font14,state.s65,state.s75);
                        MaestroG.drawLineThickB(g,state.s20,state.s95,state.s60,state.s95, thicknessV2, state.s3, state.s3, Color.green.darker());
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        MaestroG.subscripterBsubsup("Im(H","\u22a5","r",")",g,state.font14,state.s65,state.s100);
                    }
                    double x22, y22;
                    x22 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(Math.PI-state.theta1)));
                    y22 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI-state.theta1)));
                    if(state.Reflection_CoefH.Imaginary() == 0.0){
                        x2EE = x22 - fieldL*(state.Reflection_CoefH.Magnitude()+extra) * Math.sin(Math.PI-state.theta1);
                        y2EE = y22 - fieldL*(state.Reflection_CoefH.Magnitude()+extra) * Math.cos(Math.PI-state.theta1);
                        // H field vector
                        MaestroG.drawLineThick(g,x22,y22,x2EE,y2EE,thicknessV, colorH);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE-LenR*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV,colorH);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE-LenR*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV,colorH);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(colorH);                   
                        MaestroG.subscripterBsubsup("H","\u22a5","r","",g,state.font16,
                                        (int)(x2EE-Math.sin(state.theta1)*lab_pos),
                                        (int)(y2EE+Math.cos(state.theta1)*lab_pos));
                    }
                    else{
                        //----------------------------------------------------- REAL
                        double LenR_re; 
                        if(Math.abs(state.Reflection_CoefH.Real()) > 1.0){
                             LenR_re = (double)state.s15;
                        }
                        else{
                             LenR_re = ((double)state.s15) * Math.pow(Math.sin(Math.abs(state.Reflection_CoefH.Real())*Math.PI/2.0),0.5);
                        }
                        if(state.Reflection_CoefH.Real() < 0.0){
                            x2EE = x22 + fieldL*(state.Reflection_CoefH.Real()+extra) * Math.sin(state.theta1);
                            y2EE = y22 - fieldL*(state.Reflection_CoefH.Real()+extra) * Math.cos(state.theta1);
                            // H field vector
                            if(ShowREF){
                                MaestroG.drawLineThickB(g,x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, colorH);
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR_re*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE-LenR_re*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV2,colorH);
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR_re*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE-LenR_re*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV2,colorH);
                            }
                        }
                        else{
                            x2EE = x22 - fieldL*(state.Reflection_CoefH.Real()+extra) * Math.sin(state.theta1);
                            y2EE = y22 + fieldL*(state.Reflection_CoefH.Real()+extra) * Math.cos(state.theta1);
                            // H field vector
                            if(ShowREF){
                                MaestroG.drawLineThickB(g,x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, colorH);
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR_re*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE-LenR_re*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV2,colorH);
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR_re*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE-LenR_re*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV2,colorH);
                            }
                        }
                        //------------------------------------------------ IMAGINARY
                        double LenR_im; 
                        if(Math.abs(state.Reflection_CoefH.Imaginary()) > 1.0){
                             LenR_im = (double)state.s15;
                        }
                        else{
                             LenR_im = ((double)state.s15) * Math.pow(Math.sin(Math.abs(state.Reflection_CoefH.Imaginary())*Math.PI/2.0),0.5);
                        }
                        if(Field_Out){
                            x2EE = x22 - fieldL*(state.Reflection_CoefH.Imaginary()+extra) * Math.sin(state.theta1);
                            y2EE = y22 + fieldL*(state.Reflection_CoefH.Imaginary()+extra) * Math.cos(state.theta1);
                            // H field vector
                            if(ShowREF){
                                MaestroG.drawLineThickB(g,x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, Color.green.darker());
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_im*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE+LenR_im*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV2,Color.green.darker());
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR_im*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE+LenR_im*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV2,Color.green.darker());
                            }
                        }
                        else{
                            x2EE = x22 + fieldL*(state.Reflection_CoefH.Imaginary()+extra) * Math.sin(state.theta1);
                            y2EE = y22 - fieldL*(state.Reflection_CoefH.Imaginary()+extra) * Math.cos(state.theta1);
                            // H field vector
                            if(ShowREF){
                                MaestroG.drawLineThickB(g,x22, y22, x2EE, y2EE, thicknessV2, state.s3, state.s3, Color.green.darker());
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR_im*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE-LenR_im*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV2,Color.green.darker());
                                MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR_im*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE-LenR_im*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV2,Color.green.darker());
                            }
                        }
                    }
                    //----------------------------------------------------------
                    //An oval for electric field
                    MaestroG.fillCircleThick(g, x22, y22, (double)radiusH, state.s2, Color.white);
                    MaestroG.drawLineThick(g, x22-(double)(radiusH/2), y22-(double)(radiusH/2),
                                    x22 + (double)(radiusH/2), y22 + (double)(radiusH/2), state.s2,colorE);
                    MaestroG.drawLineThick(g, x22+(double)(radiusH/2), y22-(double)(radiusH/2),
                                    x22 - (double)(radiusH/2), y22 + (double)(radiusH/2), state.s2,colorE);
                    MaestroG.drawCircleThick(g, x22, y22, (double)radiusH,state.s2,colorE);
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    g.setColor(colorE);                   
                    MaestroG.subscripterBsubsup("E","\u22a5","r","",g,state.font16,(int)x22+lab_pos/2,(int)y22-lab_pos/2);
                }
         }
    }
    //-------------------------------------------------------------------------------------------------
    private void drawScaleRefTrans2(Graphics g, Color colore, int thickness){
        double x1, y1, x2, y2, x3, y3;
        int thick = state.s6;
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    	
        //transmitted ray
        x1 = (double)(getSize().width/2);
        y1 = (double)(getSize().height/2);
        x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(state.theta2)*(1.0-state.Reflection_Coef.Magnitude()*
                   state.Reflection_Coef.Magnitude())));
        y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(state.theta2)*(1.0-state.Reflection_Coef.Magnitude()*
                   state.Reflection_Coef.Magnitude())));
        x3 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(state.theta2)));
        y3 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(state.theta2)));
        if(state.theta1_deg < 90.0){MaestroG.drawLineThick(g, x1, y1, x3, y3, thick, Color.green);}
        MaestroG.drawLineThick(g, x1, y1, x2, y2, thick, colore); 
        
        //reflected ray
        x1 = (double)(getSize().width/2);
        y1 = (double)(getSize().height/2);
        x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(Math.PI - state.theta1)*state.Reflection_Coef.Magnitude()*
                                               state.Reflection_Coef.Magnitude()));
        y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI - state.theta1)*state.Reflection_Coef.Magnitude()* 
                                               state.Reflection_Coef.Magnitude()));
        x3 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(Math.PI - state.theta1)));
        y3 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI - state.theta1)));
        if(state.theta1 > Math.PI/60.0){
            MaestroG.drawLineThick(g, x1, y1, x3, y3, thick, Color.green);
            MaestroG.drawLineThick(g, x1, y1, x2, y2, thick, colore);
        }
        else{
            MaestroG.drawLineThick(g, x1, y1-state.s2, x3, y3-state.s2, thick, Color.green);
            MaestroG.drawLineThick(g, x1, y1-state.s2, x2, y2-state.s2, thick, colore);
        }
    }
    
    public void update(Graphics g){
	paint(g);
    }
    
    public void mouseClicked(MouseEvent evt){
        if(IsFocusOn){
            IsFocusOn = false;
            repaint();
        }
        else{
            IsFocusOn = true;
            repaint();
        }
    }
    public void mouseEntered(MouseEvent evt){
	IsPrintOn = true;
	repaint();
    }
    public void mouseExited(MouseEvent evt){
	IsPrintOn = false;
	repaint();
    }
    public void mousePressed(MouseEvent evt){}
    public void mouseReleased(MouseEvent evt){}
}
