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 static final int LeftMargin=10, RightMargin=10, TopMargin=10, BottomMargin=10;
    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 static final int HGap=10, VGap=10;//for vectors 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 = 150;
    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;
	IsFocusOn = false;
        IsPrintOn = false;
        Field_Out = false;
        Field_Refl_Out = true;
        ShowTR = false;
        ShowREF = false;
        
	setBackground(bgcolor);
	//Listeners
	this.addMouseListener(this);
        
        radius = getSize().height/2-TopMargin-scale_radius*VGap/20;
        radius_power = radius * 6/10;
    }

    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-10,getSize().height/2);
	MaestroG.drawArrow( getSize().width-RightMargin-10, 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-10);
        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 = 7.0;
        radiusdot = 2.0;
        radiusX = 8.0;
        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,1,colorX);
        MaestroG.fillCircleThick(g, x1, y1,radiusy-1.0,1,Color.white);
        MaestroG.fillCircleThick(g, x1, y1,radiusdot,1,colorX);
        
        
        g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
        
    }
    
    private void drawLabels(Graphics g){
	g.setFont(axisfont);
	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)-5,fm.getHeight());
	tmp = "z";
	g.drawString(tmp,getSize().width-RightMargin-fm.stringWidth(tmp)-5,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.setColor(Color.red.darker());
	tmp =  "medium 1";
	g.drawString(tmp,LeftMargin +10,getSize().height-7*VGap);
	g.setColor(Color.blue.darker());
	tmp  = "medium 2";
	g.drawString(tmp,getSize().width-95,getSize().height-7*VGap);
	
        int x0 = getSize().width/4;
        int x1 = getSize().width/2 +20;
        int y0 = getSize().height-15;
	int y1 = getSize().height-30;
        g.setFont(smallfont);
        g.setColor(Color.red.darker());
        if(IsPrintOn){
            
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            if(IsFocusOn){
                g.drawString("Click to Hide Power Flow",x1,y1);
            }
            else{
                g.drawString("Click to Visualize Power Flow",x1,y1);
            }
                
        }
        if(IsFocusOn){
                g.setFont(normalfontB);
                g.setColor(Color.red);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g.drawString("Power Flow",x1+40,y0);
                MaestroG.drawLineThick(g, x1, y0-5, x1+30, y0-5, 3, Color.red);
        }
        
        g.setColor(Color.black);
        g.setFont(labfontbig);
        //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.drawString("f",x0-2,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+5;
	//mediumY = TopMargin+2*VGap;
        mediumY = getSize().height-5*VGap;
	
        g.setColor(Color.red.darker());
	
        MaestroG.subscripter("\u03b5","r 1"," = "+state.epsilon_r1,g,12,mediumX+7,mediumY); 
	g.setFont(normalfont);   
	fm = g.getFontMetrics();
        
	mediumY += 4*fm.getHeight()/3;
        
	MaestroG.subscripter("\u03bc","r 1"," = "+state.mu_r1,g,12,mediumX+5,mediumY);
	g.setFont(normalfont); 
	fm = g.getFontMetrics();
        
	mediumY += fm.getHeight();
        //MaestroG.subscripter("  \u03c3","1"," = "+state.conductivity1,g,12,mediumX+5,mediumY);  
	g.setFont(normalfontB); 
	fm = g.getFontMetrics();
        
        mediumY = fm.getHeight();
        
        int Xpolar = 150;
        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-100;
        //mediumY = TopMargin+2*VGap;
        mediumY = getSize().height-5*VGap;
        
        MaestroG.subscripter("\u03b5","r 2"," = "+state.epsilon_r2,g,12,mediumX+7,mediumY);    
	g.setFont(normalfont); 
	fm = g.getFontMetrics();
        
	mediumY += 4*fm.getHeight()/3;
	MaestroG.subscripter("\u03bc","r 2"," = "+state.mu_r2,g,12,mediumX+5,mediumY);
	g.setFont(normalfont); 
	fm = g.getFontMetrics();
        
	mediumY += fm.getHeight();
	//MaestroG.subscripter("  \u03c3","2"," = "+state.conductivity2,g,12,mediumX+5,mediumY);  
	g.setFont(normalfont); 
	fm = g.getFontMetrics();
        
    }
    
    
    //------------------------------------------------------------------------------------------    
    
        private void drawWaves2(Graphics g){
        double x1, y1, x2, y2;
        int thickness = 6;
        int lab_pos = 28;
        
        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 = 15.0;
        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 = 3;
        thicknessV2 = 2;
        thicknessV3 = 0;
        radiusH = 8;
        
        
        // 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 - 15.0;
          yo = y1 - 50.0;
          rado = 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-40.0,y1-40.0,80.0,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-40.0, y1-40.0, 80.0, 180.0, 180.0/Math.PI*state.theta1, 3, 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,14,(int)(x1+rado*Math.cos(ango))-5, (int)(y1+2-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,14,(int)x1-(int)(50.0*Math.cos(state.theta1+Math.PI/7.0)),
                                                            (int)y1-5+(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,14,330,20);
                    MaestroG.subscripter("(Total Transmission)","","",g,14,330,40);
                }
          }
          
          else
          {
            if(state.wavelength1 == state.wavelength2){
            }
            else{
                    // reflected wave
                      MaestroG.FillArcTransp(g,x1-40.0,y1-40.0,80.0,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-40.0, y1-40.0, 80.0, 180.0, -180.0/Math.PI*state.theta1, 3, Color.cyan.darker());
                          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                          MaestroG.subscripter("\u03b8","r","",g,14,(int)(x1+rado*Math.cos(ango3))-5, (int)(y1+6-rado*Math.sin(ango3)));
                      }
                      else{
                          MaestroG.drawArcThick(g, x1-40.0, y1-40.0, 80.0, 180.0, -180.0/Math.PI*state.theta1, 3, Color.cyan.darker());
                          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                          MaestroG.subscripter("\u03b8","r","",g,14,(int)x1-(int)(50.0*Math.cos(state.theta1+Math.PI/7.0)),
                                                            (int)y1+10-(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-40.0,y1-40.0,80.0,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-40.0, y1-40.0, 80.0, 0.0, 180.0/Math.PI*state.theta2, 3, Color.yellow); 
                      g.setColor(Color.black);
                      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                      MaestroG.subscripter("\u03b8","t","",g,14,(int)(x1+rado*Math.cos(ango2))-10, (int)(y1+5-rado*Math.sin(ango2)));
                  }
                  else if(180.0/Math.PI*state.theta2 < 20.0 && state.Reflection_Coef.Imaginary() == 0.0){
                      MaestroG.drawArcThick(g, x1-40.0, y1-40.0, 80.0, 0.0, 180.0/Math.PI*state.theta2, 3, Color.yellow);
                      g.setColor(Color.black);
                      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                      MaestroG.subscripter("\u03b8","t","",g,14,(int)x1-10+(int)(50.0*Math.cos(state.theta2+Math.PI/7.0)),
                                                                (int)y1+10-(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,14,330,20);
                  MaestroG.subscripter("(Total Reflection)","","",g,14,330,40);

                  MaestroG.FillArcTransp(g,x1-40.0,y1-40.0,80.0, 0.0,90.0, Color.yellow, 0.5);
                  MaestroG.drawArcThick(g, x1-40.0, y1-40.0, 80.0, 0.0, 90.0, 3, Color.yellow); 
                  g.setColor(Color.black);
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  MaestroG.subscripter("90","","\u00ba",g,14,(int)(x1+rado*Math.sin(Math.PI*0.25))-5, (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,11,490,40);
                  MaestroG.subscripter("field details","","",g,11,490,55);
                  
                  MaestroG.subscripter("Reflected","","",g,11,20,40);
                  MaestroG.subscripter("field details","","",g,11,20,55);
                  
                  g.setColor(Color.black);
                  MaestroG.subscripter("Reflection  and  transmission coefficients","","",g,12,330,410);
                  MaestroG.subscripter("are complex.   To review the fields, select:","","",g,12,330,430);
                  g.setColor(Color.blue.darker());
                  MaestroG.subscripter("     Output \u21aa Field Phasors at (0,0,0)","","",g,12,330,450);
              }
          }
          
        // 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 = 15.0;
          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,1,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,18,(int)(x2+x2V)/2-20,(int)(y2+y2V)/2-15); // Ulaby's book notation
          // 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-1,y2,x2-1,y2V,1,colorV);
          MaestroG.drawArrow((int)(x2-1),(int)y2V+8,5,g);
          //---------------------------------------------------------------
          
          if(IsFocusOn){MaestroG.drawLineThick(g, x1, y1+2, x2_power, y2_power+2, thickness, 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, 2, Color.white);
                
                        MaestroG.drawCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),
                                    y1+(radius*Math.sin(Math.PI - state.theta1)),2.0,4,colorH);
                        MaestroG.drawCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),
                                    y1+(radius*Math.sin(Math.PI - state.theta1)),(double)radiusH,2,colorH);
                        
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup2("H","//","i","",g,16,
                                                    (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,16,
                                                    (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, 2, 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), 2,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), 2,colorH);
                        MaestroG.drawCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),
                                    y1+(radius*Math.sin(Math.PI - state.theta1)),(double)radiusH,2,colorH);
                
                        /*
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup2("H","//","i","",g,16,(int)(x1 + (radius*Math.cos(Math.PI - state.theta1)))-25, 
                                                     (int)(y1 + (radius*Math.sin(Math.PI - state.theta1)))+25);
                                                     
                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","i","",g,16,(int)(-25.0 + x2E),(int)(y2E+25.0));
                        */
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup2("H","//","i","",g,16,
                                                    (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-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, 2, Color.white);
                                    
                        MaestroG.drawCircleThick(g, x2,y2,2.0,4,colorE);
                        MaestroG.drawCircleThick(g, x2, y2,(double)radiusH,2,colorE);
                
                
                        g.setColor(colorE);                   
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        MaestroG.subscripterBsubsup("E","\u22a5","i","",g,16,
                                                    (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,16,
                                                    (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, 2, Color.white);
                                    
                        MaestroG.drawLineThick(g, x2-(double)(radiusH/2),
                                    y2-(double)(radiusH/2),x2 + (double)(radiusH/2),
                                    y2 + (double)(radiusH/2), 2,colorE);
                        MaestroG.drawLineThick(g, x2+(double)(radiusH/2),
                                    y2-(double)(radiusH/2),x2 - (double)(radiusH/2),
                                    y2 + (double)(radiusH/2), 2,colorE);
                        MaestroG.drawCircleThick(g, x2, y2,(double)radiusH,2,colorE);
                
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(colorE);
                        MaestroG.subscripterBsubsup("E","\u22a5","i","",g,16,
                                                    (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,16,
                                                    (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 = 28;
        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+13;
        
	g.setColor(incident);
	double Len = 15.0;
        double LenT = 15.0;
        double LenR = 15.0 * Math.pow(Math.sin(state.Reflection_Coef.Magnitude()*Math.PI/2.0),0.5);
        if(state.Transmission_Coef.Magnitude()>1.0){
            LenT = 15.0;
        }
        else{
            LenT = 15.0 * Math.pow(Math.sin(state.Transmission_Coef.Magnitude()*Math.PI/2.0),0.5);
        }
        double LenTH = 15.0;
        if(state.Transmission_CoefH.Magnitude()>1.0){
            LenTH = 15.0;
        }
        else{
            LenTH = 15.0 * 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 = 3;
        thicknessV2 = 2;
        thicknessV3 = 0;
        radiusH = 8;
        
        // 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'tdraw 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,1,Color.gray); // thin line to center
              MaestroG.drawLineThick(g,x2,y2,x2R,y2R,thicknessV,colorV);// vector line, thick
              //MaestroG.subscripter("\u03b2","r","",g,18,(int)(x2+x2R)/2+15,(int)(y2+y2R)/2-15);
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              //MaestroG.subscripterBHat("k","r","",g,18,(int)(x2+x2R)/2+15,(int)(y2+y2R)/2-15); // Ulaby's book notation
              MaestroG.subscripterB("k","r","",g,18,(int)(x2+x2R)/2+15,(int)(y2+y2R)/2-15); // Ulaby's book notation
              // z-component
              MaestroG.drawLineThick(g,x2,y2,x2R,y2,1,colorV);
              MaestroG.drawArrow((int)x2R+8,(int)y2,8,g);
              // x-component
              MaestroG.drawLineThick(g,x2,y2,x2,y2R,1,colorV);
              MaestroG.drawArrow((int)x2,(int)y2R+9,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,1,Color.gray); // thin line to center
                  MaestroG.drawLineThick(g,x2,y2,x2T,y2T,thicknessV,colorV); // vector line, thick
                  //MaestroG.subscripter("\u03b2","t","",g,18,(int)(x2+x2T)/2-25,(int)(y2+y2T)/2-15);
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  //MaestroG.subscripterBHat("k","t","",g,18,(int)(x2+x2T)/2-25,(int)(y2+y2T)/2-15); // Ulaby's book notation
                  MaestroG.subscripterB("k","t","",g,18,(int)(x2+x2T)/2-25,(int)(y2+y2T)/2-15); // 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,18,(int)(x2+x2T)/2-22,(int)(y2+y2T)/2+18); // Ulaby's book notation        
                      g.setColor(Color.red.darker());
                      MaestroG.subscripterB("surface wave","","",g,12,(int)(x2)+30,80);

                  }
              }
          }
          
          //--------------------------------------------------------------------
          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+10)*Math.cos(state.theta2)));
                        y2T = (double)(getSize().height/2-(int)((radius+10)*Math.sin(state.theta2))-1);

                        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, 2, Color.white);

                        MaestroG.drawCircleThick(g, x2, y2, 2.0,4,colorH);
                        MaestroG.drawCircleThick(g, x2, y2, (double)radiusH,2,colorH);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup2("H","//","t","",g,16,(int)x2+lab_pos/2, (int)y2+lab_pos);

                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","t","",g,16,
                                                    (int)(x2EE-Math.sin(Math.PI/2-state.theta2)*lab_pos),
                                                    (int)(y2EE+Math.cos(Math.PI/2-state.theta2)*lab_pos));//(int)(-20.0 + x2EE),(int)(y2EE)+10);
                    }
                    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+10)*Math.cos(state.theta2)));
                        y2T = (double)(getSize().height/2-(int)((radius+10)*Math.sin(state.theta2))-1);

                        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, 2, Color.white);

                         MaestroG.drawLineThick(g, x2-(double)(radiusH/2), y2 -(double)(radiusH/2),
                                        x2 + (double)(radiusH/2), y2 + (double)(radiusH/2), 2,colorH);
                         MaestroG.drawLineThick(g, x2 +(double)(radiusH/2), y2 -(double)(radiusH/2),
                                        x2 - (double)(radiusH/2), y2 + (double)(radiusH/2), 2,colorH);

                         MaestroG.drawCircleThick(g, x2, y2, (double)radiusH,2,colorH);
                         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup2("H","//","t","",g,16,(int)x2-lab_pos,(int)y2-lab_pos/2);

                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","t","",g,16,
                                                    (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));//(int)(5.0 + x2EE),(int)(y2EE)+10);

                    }
                    }
                }
                else{ // Total Reflection
                    // ------------------------ Transmitted Wave
                    if(state.theta1_deg < 90.0){
                        if(ShowTR){//  Legend
                            MaestroG.drawLineThickB(g,490,70,530,70, thicknessV2, 3, 3, Color.magenta.darker());
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup2("Re(E","//","t",")",g,14,535,75);
                            MaestroG.drawLineThickB(g,490,95,530,95, thicknessV2, 3, 3, Color.red);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup2("Im(E","//","t",")",g,14,535,100);
                        }
                        
                        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);

                            // Simple approach, just use Magnitude of transmitted vector just for testing, not significative
                            //y2EE = y2 + fieldL * testfieldx.Magnitude();
                            //x2EE = x1 - fieldL * testfieldy.Magnitude();
                            //angolonew = Math.atan(fieldL*(testfieldy.Magnitude())/(fieldL*testfieldx.Magnitude()));   
                            //// E field vector
                            //MaestroG.drawLineThick(g,x1,y2,x2EE,y2EE, thicknessV, Color.white);
                            ////vector arrow head
                            //MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT*Math.sin(angoloT+angolonew),y2EE-LenT*Math.cos(angoloT+angolonew),thicknessV,colorE);
                            //MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT*Math.sin(angoloT2+angolonew),y2EE-LenT*Math.cos(angoloT2+angolonew),thicknessV,colorE);
                            
                            //--------------------------------------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, 3, 3, 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);

                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //g.setColor(colorE);                   
                                //MaestroG.subscripterBsubsup2("Re(E","//","t",")",g,16,(int)(x2EE)-lab_pos/2,(int)(y2EE)-lab_pos/2);
                            }
                            //--------------------------------------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 = 15.0;
                            }
                            else{
                                LenT_im = 15.0 * 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, 3, 3, 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);

                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //g.setColor(colorE.darker());                   
                                //MaestroG.subscripterBsubsup2("Im(E","//","t",")",g,16,(int)(x2EE)-lab_pos/2,(int)(y2EE)+lab_pos);
                            }
                            //--------------------------------------------------
                            
                            //An oval for magnetic field

                            MaestroG.fillCircleThick(g, x1,y2,(double)radiusH, 2, Color.white);

                            MaestroG.drawCircleThick(g, x1, y2,2.0,4,colorH);
                            MaestroG.drawCircleThick(g, x1, y2,(double)radiusH,2,colorH);
                            
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            g.setColor(Color.blue);                   
                            MaestroG.subscripterBsubsup2("H","//","t","",g,16,(int)(x1)+5, (int)(y2)+25);

                        }
                        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(state.epsilon_r1/state.epsilon_r2,state.Transmission_Coef);
                            testfieldy = Complex.Multiply(1.0,state.Transmission_Coef);
                            
                            // Simple approach with magnitude just to test system, not significative
                            //y2EE = y2 - fieldL*testfieldx.Magnitude();
                            //x2EE = x1 + fieldL*(testfieldy.Magnitude());
                            //angolonew = Math.atan(fieldL*(testfieldy.Magnitude())/(fieldL*testfieldx.Magnitude()));   
                            //// E field vector
                            //MaestroG.drawLineThick(g,x2,y2,x2EE, y2EE,thicknessV, Color.white);
                            //// arrow head
                            //MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT*Math.sin(angoloT+angolonew),y2EE+LenT*Math.cos(angoloT+angolonew),thicknessV,colorE);
                            //MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT*Math.sin(angoloT2+angolonew),y2EE+LenT*Math.cos(angoloT2+angolonew),thicknessV,colorE);
                            
                            //----------------------------------------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, 3, 3, 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);

                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //g.setColor(colorE);
                                //MaestroG.subscripterBsubsup2("Re(E","//","t",")",g,16,(int)(x2EE)+lab_pos/2,(int)(y2EE)+lab_pos/2);
                            }
                            //----------------------------------------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 = 15.0;
                            }
                            else{
                                LenT_im = 15.0 * 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, 3, 3, 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);

                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //g.setColor(colorE.darker());
                                //MaestroG.subscripterBsubsup2("Im(E","//","t",")",g,16,(int)(x2EE)-lab_pos/2,(int)(y2EE)-lab_pos/2);
                            }
                            //--------------------------------------------------
                            
                            //An oval for magnetic field

                             MaestroG.fillCircleThick(g, x2, y2, (double)radiusH, 2, Color.white);

                             MaestroG.drawLineThick(g, x2-(double)(radiusH/2), y2 -(double)(radiusH/2),
                                            x2 + (double)(radiusH/2), y2 + (double)(radiusH/2), 2,colorH);
                             MaestroG.drawLineThick(g, x2 +(double)(radiusH/2), y2 -(double)(radiusH/2),
                                            x2 - (double)(radiusH/2), y2 + (double)(radiusH/2), 2,colorH);

                             MaestroG.drawCircleThick(g, x2, y2, (double)radiusH,2,colorH);
                             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            
                             g.setColor(colorH);                   
                             MaestroG.subscripterBsubsup2("H","//","t","",g,16,(int)(x1)+5, (int)(y2)+25);

                        }
                    }
                }
                
                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, 2, Color.white);

                        MaestroG.drawCircleThick(g, x22, y22, 2.0,4,colorH);
                        MaestroG.drawCircleThick(g, x22, y22, (double)radiusH,2,colorH);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup2("H","//","r","",g,16,(int)(x22)+lab_pos/2, (int)(y22)-lab_pos/2);

                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","r","",g,16,
                                                    (int)(x2EE-Math.sin(state.theta1)*lab_pos),
                                                    (int)(y2EE+Math.cos(state.theta1)*lab_pos));
                                                    //(int)(-20.0 + x2EE),(int)(y2EE));                    
                    }
                    else{//Total Reflection - Field OUT Parallel Polarization
                        
                        if(ShowREF && state.theta1_deg < 90.0 ){// Legend
                            MaestroG.drawLineThickB(g,20,70,60,70, thicknessV2, 3, 3, Color.magenta.darker());
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup2("Re(E","//","r",")",g,14,65,75);
                            MaestroG.drawLineThickB(g,20,95,60,95, thicknessV2, 3, 3, Color.red);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup2("Im(E","//","r",")",g,14,65,100);
                        }
                    
                    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);
                    //----------------------------------------------------- REAL  
                    double LenR_re; 
                    if(Math.abs(state.Reflection_Coef.Real()) > 1.0){
                         LenR_re = 15.0;
                    }
                    else{
                         LenR_re = 15.0 * 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, 3, 3, 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);
                            //g.setColor(colorE);                   
                            //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            //MaestroG.subscripterBsubsup2("Re(E","//","r",")",g,16,
                            //                    (int)(x2EE-Math.sin(state.theta1)*lab_pos),
                            //                    (int)(y2EE+Math.cos(state.theta1)*lab_pos)); 
                        }
                    }
                    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, 3, 3, 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);
                            //g.setColor(colorE);                   
                            //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            //MaestroG.subscripterBsubsup2("Re(E","//","r",")",g,16,
                            //                    (int)(x2EE-Math.sin(state.theta1)*lab_pos),
                            //                    (int)(y2EE+Math.cos(state.theta1)*lab_pos));
                        }
                    }
                    
                    //------------------------------------------------ IMAGINARY
                    double LenR_im; 
                    if(Math.abs(state.Reflection_Coef.Imaginary()) > 1.0){
                         LenR_im = 15.0;
                    }
                    else{
                         LenR_im = 15.0 * 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, 3, 3, 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);
                    
                            //g.setColor(colorE.darker());                   
                            //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            //MaestroG.subscripterBsubsup2("Im(E","//","r",")",g,16,
                            //                    (int)(x2EE-Math.sin(state.theta1)*lab_pos),
                            //                    (int)(y2EE+Math.cos(state.theta1)*lab_pos));
                        }    
                    }
                    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, 3, 3, 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);
                        
                            //g.setColor(colorE.darker());                   
                            //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            //MaestroG.subscripterBsubsup2("Im(E","//","r",")",g,16,
                            //                    (int)(x2EE-Math.sin(state.theta1)*lab_pos),
                            //                    (int)(y2EE+Math.cos(state.theta1)*lab_pos));
                        }
                    }
                    
                    //An oval for magnetic field

                     MaestroG.fillCircleThick(g, x22, y22, (double)radiusH, 2, Color.white);

                    MaestroG.drawCircleThick(g, x22, y22, 2.0,4,colorH);
                    MaestroG.drawCircleThick(g, x22, y22, (double)radiusH,2,colorH);
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    g.setColor(Color.blue);                   
                    MaestroG.subscripterBsubsup2("H","//","r","",g,16,(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, 2, Color.white);
                        MaestroG.drawLineThick(g, x22 - (double)(radiusH/2), y22-(double)(radiusH/2),
                                        x22 + (double)(radiusH/2), y22 + (double)(radiusH/2), 2,colorH);
                        MaestroG.drawLineThick(g, x22 +(double)(radiusH/2), y22 -(double)(radiusH/2),
                                        x22 - (double)(radiusH/2), y22 + (double)(radiusH/2), 2,colorH);
                        MaestroG.drawCircleThick(g, x22,y22,(double)radiusH,2,colorH);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(colorH);                   
                        MaestroG.subscripterBsubsup2("H","//","r","",g,16,(int)x22-lab_pos,(int)y22+lab_pos);

                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","r","",g,16,
                                                    (int)(x2EE+Math.sin(state.theta1)*lab_pos/2),
                                                    (int)(y2EE-Math.cos(state.theta1)*lab_pos/2));
                                                    //(int)(5.0 + x2EE),(int)(y2EE)-5);
                    }
                    else{// Total Reflection
                        
                        if(ShowREF && state.theta1_deg < 90.0 ){// Legend
                            MaestroG.drawLineThickB(g,20,70,60,70, thicknessV2, 3, 3, Color.magenta.darker());
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup2("Re(E","//","r",")",g,14,65,75);
                            MaestroG.drawLineThickB(g,20,95,60,95, thicknessV2, 3, 3, Color.red);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup2("Im(E","//","r",")",g,14,65,100);
                        }
                        
                    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)));
                    
                    // reference magnitude
                    //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);
                    
                    //----------------------------------------------------- REAL
                    double LenR_re; 
                    if(Math.abs(state.Reflection_Coef.Real()) > 1.0){
                         LenR_re = 15.0;
                    }
                    else{
                         LenR_re = 15.0 * 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, 3, 3, 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);
                            //g.setColor(colorE);                   
                            //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            //MaestroG.subscripterBsubsup2("Re(E","//","r",")",g,16,
                            //                    (int)(x2EE+Math.sin(state.theta1)*lab_pos/2),
                            //                    (int)(y2EE-Math.cos(state.theta1)*lab_pos/2));
                        }
                    }
                    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, 3, 3, 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);
                            //g.setColor(colorE);                   
                            //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            //MaestroG.subscripterBsubsup2("Re(E","//","r",")",g,16,
                            //                    (int)(x2EE+Math.sin(state.theta1)*lab_pos/2),
                            //                    (int)(y2EE-Math.cos(state.theta1)*lab_pos/2));
                        }
                    }
                    //------------------------------------------------ IMAGINARY
                    double LenR_im; 
                    if(Math.abs(state.Reflection_Coef.Imaginary()) > 1.0){
                         LenR_im = 15.0;
                    }
                    else{
                         LenR_im = 15.0 * 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, 3, 3, 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);
                            
                            //g.setColor(colorE.darker());                   
                            //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            //MaestroG.subscripterBsubsup2("Im(E","//","r",")",g,16,
                            //                    (int)(x2EE+Math.sin(state.theta1)*lab_pos/2),
                            //                    (int)(y2EE-Math.cos(state.theta1)*lab_pos/2));
                        }
                    }
                    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, 3, 3, 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);
                            
                            //g.setColor(colorE.darker());                   
                            //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            //MaestroG.subscripterBsubsup2("Im(E","//","r",")",g,16,
                            //                    (int)(x2EE+Math.sin(state.theta1)*lab_pos/2),
                            //                    (int)(y2EE-Math.cos(state.theta1)*lab_pos/2));
                        }    
                    }
                    
                    //----------------------------------------------------------
                    //An oval for magnetic field

                    MaestroG.fillCircleThick(g, x22, y22, (double)radiusH, 2, Color.white);

                    MaestroG.drawLineThick(g, x22 - (double)(radiusH/2), y22-(double)(radiusH/2),
                                    x22 + (double)(radiusH/2), y22 + (double)(radiusH/2), 2,colorH);
                    MaestroG.drawLineThick(g, x22 +(double)(radiusH/2), y22 -(double)(radiusH/2),
                                    x22 - (double)(radiusH/2), y22 + (double)(radiusH/2), 2,colorH);
                    MaestroG.drawCircleThick(g, x22,y22,(double)radiusH,2,colorH);
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    g.setColor(colorH);                   
                    MaestroG.subscripterBsubsup2("H","//","r","",g,16,(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+10)*Math.cos(state.theta2)));
                        y2T = (double)(getSize().height/2-(int)((radius+10)*Math.sin(state.theta2))-1);

                        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, 2, Color.white);

                        MaestroG.drawCircleThick(g, x2, y2, 2.0,4,colorE);
                        MaestroG.drawCircleThick(g, x2, y2, (double)radiusH,2,colorE);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup("E","\u22a5","t","",g,16,(int)x2-lab_pos,(int)y2-lab_pos/2);

                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup("H","\u22a5","t","",g,16,
                                                    (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));
                                                    //(int)x2EE+5,(int)y2EE+10);
                        
                    }
                    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+10)*Math.cos(state.theta2)));
                        y2T = (double)(getSize().height/2-(int)((radius+10)*Math.sin(state.theta2))-1);
                        
                        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, 2, Color.white);

                         MaestroG.drawLineThick(g, x2 -(double)(radiusH/2), y2 -(double)(radiusH/2),
                                        x2 + (double)(radiusH/2), y2 + (double)(radiusH/2), 2,colorE);
                         MaestroG.drawLineThick(g, x2 +(double)(radiusH/2), y2 -(double)(radiusH/2),
                                        x2 - (double)(radiusH/2), y2 + (double)(radiusH/2), 2,colorE);

                         MaestroG.drawCircleThick(g, x2, y2, (double)radiusH,2,colorE);
                         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                         g.setColor(colorE);                   
                            MaestroG.subscripterBsubsup("E","\u22a5","t","",g,16,(int)x2+lab_pos/2,(int)y2+lab_pos);

                         g.setColor(colorH);                   
                         MaestroG.subscripterBsubsup("H","\u22a5","t","",g,16,
                                                     (int)(x2EE-Math.sin(Math.PI*0.5-state.theta2)*lab_pos),
                                                     (int)(y2EE+Math.cos(Math.PI*0.5-state.theta2)*lab_pos));
                                                     //(int)x2EE-20,(int)y2EE+10);
                        
                    }
                  }
                }
                else{ // TOTAL REFLECTION
                    
                    //------------------ transmitted field
                    if(state.theta1_deg < 90.0){
                        
                        if(ShowTR && state.theta1_deg < 90.0 ){// Legend
                            MaestroG.drawLineThickB(g,490,70,530,70, thicknessV2, 3, 3, Color.blue);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup("Re(H","\u22a5","t",")",g,14,535,75);
                            MaestroG.drawLineThickB(g,490,95,530,95, thicknessV2, 3, 3, Color.green.darker());
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup("Im(H","\u22a5","t",")",g,14,535,100);
                        }
                        
                        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;

                            // simple testing with magnitude, not really significant
                            //y2EE = y2 - fieldL*testfieldx.Magnitude();
                            //x2EE = x1 + fieldL*testfieldy.Magnitude();
                            //angolonew = Math.atan(fieldL*(testfieldy.Magnitude())/(fieldL*testfieldx.Magnitude()));   
                            //MaestroG.drawLineThick(g,x2,y2,x2EE,y2EE, thicknessV, colorH);
                            //MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT*Math.sin(angoloT+angolonew),y2EE+LenT*Math.cos(angoloT+angolonew),thicknessV,colorH);
                            //MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenT*Math.sin(angoloT2+angolonew),y2EE+LenT*Math.cos(angoloT2+angolonew),thicknessV,colorH);
                            
                            //-------------------------------------------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 = 15.0;
                            }
                            else{
                                LenT_re = 15.0 * 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, 3, 3, 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);
                                //g.setColor(Color.blue);   
                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //MaestroG.subscripterBsubsup("Re(H","\u22a5","t",")",g,16,(int)(x2EE+lab_pos/2),(int)(y2EE)+lab_pos/2);
                            }
                            //-----------------------------------------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 = 15.0;
                            }
                            else{
                                LenT_im = 15.0 * 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, 3, 3, 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());
                                
                                //g.setColor(Color.blue.darker());   
                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //MaestroG.subscripterBsubsup("Im(H","\u22a5","t",")",g,16,(int)(x2EE+lab_pos/2),(int)(y2EE)-5);
                            }
                            //-------------------------------------------------
                            
                            //An oval for electric field

                            MaestroG.fillCircleThick(g, x2, y2, (double)radiusH, 2, Color.white);
                            MaestroG.drawCircleThick(g, x2, y2, 2.0,4,colorE);
                            MaestroG.drawCircleThick(g, x2, y2, (double)radiusH,2,colorE);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            g.setColor(colorE);                   
                            MaestroG.subscripterBsubsup("E","\u22a5","t","",g,16, (int)x2+5, (int)y2+25);


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

                            // Simple case with magnitude, not significant, just testing
                            //testfieldx = Complex.Multiply(Math.cos(state.theta1),Complex.Add(1.0,state.Reflection_CoefH));
                            //testfieldy = state.Transmission_CoefH;
                            //y2EE = y2 + fieldL * testfieldx.Magnitude();
                            //x2EE = x1 - fieldL * testfieldy.Magnitude();
                            //angolonew = Math.atan(fieldL*(testfieldy.Magnitude())/(fieldL*testfieldx.Magnitude()));   
                            //MaestroG.drawLineThick(g,x2,y2,x2EE,y2EE, thicknessV, colorH);
                            //MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT*Math.sin(angoloT+angolonew),y2EE-LenT*Math.cos(angoloT+angolonew),thicknessV,colorH);
                            //MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenT*Math.sin(angoloT2+angolonew),y2EE-LenT*Math.cos(angoloT2+angolonew),thicknessV,colorH);

                            //----------------------------------------------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 = 15.0;
                            }
                            else{
                                LenT_re = 15.0 * 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, 3, 3, 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);
                                //g.setColor(colorH);                   
                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //MaestroG.subscripterBsubsup("Re(H","\u22a5","t",")",g,16,(int)(x2EE)-lab_pos,(int)(y2EE)-lab_pos/2);
                            }
                            //-----------------------------------------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 = 15.0;
                            }
                            else{
                                LenT_im = 15.0 * 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, 3, 3, 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());
                                
                                //g.setColor(colorH.darker());                   
                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //MaestroG.subscripterBsubsup("Im(H","\u22a5","t",")",g,16,(int)(x2EE)-lab_pos,(int)(y2EE)+lab_pos);
                            }
                            //--------------------------------------------------
                            
                            //An oval for electric field

                             MaestroG.fillCircleThick(g, x2, y2, (double)radiusH, 2, Color.white);

                             MaestroG.drawLineThick(g, x2 - (double)(radiusH/2), y2-(double)(radiusH/2),
                                            x2 + (double)(radiusH/2), y2 + (double)(radiusH/2), 2,colorE);
                             MaestroG.drawLineThick(g, x2+(double)(radiusH/2), y2-(double)(radiusH/2),
                                            x2 - (double)(radiusH/2), y2 + (double)(radiusH/2), 2,colorE);

                             MaestroG.drawCircleThick(g, x2, y2, (double)radiusH,2,colorE);
                             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                             g.setColor(colorE);                   
                             MaestroG.subscripterBsubsup("E","\u22a5","t","",g,16, (int)x2+5, (int)y2+25);
                             
                        }
                    }
                }
                
                if(Field_Refl_Out){ // reflected wave
                    
                        if(ShowREF && state.theta1_deg < 90.0 ){// Legend
                            MaestroG.drawLineThickB(g,20,70,60,70, thicknessV2, 3, 3, Color.blue);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup("Re(H","\u22a5","r",")",g,14,65,75);
                            MaestroG.drawLineThickB(g,20,95,60,95, thicknessV2, 3, 3, Color.green.darker());
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup("Im(H","\u22a5","r",")",g,14,65,100);
                        }
                    
                    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,16,
                                                (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 = 15.0;
                        }
                        else{
                             LenR_re = 15.0 * 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, 3, 3, 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);
                            
                                //g.setColor(colorH); 
                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //MaestroG.subscripterBsubsup("Re(H","\u22a5","r",")",g,16,
                                //                (int)(x2EE - Math.cos(state.theta1)*lab_pos),
                                //                (int)(y2EE - Math.sin(state.theta1)*lab_pos/2));
                            }
                        }
                        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, 3, 3, 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);
                            
                                //g.setColor(colorH);
                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //MaestroG.subscripterBsubsup("Re(H","\u22a5","r",")",g,16,
                                //                (int)(x2EE - Math.cos(state.theta1)*lab_pos),
                                //                (int)(y2EE - Math.sin(state.theta1)*lab_pos/2));
                            }
                        }

                        //------------------------------------------------ IMAGINARY
                        double LenR_im; 
                        if(Math.abs(state.Reflection_CoefH.Imaginary()) > 1.0){
                             LenR_im = 15.0;
                        }
                        else{
                             LenR_im = 15.0 * 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, 3, 3, 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());
                            
                                //g.setColor(colorH.darker());                   
                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //MaestroG.subscripterBsubsup("Im(H","\u22a5","r",")",g,16,
                                //                (int)(x2EE - Math.cos(state.theta1)*lab_pos),
                                //                (int)(y2EE - Math.sin(state.theta1)*lab_pos/2));
                            }
                        }
                        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, 3, 3, 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());
                            
                                //g.setColor(colorH.darker());                   
                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //MaestroG.subscripterBsubsup("Im(H","\u22a5","r",")",g,16,
                                //                (int)(x2EE - Math.cos(state.theta1)*lab_pos),
                                //                (int)(y2EE - Math.sin(state.theta1)*lab_pos/2));
                            }
                        }                 
                    }
                    
                    //An oval for electric field

                    MaestroG.fillCircleThick(g, x22,y22,(double)radiusH, 2, Color.white);
                    MaestroG.drawCircleThick(g, x22,y22,2.0,4,colorE);
                    MaestroG.drawCircleThick(g, x22,y22,(double)radiusH,2,colorE);

                    g.setColor(colorE);                   
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    MaestroG.subscripterBsubsup("E","\u22a5","r","",g,16,(int)x22-lab_pos,(int)y22+lab_pos);
                    
                }
                else{ //Field IN
                    
                        if(ShowREF && state.theta1_deg < 90.0 ){// Legend
                            MaestroG.drawLineThickB(g,20,70,60,70, thicknessV2, 3, 3, Color.blue);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup("Re(H","\u22a5","r",")",g,14,65,75);
                            MaestroG.drawLineThickB(g,20,95,60,95, thicknessV2, 3, 3, Color.green.darker());
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup("Im(H","\u22a5","r",")",g,14,65,100);
                        }
                    
                    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,16,
                                                (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 = 15.0;
                        }
                        else{
                             LenR_re = 15.0 * 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, 3, 3, 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);
                                //g.setColor(colorH);                   
                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //MaestroG.subscripterBsubsup("Re(H","\u22a5","r",")",g,16,
                                //                (int)(x2EE-Math.sin(state.theta1)*lab_pos),
                                //                (int)(y2EE+Math.cos(state.theta1)*lab_pos));
                            }
                        }
                        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, 3, 3, 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);
                            
                                //g.setColor(colorH);                   
                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //MaestroG.subscripterBsubsup("Re(H","\u22a5","r",")",g,16,
                                //                (int)(x2EE-Math.sin(state.theta1)*lab_pos),
                                //                (int)(y2EE+Math.cos(state.theta1)*lab_pos));
                            }
                        }
                        //------------------------------------------------ IMAGINARY
                        double LenR_im; 
                        if(Math.abs(state.Reflection_CoefH.Imaginary()) > 1.0){
                             LenR_im = 15.0;
                        }
                        else{
                             LenR_im = 15.0 * 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, 3, 3, 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());
                                
                                //g.setColor(colorH.darker());                   
                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //MaestroG.subscripterBsubsup("Im(H","\u22a5","r",")",g,16,
                                //                (int)(x2EE-Math.sin(state.theta1)*lab_pos),
                                //                (int)(y2EE+Math.cos(state.theta1)*lab_pos));
                            }
                        }
                        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, 3, 3, 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());
                                
                                //g.setColor(colorH.darker());                   
                                //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                                //MaestroG.subscripterBsubsup("Im(H","\u22a5","r",")",g,16,
                                //                (int)(x2EE-Math.sin(state.theta1)*lab_pos),
                                //                (int)(y2EE+Math.cos(state.theta1)*lab_pos));
                            }
                        }
                    }
                    //----------------------------------------------------------
                    
                    //An oval for electric field

                    MaestroG.fillCircleThick(g, x22, y22, (double)radiusH, 2, Color.white);

                    MaestroG.drawLineThick(g, x22-(double)(radiusH/2), y22-(double)(radiusH/2),
                                    x22 + (double)(radiusH/2), y22 + (double)(radiusH/2), 2,colorE);
                    MaestroG.drawLineThick(g, x22+(double)(radiusH/2), y22-(double)(radiusH/2),
                                    x22 - (double)(radiusH/2), y22 + (double)(radiusH/2), 2,colorE);
                        
                    MaestroG.drawCircleThick(g, x22, y22, (double)radiusH,2,colorE);
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    g.setColor(colorE);                   
                    MaestroG.subscripterBsubsup("E","\u22a5","r","",g,16,(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;
            
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    	
	  //transmitted ray
	  if(state.Reflection_Coef.Imaginary() == 0.0){
              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, thickness, Color.green);}
              MaestroG.drawLineThick(g, x1, y1, x2, y2, thickness, 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)));
          MaestroG.drawLineThick(g, x1, y1-2, x3, y3-2, thickness, Color.green);
          MaestroG.drawLineThick(g, x1, y1-2, x2, y2-2, thickness, 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){}
}
