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(255,255,255);
    private static final Color medium2Color = new Color(220,240,240);
    private static final Color medium3Color = new Color(255,255,230);
    
    private int LeftMargin, RightMargin, TopMargin, BottomMargin;
    private Font labfont = new Font("SanSerif",Font.PLAIN,10);
    private Font axisfont = new Font("Serif",Font.ITALIC | Font.BOLD,18);
    public Font normalfont = new Font("Sanserif",Font.PLAIN,12);
    public Font normalfontB = new Font("Sanserif",Font.BOLD,12);
    public Font smallfont = new Font("Sanserif",Font.PLAIN,10);
    public Font symbfont = new Font("Serif",Font.PLAIN,12);
    private Font labfontbig=new Font("SanSerif",Font.PLAIN,14);
    private Font labfontbigI=new Font("Serif",Font.ITALIC,14);
    private Font labfontbigIB=new Font("Serif",Font.ITALIC | Font.BOLD,14);
    
    private int HGap, VGap;//for vectors fields.
    private Image im;
    private Graphics buf;
    private double extra = 0.1;
    private double thetaG1, thetaG2, thetaG3, thetaG4;
    private Oblique_State state;
    private int radius, radius_power, radius_powernew, scale_radius;
    private boolean IsFocusOn, IsPrintOn;
    public boolean Field_Out, Field_Refl_Out, ShowTR, ShowREF, total;
    private Color incident = Color.red; 
    
    public int cladding_layer;
    public int xsize; 
    public int xsize2; 
    
    public ObliqueCanvas(Oblique_State state){
	super();
	this.state = state;
	
        IsFocusOn = false;
        IsPrintOn = false;
        Field_Out = false;
        Field_Refl_Out = true;
        ShowTR = false;
        ShowREF = false;
        total = false;
        
        labfont = new Font("SanSerif",Font.PLAIN,state.font10);
        axisfont = new Font("Serif",Font.ITALIC | Font.BOLD,state.font18);
        normalfont = new Font("Sanserif",Font.PLAIN,state.font12);
        normalfontB = new Font("Sanserif",Font.BOLD,state.font12);
        smallfont = new Font("Sanserif",Font.PLAIN,state.font10);
        symbfont = new Font("Serif",Font.PLAIN,state.font12);
        labfontbig = new Font("SanSerif",Font.PLAIN,state.font14);
        labfontbigI = new Font("Serif",Font.ITALIC,state.font14);
        labfontbigIB = new Font("Serif",Font.ITALIC | Font.BOLD,state.font14);
    
        LeftMargin = state.s10;
        RightMargin = state.s10;
        TopMargin = state.s10;
        BottomMargin = state.s10;
        
        HGap = state.s10;
        VGap = state.s10;        //for vectors fields.
        scale_radius = state.s150;
        
        setBackground(bgcolor);
	
        radius = getSize().height/2-TopMargin-scale_radius*VGap/20;
        radius_power = radius * 6/10;
        
        //Listeners
	this.addMouseListener(this);
    }
    
    public void drawCanvas(Graphics g){
        cladding_layer = getSize().height/3;
        xsize = 7*getSize().width/8;
        xsize2 = getSize().width/8;
        
        thetaG1 = Math.abs(state.theta1); thetaG2 = Math.abs(state.theta2); 
        thetaG3 = Math.abs(state.theta3); thetaG4 = Math.abs(state.theta4);
	
        g.setColor(Color.black);
	//g.draw3DRect(0,0,getSize().width-1,getSize().height-1,false);
	drawMedium(g);
	drawAxis(g);
        
	drawLabels(g);
        drawBackLabels(g);
        
        
            if(state.theta1 >=0.0){
                if(state.Guiding || state.epsilon_r2 < state.epsilon_r3){
                    drawWaves2(g);
                }
                else{
                    drawNoGuiding2(g);
                }
            }
            else{
                if(state.Guiding || state.epsilon_r2 < state.epsilon_r3){
                    drawWaves3(g);
                }
                else{
                    drawNoGuiding3(g);
                }
            }
        
            drawY(g);
    }
    
    private void drawMedium(Graphics g){/// OK WITH ULABY's BOOK CONVENTIONS
	g.setColor(medium1Color);
	g.fillRect(1,1,xsize2-1,getSize().height-1);
	
        g.setColor(medium2Color);
	g.fillRect(xsize2,1,xsize-1,getSize().height-1);
        
        g.setColor(medium3Color);
	g.fillRect(xsize2,1,xsize-1,cladding_layer);
        g.fillRect(xsize2,2*cladding_layer,xsize-1,cladding_layer-1);
        
        g.setColor(Color.gray);
        g.drawLine(xsize2,cladding_layer,getSize().width,cladding_layer);
        g.drawLine(xsize2,2*cladding_layer,getSize().width,2*cladding_layer);
        
        g.drawLine(getSize().width-state.s40,cladding_layer,getSize().width-state.s40,3*cladding_layer/2);
        drawArrowScaled( getSize().width-state.s40,cladding_layer+state.s10, 1, state.sfactor, g);
        drawArrowScaled( getSize().width-state.s40,3*cladding_layer/2-state.s10, 2,state.sfactor, g);
        
        g.setColor(Color.black);
        g.setFont(labfontbigIB);
        g.drawString("R",getSize().width-state.s35,5*cladding_layer/4+state.s5);
        
        g.setFont(new Font("Sanserif",Font.BOLD,state.font14));
        if(state.Radiates && (state.theta1_deg < 90.0 && state.theta1_deg > -90.0) ){
            g.setColor(Color.red);
            if(state.epsilon_r2 == state.epsilon_r3){
                g.drawString("NO GUIDANCE - Core and Cladding have the same permittivity", state.s200, state.s20);
            }
            else if(state.epsilon_r2 < state.epsilon_r3){
                g.drawString("NO GUIDANCE - Radiating (Leaky) Mode at any angle - Permittivity in Cladding is larger than in Core", state.s200, state.s20);
            }
            else{
                g.drawString("Radiating (Leaky) Mode", state.s200, state.s20);
            }
        }
        else if(state.theta1_deg == 90.0 || state.theta1_deg == -90.0 || total){ 
            g.setColor(Color.red);
            g.drawString("No Propagation", state.s200, state.s20);
        }
        else{  
            g.setColor(Color.red);
            g.drawString("Guided Mode", state.s200, state.s20);
        }
        
        g.setColor(Color.blue);
        MaestroG.subscripterBSym("\u03b8","imax"," = "+MaestroA.rounder(state.theta_range,4)+"\u00ba",g,state.font14,state.s10,11*getSize().height/12);
    }
    
    private void drawAxis(Graphics g){/// OK WITH ULABY's BOOK CONVENTIONS
	g.setColor(Color.black);
	//horizontal axis - z
	g.drawLine(LeftMargin,getSize().height/2,getSize().width-RightMargin-state.s10,getSize().height/2);
	drawArrowScaled( getSize().width-RightMargin-state.s10, getSize().height/2, 3,state.sfactor, g);
        
        //vertical axis - x (pointing up in Ulaby's book)
	g.drawLine(xsize2,TopMargin,xsize2,getSize().height-BottomMargin-state.s10);
        drawArrowScaled( xsize2, TopMargin+state.s10, 1,state.sfactor, g);
    }

    private void drawY(Graphics g){/// OK WITH ULABY's BOOK CONVENTIONS
	// Y axis circle normal to screen
        double x1, y1, radiusy, radiusX, radiusdot; 
        x1 = (double)(xsize2);
        y1 = (double)(getSize().height/2);
        radiusy = (double)state.s7;
        radiusdot = (double)state.s2;
        radiusX = (double)state.s8;
        Color colorX = new Color(100,100,100);
               
        int rule; float alpha;
        rule = AlphaComposite.SRC_OVER;
        alpha = 1.0f;
        
        Graphics2D g2d = (Graphics2D)g;
        g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
        MaestroG.fillCircleThick(g, x1, y1,radiusy,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,xsize2+2*fm.stringWidth(tmp)-state.s5,fm.getHeight());
	tmp = "z";
	g.drawString(tmp,getSize().width-RightMargin-fm.stringWidth(tmp)-state.s5,getSize().height/2+fm.getHeight());
	tmp = "y";
	if(state.theta1 >=0.0){g.drawString(tmp,(xsize2+fm.stringWidth(tmp)),getSize().height/2+fm.getHeight());}
        else{g.drawString(tmp,(xsize2+fm.stringWidth(tmp)),getSize().height/2-fm.getHeight()+state.s5);}
	
        g.setFont(normalfontB);
        g.setColor(Color.red.darker());
	if(state.epsilon_r1 > 1.0){
            tmp =  "medium 1";
        }
        else{
            tmp =  "medium 1 - Air";
        }
	//g.drawString(tmp,LeftMargin +10,getSize().height-4*VGap);
        g.drawString(tmp,LeftMargin + state.s10,2*VGap);
        
	g.setColor(Color.blue.darker());
	tmp  = "medium 2 - Core";
	//g.drawString(tmp,xsize2+40,2*cladding_layer-VGap/2);
        g.drawString(tmp,getSize().width-state.s200,cladding_layer+3*VGap/2);
        
        g.setColor(Color.black);
	tmp  = "medium 3 - Cladding";
	g.drawString(tmp,getSize().width - state.s200,getSize().height-4*VGap);
        g.drawString(tmp,getSize().width - state.s200,state.s20);
	
        int x0 = getSize().width/8 + state.s50;
        int y0 = getSize().height-state.s15;
	g.setFont(smallfont);
        g.setColor(Color.red.darker());
        
        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-state.s2,y0);
        
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    }
    
    private void drawBackLabels(Graphics g){/// DONE
	//cladding_layer = getSize().height/3;
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        int mediumX, mediumY;
	//radius = getSize().height/2-TopMargin-scale_radius*VGap/20;
	FontMetrics fm = g.getFontMetrics();
	//For medium 1
	mediumX = LeftMargin+state.s5;
	//mediumY = getSize().height-2*VGap;
        mediumY = 4*VGap;
	
        g.setColor(Color.red.darker());
	// permittivity
        if(state.Show_index){
            MaestroG.subscripter("n","0"," = "+MaestroA.rounder(state.index1,6),g,state.font14,mediumX+state.s7,mediumY);
        }
        else{
            MaestroG.subscripter("\u03b5","r0"," = "+MaestroA.rounder(state.epsilon_r1,3),g,state.font14,mediumX+state.s7,mediumY);
        } 
        
        //For medium 2
	g.setColor(Color.blue.darker());
	//mediumX = xsize2+40;
        //mediumY = 2*cladding_layer+3*VGap/2;
        mediumX = getSize().width - state.s200;
        mediumY = cladding_layer+7*VGap/2;
        if(state.Show_index){
            MaestroG.subscripter("n","f"," = "+MaestroA.rounder(state.index2,6),g,state.font14,mediumX,mediumY);
        }
        else{
            MaestroG.subscripter("\u03b5","rf"," = "+MaestroA.rounder(state.epsilon_r2,3),g,state.font14,mediumX,mediumY);
        } 
	
	//For medium 3
	g.setColor(Color.black);
	mediumX = getSize().width-state.s200;
        mediumY = getSize().height-2*VGap;
        if(state.Show_index){
            MaestroG.subscripter("n","c"," = "+MaestroA.rounder(state.index3,6),g,state.font14,mediumX,mediumY);
            MaestroG.subscripter("n","c"," = "+MaestroA.rounder(state.index3,6),g,state.font14,mediumX,state.s20+2*VGap);
        }
        else{
            MaestroG.subscripter("\u03b5","rc"," = "+MaestroA.rounder(state.epsilon_r3,3),g,state.font14,mediumX,mediumY);
            MaestroG.subscripter("\u03b5","rc"," = "+MaestroA.rounder(state.epsilon_r3,3),g,state.font14,mediumX,state.s20+2*VGap);
        }
        
	g.setFont(normalfont); 
	fm = g.getFontMetrics(); 
    }
    
    //==========================================================================
    //  epsilon in core = epsilon in cladding --- NO GUIDANCE
    //==========================================================================
    private void drawNoGuiding2(Graphics g){
        
        //cladding_layer = getSize().height/3;
        double x1, y1, x2, y2;
        int thickness = state.s6;
        int lab_pos = state.s28;
        
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
	boolean Scaled_Waves = true;
	radius = getSize().height/2-TopMargin-scale_radius*VGap/60;
        radius_powernew = 93*radius/233;
        
	g.setColor(incident);
	//incident ray
	
        double Len = (double)state.s15;
        double radiusV, fieldL, x2_power, y2_power;
        int thicknessV, thicknessV2, thicknessV3, radiusH;
        double angolo, angolo2, angolobeta, angolobeta2, angolobetaT, angolobetaT2,x2V, y2V, x2E, y2E;
        Color colorV = Color.black;
        Color colorE = Color.magenta.darker();
        Color colorH = Color.blue;
        
        angolobeta2 = (-thetaG1+Math.PI)+23.0*Math.PI/180.0; 
        angolobeta = (-thetaG1+Math.PI)-23.0*Math.PI/180.0;  
        angolobetaT2 = (-thetaG2+Math.PI)+23.0*Math.PI/180.0; 
        angolobetaT = (-thetaG2+Math.PI)-23.0*Math.PI/180.0;  
        
        angolo2 = (thetaG1+Math.PI)+23.0*Math.PI/180.0; 
        angolo = (thetaG1+Math.PI)-23.0*Math.PI/180.0;  
        
        radiusV = radius * 0.5;
        fieldL = 1.1*(radius * 0.4);
        thicknessV = state.s3;
        thicknessV2 = state.s2;
        thicknessV3 = 0;
        radiusH = state.s8;
        
        // center coordinates
          x1 = (double)(xsize2);
          y1 = (double)(getSize().height/2);
        
        // coordinates of beginning of incident line
          x2 = (double)(x1+(int)(radius*Math.cos(Math.PI - thetaG1)));
          y2 = (double)(y1+(int)(radius*Math.sin(Math.PI - thetaG1)));
          
          x2_power = (double)(x1-(int)(radius_powernew*Math.cos(thetaG1)));
          y2_power = (double)(y1+(int)(radius_powernew*Math.sin(thetaG1)));
          
          double xo, yo, ango, ango2, ango3, rado;
          xo = x1 - (double)state.s15;
          yo = y1 - (double)state.s50;
          
          ango = Math.PI + thetaG1*0.5;
          ango3 = Math.PI - thetaG1*0.5;
          
          ango2 = thetaG2*0.5;
          
          // Represent angle
          // incident wave
          
          double arco = (double)state.s30;
          rado = 3.0*arco/2.0;
          
          // entrance angle arc
          
          //MaestroG.FillArcTransp(g,x1-2*arco,y1-2*arco,4.0*arco,180.0-state.theta_range,2.0*state.theta_range, Color.pink, 0.4);
          MaestroG.FillArcTransp(g,x1-36*arco/10,y1-36*arco/10,7.2*arco,180.0-state.theta_range,2.0*state.theta_range, Color.pink, 0.1);
          MaestroG.drawLineThickB(g,x1,y1,x1-4*arco*Math.cos(Math.PI*state.theta_range/180),y1+4*arco*Math.sin(Math.PI*state.theta_range/180),1,5,5,Color.black);
          MaestroG.drawLineThickB(g,x1,y1,x1-4*arco*Math.cos(Math.PI*state.theta_range/180),y1-4*arco*Math.sin(Math.PI*state.theta_range/180),1,5,5,Color.black);
          MaestroG.drawArcThick(g, x1-36*arco/10, y1-36*arco/10,7.2*arco, 180.0-state.theta_range,2.0*state.theta_range, 1, Color.gray);
          
          if(180.0/Math.PI*thetaG1 > 2.01 ){// to remove a little numerical glitch around 1.0 degree
                MaestroG.FillArcTransp(g,x1-arco,y1-arco,2.0*arco,180.0,180.0/Math.PI*thetaG1, Color.orange, 0.2);
                MaestroG.drawArcThick(g, x1-arco, y1-arco, 2.0*arco, 180.0, 180.0/Math.PI*thetaG1, 3, Color.orange.darker());
          }
          // "alpha" entrance angle in air
          
          //g.setColor(Color.orange.darker());
          g.setColor(Color.black);
          if(180.0/Math.PI*thetaG1 >= 25.0){
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter3("\u03b8","i","","","",g,state.font18,(int)(x1+rado*Math.cos(ango))-state.s5, (int)(y1+state.s2-rado*Math.sin(ango)));
          }
          else{
              //MaestroG.drawArcThick(g, x1-40.0, y1-40.0, 80.0, 180.0, 180.0/Math.PI*thetaG1, 3, Color.orange.darker());
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter3("\u03b8","i","","","",g,state.font18,(int)x1-(int)(50.0*Math.cos(thetaG1+Math.PI/7.0)),
                                                            (int)y1-state.s5+(int)(50.0*Math.sin(thetaG1+Math.PI/7.0)));
          }
          
          // "theta_i" entrance angle in glass  
          if(Math.abs(state.theta1_deg) < 90.0){
              if(state.Reflection_Coef.Imaginary() == 0.0 ){
                  total = false;
                  // transmitted wave
                  MaestroG.FillArcTransp(g,x1-arco,y1-arco,2.0*arco,0.0,180.0/Math.PI*thetaG2, Color.yellow, 0.5);
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  MaestroG.drawArcThick(g, x1-arco, y1-arco, 2.0*arco, 0.0, 180.0/Math.PI*thetaG2, 3, Color.orange);
                  g.setColor(Color.black);
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  MaestroG.subscripter3("\u03b8","2","","","",g,state.font18,(int)x1-state.s12+(int)((double)state.s50*Math.cos(thetaG2+Math.PI/7.0)),
                                                                (int)y1+state.s13-(int)((double)state.s50*Math.sin(thetaG2+Math.PI/7.0)));
                  
                  MaestroG.drawLineThickB(g,x1,y1,x1 + (getSize().width)*Math.cos(thetaG2),y1 - (getSize().width)*Math.sin(thetaG2),1,5,5,Color.black);
              }
              else{ // Total Reflection
                  total = true;
                  g.setColor(Color.red.darker());
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  MaestroG.subscripter("\u03b8","i"," > Critical Angle between Medium 1 and Medium 2",g,state.font14,state.s300+state.s30,state.s20);
                  MaestroG.subscripter("(Total Reflection)","","",g,state.font14,state.s300+state.s30,state.s40);

                  MaestroG.FillArcTransp(g,x1-arco,y1-arco,2.0*arco, 0.0, 90.0, Color.yellow, 0.5);
                  MaestroG.drawArcThick(g, x1-arco, y1-arco,2.0*arco, 0.0, 90.0, 3, Color.orange); 
                  g.setColor(Color.black);
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  MaestroG.subscripter("90","","\u00ba",g,state.font14,(int)(x1+rado*Math.sin(Math.PI*0.25))-state.s5, (int)(y1-rado*Math.cos(Math.PI*0.25)));
              }
              
          }
          
        // coordinates of incident vector tip
          x2V = (double)(x1+(int)(radiusV*Math.cos(Math.PI - thetaG1)));
          y2V = (double)(y1+(int)(radiusV*Math.sin(Math.PI - thetaG1)));
          
          drawAxis(g);
          
          //vectors
          //---------------------------------------------------------------
          // incident beta vector
          // arrow tip
          double Len2 = (double)state.s15;
          MaestroG.drawLineThick(g,x2V,y2V,x2V+Len2*Math.cos(angolobeta),y2V+Len2*Math.sin(angolobeta),thicknessV,colorV);
          MaestroG.drawLineThick(g,x2V,y2V,x2V+Len2*Math.cos(angolobeta2),y2V+Len2*Math.sin(angolobeta2)+state.s1,thicknessV,colorV);
            // stem
          MaestroG.drawLineThick(g,x1,y1,x2,y2,1,Color.gray);
          MaestroG.drawLineThick(g,x2,y2,x2V,y2V,thicknessV,colorV);
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
          
          //MaestroG.subscripterB("k","i","",g,18,(int)(x2+x2V)/2-20,(int)(y2+y2V)/2-15); // Ulaby's book notation
          
	  //if(!total){drawRefTrans2(g, incident, thickness);}
	
    }
        
    private void drawNoGuiding3(Graphics g){
        
        //cladding_layer = getSize().height/3;
        double x1, y1, x2, y2;
        int thickness = state.s6;
        int lab_pos = state.s28;
        
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
	boolean Scaled_Waves = true;
	radius = getSize().height/2-TopMargin-scale_radius*VGap/60;
        radius_powernew = 93*radius/233;
        
	g.setColor(incident);
	//incident ray
	
        double Len = (double)state.s15;
        double radiusV, fieldL, x2_power, y2_power;
        int thicknessV, thicknessV2, thicknessV3, radiusH;
        double angolo, angolo2, angolobeta, angolobeta2, angolobetaT, angolobetaT2,x2V, y2V, x2E, y2E;
        Color colorV = Color.black;
        Color colorE = Color.magenta.darker();
        Color colorH = Color.blue;
        
        angolobeta2 = (thetaG1+Math.PI)+23.0*Math.PI/180.0; 
        angolobeta = (thetaG1+Math.PI)-23.0*Math.PI/180.0;  
        angolobetaT2 = (thetaG2+Math.PI)+23.0*Math.PI/180.0; 
        angolobetaT = (thetaG2+Math.PI)-23.0*Math.PI/180.0;  
        
        angolo2 = (-thetaG1+Math.PI)+23.0*Math.PI/180.0; 
        angolo = (-thetaG1+Math.PI)-23.0*Math.PI/180.0;  
        
        radiusV = radius * 0.5;
        fieldL = 1.1*(radius * 0.4);
        thicknessV = state.s3;
        thicknessV2 = state.s2;
        thicknessV3 = 0;
        radiusH = state.s8;
        
        // center coordinates
          x1 = (double)(xsize2);
          y1 = (double)(getSize().height/2);
        
        // coordinates of beginning of incident line
          x2 = (double)(x1+(int)(radius*Math.cos(-Math.PI + thetaG1)));
          y2 = (double)(y1+(int)(radius*Math.sin(-Math.PI + thetaG1)));
          
          x2_power = (double)(x1-(int)(radius_powernew*Math.cos(thetaG1)));
          y2_power = (double)(y1+(int)(radius_powernew*Math.sin(thetaG1)));
          
          double xo, yo, ango, ango2, ango3, rado;
          xo = x1 - (double)state.s15;
          yo = y1 - (double)state.s50;
          
          ango = Math.PI - thetaG1*0.5;
          ango3 = Math.PI + thetaG1*0.5;
          
          ango2 = -thetaG2*0.5;
          
          // Represent angle
          // incident wave
          
          double arco = (double)state.s30;
          rado = 3.0*arco/2.0;
          
          // entrance angle arc
          
          MaestroG.FillArcTransp(g,x1-36*arco/10,y1-36*arco/10,7.2*arco,180.0-state.theta_range,2.0*state.theta_range, Color.pink, 0.1);
          MaestroG.drawLineThickB(g,x1,y1,x1-4*arco*Math.cos(Math.PI*state.theta_range/180),y1+4*arco*Math.sin(Math.PI*state.theta_range/180),1,5,5,Color.black);
          MaestroG.drawLineThickB(g,x1,y1,x1-4*arco*Math.cos(Math.PI*state.theta_range/180),y1-4*arco*Math.sin(Math.PI*state.theta_range/180),1,5,5,Color.black);
          MaestroG.drawArcThick(g, x1-36*arco/10, y1-36*arco/10, 7.2*arco, 180.0-state.theta_range,2.0*state.theta_range, 1, Color.gray);
          
          if(Math.abs(180.0/Math.PI*thetaG1) > 2.01 ){// to remove a little numerical glitch around 1.0 degree
                MaestroG.FillArcTransp(g,x1-arco,y1-arco,2.0*arco,180.0,-180.0/Math.PI*Math.abs(thetaG1), Color.orange, 0.2);
                MaestroG.drawArcThick(g, x1-arco, y1-arco, 2.0*arco, 180.0, -180.0/Math.PI*Math.abs(thetaG1), 3, Color.orange.darker());
          }
          // "alpha" entrance angle in air
          
          //g.setColor(Color.orange.darker());
          g.setColor(Color.black);
          if(180.0/Math.PI*thetaG1 >= 25.0){
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter3("\u03b8","i","","","",g,state.font18,(int)(x1+rado*Math.cos(ango))-state.s5, (int)(y1+state.s2-rado*Math.sin(ango))+state.s5);
          }
          else{
              //MaestroG.drawArcThick(g, x1-40.0, y1-40.0, 80.0, 180.0, 180.0/Math.PI*thetaG1, 3, Color.orange.darker());
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter3("\u03b8","i","","","",g,state.font18,(int)x1-(int)(50.0*Math.cos(thetaG1+Math.PI/7.0)),
                                                            (int)y1-(int)(25.0*Math.sin(thetaG1+Math.PI/7.0)));
          }
          
          // "theta_2" entrance angle in glass  
          if(Math.abs(state.theta1_deg) < 90.0){
              if(state.Reflection_Coef.Imaginary() == 0.0 ){
                    total = false;
                    // transmitted wave
                    MaestroG.FillArcTransp(g,x1-arco,y1-arco,2.0*arco,0.0,-180.0/Math.PI*thetaG2, Color.yellow, 0.5);
                    MaestroG.drawArcThick(g, x1-arco, y1-arco, 2.0*arco, 0.0, -180.0/Math.PI*thetaG2, 3, Color.orange);
                    g.setColor(Color.black);
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    MaestroG.subscripter3("\u03b8","2","","","",g,state.font18,(int)x1-12+(int)((double)state.s50*Math.cos(thetaG2+Math.PI/7.0)),
                                                        (int)y1-state.s5+(int)((double)state.s50*Math.sin(thetaG2+Math.PI/7.0)));
                    
                    MaestroG.drawLineThickB(g,x1,y1,x1 + (getSize().width)*Math.cos(thetaG2),y1 + (getSize().width)*Math.sin(thetaG2),1,5,5,Color.black);
              }
                
              else{ // Total Reflection
                    total = true;
                    g.setColor(Color.red.darker());
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    MaestroG.subscripter("\u03b8","i"," > Critical Angle between Medium 1 and Medium 2",g,state.font14,state.s300+state.s30,state.s20);
                    MaestroG.subscripter("(Total Reflection)","","",g,state.font14,state.s300+state.s30,state.s40);

                    MaestroG.FillArcTransp(g,x1-arco,y1-arco,2.0*arco, 0.0,-90.0, Color.yellow, 0.5);
                    MaestroG.drawArcThick(g, x1-arco, y1-arco, 2.0*arco, 0.0, -90.0, 3, Color.orange); 
                    g.setColor(Color.black);
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    MaestroG.subscripter("90","","\u00ba",g,state.font14,(int)(x1+rado*Math.sin(Math.PI*0.25))-state.s5, (int)(y1+rado*Math.cos(Math.PI*0.25)));
              }
          }
          
        // coordinates of incident vector tip
          x2V = (double)(x1+(int)(radiusV*Math.cos(-Math.PI + thetaG1)));
          y2V = (double)(y1+(int)(radiusV*Math.sin(-Math.PI + thetaG1)));
          
          drawAxis(g);
          
          //vectors
          //---------------------------------------------------------------
          // incident beta vector
          // arrow tip
          double Len2 = (double)state.s15;
          MaestroG.drawLineThick(g,x2V,y2V,x2V+Len2*Math.cos(angolobeta),y2V+Len2*Math.sin(angolobeta),thicknessV,colorV);
          MaestroG.drawLineThick(g,x2V,y2V,x2V+Len2*Math.cos(angolobeta2),y2V+Len2*Math.sin(angolobeta2)+state.s1,thicknessV,colorV);
            // stem
          MaestroG.drawLineThick(g,x1,y1,x2,y2,1,Color.gray);
          MaestroG.drawLineThick(g,x2,y2,x2V,y2V,thicknessV,colorV);
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
          
          //if(!total){drawRefTrans3(g, incident, thickness);}
	
    }
    
    //==========================================================================
    
    
    //------------------------------------------------------------------------------------------    
    
        private void drawWaves2(Graphics g){
        
        //cladding_layer = getSize().height/3;
        double x1, y1, x2, y2;
        int thickness = state.s6;
        int lab_pos = state.s28;
        
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
	boolean Scaled_Waves = true;
	radius = getSize().height/2-TopMargin-scale_radius*VGap/60;
        radius_powernew = 93*radius/233;
        
	g.setColor(incident);
	//incident ray
	
        double Len = (double)state.s15;
        double radiusV, fieldL, x2_power, y2_power;
        int thicknessV, thicknessV2, thicknessV3, radiusH;
        double angolo, angolo2, angolobeta, angolobeta2, angolobetaT, angolobetaT2,x2V, y2V, x2E, y2E;
        Color colorV = Color.black;
        Color colorE = Color.magenta.darker();
        Color colorH = Color.blue;
        
        angolobeta2 = (-thetaG1+Math.PI)+23.0*Math.PI/180.0; 
        angolobeta = (-thetaG1+Math.PI)-23.0*Math.PI/180.0;  
        angolobetaT2 = (-thetaG2+Math.PI)+23.0*Math.PI/180.0; 
        angolobetaT = (-thetaG2+Math.PI)-23.0*Math.PI/180.0;  
        
        angolo2 = (thetaG1+Math.PI)+23.0*Math.PI/180.0; 
        angolo = (thetaG1+Math.PI)-23.0*Math.PI/180.0;  
        
        radiusV = radius * 0.5;
        fieldL = 1.1*(radius * 0.4);
        thicknessV = state.s3;
        thicknessV2 = state.s2;
        thicknessV3 = 0;
        radiusH = state.s8;
        
        // center coordinates
          x1 = (double)(xsize2);
          y1 = (double)(getSize().height/2);
        
        // coordinates of beginning of incident line
          x2 = (double)(x1+(int)(radius*Math.cos(Math.PI - thetaG1)));
          y2 = (double)(y1+(int)(radius*Math.sin(Math.PI - thetaG1)));
          
          x2_power = (double)(x1-(int)(radius_powernew*Math.cos(thetaG1)));
          y2_power = (double)(y1+(int)(radius_powernew*Math.sin(thetaG1)));
          
          double xo, yo, ango, ango2, ango3, rado;
          xo = x1 - (double)state.s15;
          yo = y1 - (double)state.s50;
          
          ango = Math.PI + thetaG1*0.5;
          ango3 = Math.PI - thetaG1*0.5;
          
          ango2 = thetaG2*0.5;
          
          // Represent angle
          // incident wave
          
          double arco = (double)state.s30;
          rado = 3.0*arco/2.0;
          
          // entrance angle arc
          
          //MaestroG.FillArcTransp(g,x1-2*arco,y1-2*arco,4.0*arco,180.0-state.theta_range,2.0*state.theta_range, Color.pink, 0.4);
          MaestroG.FillArcTransp(g,x1-36*arco/10,y1-36*arco/10,7.2*arco,180.0-state.theta_range,2.0*state.theta_range, Color.pink, 0.1);
          MaestroG.drawLineThickB(g,x1,y1,x1-4*arco*Math.cos(Math.PI*state.theta_range/180),y1+4*arco*Math.sin(Math.PI*state.theta_range/180),1,5,5,Color.black);
          MaestroG.drawLineThickB(g,x1,y1,x1-4*arco*Math.cos(Math.PI*state.theta_range/180),y1-4*arco*Math.sin(Math.PI*state.theta_range/180),1,5,5,Color.black);
          MaestroG.drawArcThick(g, x1-36*arco/10, y1-36*arco/10,7.2*arco, 180.0-state.theta_range,2.0*state.theta_range, 1, Color.gray);
          
          if(180.0/Math.PI*thetaG1 > 2.01 ){// to remove a little numerical glitch around 1.0 degree
                MaestroG.FillArcTransp(g,x1-arco,y1-arco,2.0*arco,180.0,180.0/Math.PI*thetaG1, Color.orange, 0.2);
                MaestroG.drawArcThick(g, x1-arco, y1-arco, 2.0*arco, 180.0, 180.0/Math.PI*thetaG1, 3, Color.orange.darker());
          }
          // "alpha" entrance angle in air
          
          //g.setColor(Color.orange.darker());
          g.setColor(Color.black);
          if(180.0/Math.PI*thetaG1 >= 25.0){
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter3("\u03b8","i","","","",g,state.font18,(int)(x1+rado*Math.cos(ango))-state.s5, (int)(y1+state.s2-rado*Math.sin(ango)));
          }
          else{
              //MaestroG.drawArcThick(g, x1-40.0, y1-40.0, 80.0, 180.0, 180.0/Math.PI*thetaG1, 3, Color.orange.darker());
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter3("\u03b8","i","","","",g,state.font18,(int)x1-(int)(50.0*Math.cos(thetaG1+Math.PI/7.0)),
                                                            (int)y1-state.s5+(int)(50.0*Math.sin(thetaG1+Math.PI/7.0)));
          }
          
          // "theta_i" entrance angle in glass  
          if(Math.abs(state.theta1_deg) < 90.0){
              if(state.Reflection_Coef.Imaginary() == 0.0 ){
                  total = false;
                  // transmitted wave
                  MaestroG.FillArcTransp(g,x1-arco,y1-arco,2.0*arco,0.0,180.0/Math.PI*thetaG2, Color.yellow, 0.5);
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  MaestroG.drawArcThick(g, x1-arco, y1-arco, 2.0*arco, 0.0, 180.0/Math.PI*thetaG2, 3, Color.orange);
                  g.setColor(Color.black);
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  MaestroG.subscripter3("\u03b8","2","","","",g,state.font18,(int)x1-state.s12+(int)((double)state.s50*Math.cos(thetaG2+Math.PI/7.0)),
                                                                (int)y1+state.s13-(int)((double)state.s50*Math.sin(thetaG2+Math.PI/7.0)));
              }
              else{ // Total Reflection
                  total = true;
                  g.setColor(Color.red.darker());
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  MaestroG.subscripter("\u03b8","i"," > Critical Angle between Medium 1 and Medium 2",g,state.font14,state.s300+state.s30,state.s20);
                  MaestroG.subscripter("(Total Reflection)","","",g,state.font14,state.s300+state.s30,state.s40);

                  MaestroG.FillArcTransp(g,x1-arco,y1-arco,2.0*arco, 0.0, 90.0, Color.yellow, 0.5);
                  MaestroG.drawArcThick(g, x1-arco, y1-arco,2.0*arco, 0.0, 90.0, 3, Color.orange); 
                  g.setColor(Color.black);
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  MaestroG.subscripter("90","","\u00ba",g,state.font14,(int)(x1+rado*Math.sin(Math.PI*0.25))-state.s5, (int)(y1-rado*Math.cos(Math.PI*0.25)));
              }
              
          }
          
        // coordinates of incident vector tip
          x2V = (double)(x1+(int)(radiusV*Math.cos(Math.PI - thetaG1)));
          y2V = (double)(y1+(int)(radiusV*Math.sin(Math.PI - thetaG1)));
          
          drawAxis(g);
          
          //vectors
          //---------------------------------------------------------------
          // incident beta vector
          // arrow tip
          double Len2 = (double)state.s15;
          MaestroG.drawLineThick(g,x2V,y2V,x2V+Len2*Math.cos(angolobeta),y2V+Len2*Math.sin(angolobeta),thicknessV,colorV);
          MaestroG.drawLineThick(g,x2V,y2V,x2V+Len2*Math.cos(angolobeta2),y2V+Len2*Math.sin(angolobeta2)+state.s1,thicknessV,colorV);
            // stem
          MaestroG.drawLineThick(g,x1,y1,x2,y2,1,Color.gray);
          MaestroG.drawLineThick(g,x2,y2,x2V,y2V,thicknessV,colorV);
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
          
          //MaestroG.subscripterB("k","i","",g,18,(int)(x2+x2V)/2-20,(int)(y2+y2V)/2-15); // Ulaby's book notation
          
	  if(!total){drawRefTrans2(g, incident, thickness);}
	
    }
        
    private void drawWaves3(Graphics g){
        
        //cladding_layer = getSize().height/3;
        double x1, y1, x2, y2;
        int thickness = state.s6;
        int lab_pos = state.s28;
        
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
	boolean Scaled_Waves = true;
	radius = getSize().height/2-TopMargin-scale_radius*VGap/60;
        radius_powernew = 93*radius/233;
        
	g.setColor(incident);
	//incident ray
	
        double Len = (double)state.s15;
        double radiusV, fieldL, x2_power, y2_power;
        int thicknessV, thicknessV2, thicknessV3, radiusH;
        double angolo, angolo2, angolobeta, angolobeta2, angolobetaT, angolobetaT2,x2V, y2V, x2E, y2E;
        Color colorV = Color.black;
        Color colorE = Color.magenta.darker();
        Color colorH = Color.blue;
        
        angolobeta2 = (thetaG1+Math.PI)+23.0*Math.PI/180.0; 
        angolobeta = (thetaG1+Math.PI)-23.0*Math.PI/180.0;  
        angolobetaT2 = (thetaG2+Math.PI)+23.0*Math.PI/180.0; 
        angolobetaT = (thetaG2+Math.PI)-23.0*Math.PI/180.0;  
        
        angolo2 = (-thetaG1+Math.PI)+23.0*Math.PI/180.0; 
        angolo = (-thetaG1+Math.PI)-23.0*Math.PI/180.0;  
        
        radiusV = radius * 0.5;
        fieldL = 1.1*(radius * 0.4);
        thicknessV = state.s3;
        thicknessV2 = state.s2;
        thicknessV3 = 0;
        radiusH = state.s8;
        
        // center coordinates
          x1 = (double)(xsize2);
          y1 = (double)(getSize().height/2);
        
        // coordinates of beginning of incident line
          x2 = (double)(x1+(int)(radius*Math.cos(-Math.PI + thetaG1)));
          y2 = (double)(y1+(int)(radius*Math.sin(-Math.PI + thetaG1)));
          
          x2_power = (double)(x1-(int)(radius_powernew*Math.cos(thetaG1)));
          y2_power = (double)(y1+(int)(radius_powernew*Math.sin(thetaG1)));
          
          double xo, yo, ango, ango2, ango3, rado;
          xo = x1 - (double)state.s15;
          yo = y1 - (double)state.s50;
          
          ango = Math.PI - thetaG1*0.5;
          ango3 = Math.PI + thetaG1*0.5;
          
          ango2 = -thetaG2*0.5;
          
          // Represent angle
          // incident wave
          
          double arco = (double)state.s30;
          rado = 3.0*arco/2.0;
          
          // entrance angle arc
          
          MaestroG.FillArcTransp(g,x1-36*arco/10,y1-36*arco/10,7.2*arco,180.0-state.theta_range,2.0*state.theta_range, Color.pink, 0.1);
          MaestroG.drawLineThickB(g,x1,y1,x1-4*arco*Math.cos(Math.PI*state.theta_range/180),y1+4*arco*Math.sin(Math.PI*state.theta_range/180),1,5,5,Color.black);
          MaestroG.drawLineThickB(g,x1,y1,x1-4*arco*Math.cos(Math.PI*state.theta_range/180),y1-4*arco*Math.sin(Math.PI*state.theta_range/180),1,5,5,Color.black);
          MaestroG.drawArcThick(g, x1-36*arco/10, y1-36*arco/10, 7.2*arco, 180.0-state.theta_range,2.0*state.theta_range, 1, Color.gray);
          
          if(Math.abs(180.0/Math.PI*thetaG1) > 2.01 ){// to remove a little numerical glitch around 1.0 degree
                MaestroG.FillArcTransp(g,x1-arco,y1-arco,2.0*arco,180.0,-180.0/Math.PI*Math.abs(thetaG1), Color.orange, 0.2);
                MaestroG.drawArcThick(g, x1-arco, y1-arco, 2.0*arco, 180.0, -180.0/Math.PI*Math.abs(thetaG1), 3, Color.orange.darker());
          }
          // "alpha" entrance angle in air
          
          //g.setColor(Color.orange.darker());
          g.setColor(Color.black);
          if(180.0/Math.PI*thetaG1 >= 25.0){
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter3("\u03b8","i","","","",g,state.font18,(int)(x1+rado*Math.cos(ango))-state.s5, (int)(y1+state.s2-rado*Math.sin(ango))+state.s5);
          }
          else{
              //MaestroG.drawArcThick(g, x1-40.0, y1-40.0, 80.0, 180.0, 180.0/Math.PI*thetaG1, 3, Color.orange.darker());
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter3("\u03b8","i","","","",g,state.font18,(int)x1-(int)(50.0*Math.cos(thetaG1+Math.PI/7.0)),
                                                            (int)y1-(int)(25.0*Math.sin(thetaG1+Math.PI/7.0)));
          }
          
          // "theta_i" entrance angle in glass  
          if(Math.abs(state.theta1_deg) < 90.0){
              if(state.Reflection_Coef.Imaginary() == 0.0 ){
                    total = false;
                    // transmitted wave
                    MaestroG.FillArcTransp(g,x1-arco,y1-arco,2.0*arco,0.0,-180.0/Math.PI*thetaG2, Color.yellow, 0.5);
                    MaestroG.drawArcThick(g, x1-arco, y1-arco, 2.0*arco, 0.0, -180.0/Math.PI*thetaG2, 3, Color.orange);
                    g.setColor(Color.black);
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    MaestroG.subscripter3("\u03b8","2","","","",g,state.font18,(int)x1-12+(int)((double)state.s50*Math.cos(thetaG2+Math.PI/7.0)),
                                                        (int)y1-state.s5+(int)((double)state.s50*Math.sin(thetaG2+Math.PI/7.0)));
              }
                
              else{ // Total Reflection
                    total = true;
                    g.setColor(Color.red.darker());
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    MaestroG.subscripter("\u03b8","i"," > Critical Angle between Medium 1 and Medium 2",g,state.font14,state.s300+state.s30,state.s20);
                    MaestroG.subscripter("(Total Reflection)","","",g,state.font14,state.s300+state.s30,state.s40);

                    MaestroG.FillArcTransp(g,x1-arco,y1-arco,2.0*arco, 0.0,-90.0, Color.yellow, 0.5);
                    MaestroG.drawArcThick(g, x1-arco, y1-arco, 2.0*arco, 0.0, -90.0, 3, Color.orange); 
                    g.setColor(Color.black);
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    MaestroG.subscripter("90","","\u00ba",g,state.font14,(int)(x1+rado*Math.sin(Math.PI*0.25))-state.s5, (int)(y1+rado*Math.cos(Math.PI*0.25)));
              }
          }
          
        // coordinates of incident vector tip
          x2V = (double)(x1+(int)(radiusV*Math.cos(-Math.PI + thetaG1)));
          y2V = (double)(y1+(int)(radiusV*Math.sin(-Math.PI + thetaG1)));
          
          drawAxis(g);
          
          //vectors
          //---------------------------------------------------------------
          // incident beta vector
          // arrow tip
          double Len2 = (double)state.s15;
          MaestroG.drawLineThick(g,x2V,y2V,x2V+Len2*Math.cos(angolobeta),y2V+Len2*Math.sin(angolobeta),thicknessV,colorV);
          MaestroG.drawLineThick(g,x2V,y2V,x2V+Len2*Math.cos(angolobeta2),y2V+Len2*Math.sin(angolobeta2)+state.s1,thicknessV,colorV);
            // stem
          MaestroG.drawLineThick(g,x1,y1,x2,y2,1,Color.gray);
          MaestroG.drawLineThick(g,x2,y2,x2V,y2V,thicknessV,colorV);
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
          
          if(!total){drawRefTrans3(g, incident, thickness);}
	
    }
    
    //-------------------------------------------------------------------------------------------
    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){
        //cladding_layer = getSize().height/3;
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        int lab_pos = state.s28;
        double x1, y1, x2, y2, x2R, y2R, x2T, y2T;
        double angolobetaT, angolobetaT2;
        angolobetaT2 = (-thetaG2+Math.PI)+23.0*Math.PI/180.0; // THIS IS GOOD!!!
        angolobetaT = (-thetaG2+Math.PI)-23.0*Math.PI/180.0;  //     "   "
        
        radius = getSize().height/2-TopMargin-scale_radius*VGap/60;
        radius_powernew = 93*radius/233;
        
        double radius_powernew2;
        radius_powernew2 = radius_powernew + state.s13;
        
	g.setColor(incident);
	double Len = (double)state.s15;
        double LenT = (double)state.s15;
        double LenR = Len * Math.pow(Math.sin(state.Reflection_Coef.Magnitude()*Math.PI/2.0),0.5);
        if(state.Transmission_Coef.Magnitude()>1.0){
            LenT = Len;
        }
        else{
            LenT = Len * Math.pow(Math.sin(state.Transmission_Coef.Magnitude()*Math.PI/2.0),0.5);
        }
        double LenTH = (double)state.s15;;
        if(state.Transmission_CoefH.Magnitude()>1.0){
            LenTH = (double)state.s15;;
        }
        else{
            LenTH = LenTH * 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 = (-thetaG1+Math.PI)+23.0*Math.PI/180.0;
        angolo = (-thetaG1+Math.PI)-23.0*Math.PI/180.0;
        
        angoloT2 = (thetaG2+Math.PI*0.5)+23.0*Math.PI/180.0;
        angoloT = (thetaG2+Math.PI*0.5)-23.0*Math.PI/180.0;
        
        radiusV = radius * 0.5;
        fieldL = radius * 0.4;
        thicknessV = state.s3;
        thicknessV2 = state.s2;
        thicknessV3 = 0;
        radiusH = state.s8;
        
	  //reflected ray
	    
          //center coordinates
          x1 = (double)(xsize2);
          y1 = (double)(getSize().height/2);
          
          // coordinates of beta vector root
          x2 = (double)(xsize2 +(int)(radius_powernew*Math.cos(Math.PI-thetaG1)));
          y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI-thetaG1)));
          
          // coordinates of beta vector tip
          x2R = (double)(xsize2 +(int)((radius)*Math.cos(Math.PI-thetaG1)));
          y2R = (double)(getSize().height/2-(int)((radius)*Math.sin(Math.PI-thetaG1)));
          
          double vscaled;
          vscaled = state.beta_ratio*((radius+10.0) - radius_powernew2);
          
          int radiusnew;
	  //transmitted ray
          if(Math.abs(state.theta1_deg) < 90.0 && Math.abs(state.theta1_deg) > 0.0000001){
              
                  radiusnew = (int)((cladding_layer/2.0)/Math.sin(thetaG2));
                  
                  x1 = (double)(xsize2);
                  y1 = (double)(getSize().height/2);
                  x2 = (double)(xsize2 +(int)(radiusnew*Math.cos(thetaG2)));
                  y2 = (double)(getSize().height/2-(int)(radiusnew*Math.sin(thetaG2)));
                  
                  // stem
                  MaestroG.drawLineThick(g,x1,y1,x2,y2,1,Color.gray); // thin line to center
                  
                  double arco = (double)state.s40;
                  double yup, ydown, xright, xold, xtot;
                  xright = cladding_layer * Math.tan(0.5*Math.PI-thetaG2);
                  yup = cladding_layer; 
                  ydown = 2*cladding_layer;
                  xold = x2;
                  xtot = x2;
                  
                  MaestroG.FillArcTransp(g,x2-arco/2,y2-arco/2,arco,-90.0,-180.0/Math.PI*thetaG3, Color.yellow, 0.5);
                  MaestroG.drawArcThick(g,x2-arco/2,y2-arco/2,arco,-90.0,-180.0/Math.PI*thetaG3, 3, Color.orange);
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  g.setColor(Color.black);
                  MaestroG.subscripter3("\u03b8","3","","","",g,state.font18,(int)(x2-arco/20),(int)(yup+state.s40));
                  
                  
                  if(state.Radiates){MaestroG.drawLineThick(g,x2,y2-state.s25,x2,y2+state.s25,1,Color.red);
                        MaestroG.FillArcTransp(g,x2-arco/2,y2-arco/2,arco,90.0,-180.0/Math.PI*thetaG4, Color.yellow, 0.2);
                        MaestroG.drawArcThick(g,x2-arco/2,y2-arco/2,arco,90.0,-180.0/Math.PI*thetaG4, 3, Color.orange);
                        g.setColor(Color.black);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        MaestroG.subscripter3("\u03b8","t","","","",g,state.font18,(int)(x2+arco/3),(int)(yup-state.s20));
                  }
                  else{MaestroG.drawLineThick(g,x2,y2,x2,y2+state.s25,1,Color.red);}
                  
                  // stem
                  MaestroG.drawLineThick(g,x1,y1,x2,y2,1,Color.gray); // thin line to center
                  
                  while(xtot <= getSize().width)
                  {
                      x2 = x2+ xright;
                      MaestroG.drawLineThick(g,xold,yup,x2,ydown,1,Color.gray);
                      if(state.Radiates){
                            MaestroG.drawLineThickB(g,xold,yup,xold+(int)(cladding_layer/Math.tan(Math.PI*0.5 - thetaG4)),0,1,5,5,Color.pink);
                      }
                      xold = x2;
                      x2 = x2+ xright;
                      MaestroG.drawLineThick(g,xold,ydown,x2,yup,1,Color.gray);
                      if(state.Radiates){
                        MaestroG.drawLineThickB(g,xold,ydown,xold+(int)(cladding_layer/Math.tan(Math.PI*0.5 - thetaG4)),getSize().height,1,5,5,Color.pink);
                      }
                      xold = x2;
                      xtot = x2 - 2*xright;
                  } 

                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  
          }
          
                 
    }
    
    //-------------------------------------------------------------------------------------------------
    
        private void drawRefTrans3(Graphics g, Color colore, int thickness){
        //cladding_layer = getSize().height/3;
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        int lab_pos = state.s28;
        double x1, y1, x2, y2, x2R, y2R, x2T, y2T;
        double angolobetaT, angolobetaT2;
        angolobetaT2 = (thetaG2-Math.PI)+23.0*Math.PI/180.0; // THIS IS GOOD!!!
        angolobetaT = (thetaG2-Math.PI)-23.0*Math.PI/180.0;  //     "   "
        
        radius = getSize().height/2-TopMargin-scale_radius*VGap/60;
        //radius_powernew = radius*40/100;
        radius_powernew = 93*radius/233;
        
        double radius_powernew2;
        radius_powernew2 = radius_powernew+13;
        
	g.setColor(incident);
	double Len = (double)state.s15;
        double LenT = (double)state.s15;
        double LenR = (double)state.s15 * Math.pow(Math.sin(state.Reflection_Coef.Magnitude()*Math.PI/2.0),0.5);
        if(state.Transmission_Coef.Magnitude()>1.0){
            LenT = (double)state.s15;
        }
        else{
            LenT = (double)state.s15 * Math.pow(Math.sin(state.Transmission_Coef.Magnitude()*Math.PI/2.0),0.5);
        }
        double LenTH = (double)state.s15;
        if(state.Transmission_CoefH.Magnitude()>1.0){
            LenTH = (double)state.s15;
        }
        else{
            LenTH = (double)state.s15 * 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 = (thetaG1-Math.PI)+23.0*Math.PI/180.0;
        angolo = (thetaG1-Math.PI)-23.0*Math.PI/180.0;
        
        angoloT2 = (-thetaG2-Math.PI*0.5)+23.0*Math.PI/180.0;
        angoloT = (-thetaG2-Math.PI*0.5)-23.0*Math.PI/180.0;
        
        radiusV = radius * 0.5;
        fieldL = radius * 0.4;
        thicknessV = state.s3;
        thicknessV2 = state.s2;
        thicknessV3 = 0;
        radiusH = state.s8;
        
	  //reflected ray
	    
          //center coordinates
          x1 = (double)(xsize2);
          y1 = (double)(getSize().height/2);
          
          // coordinates of beta vector root
          x2 = (double)(xsize2 +(int)(radius_powernew*Math.cos(-Math.PI+thetaG1)));
          y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(-Math.PI+thetaG1)));
          
          // coordinates of beta vector tip
          x2R = (double)(xsize2 +(int)((radius)*Math.cos(-Math.PI+thetaG1)));
          y2R = (double)(getSize().height/2-(int)((radius)*Math.sin(-Math.PI+thetaG1)));
          
          double vscaled;
          vscaled = state.beta_ratio*((radius+10.0) - radius_powernew2);
          
          int radiusnew;
	  //transmitted ray
          if(Math.abs(state.theta1_deg) < 90.0 && Math.abs(state.theta1_deg) > 0.0000001){
              
                  radiusnew = (int)((cladding_layer/2.0)/Math.sin(thetaG2));
                  
                  x1 = (double)(xsize2);
                  y1 = (double)(getSize().height/2);
                  x2 = (double)(xsize2 +(int)(radiusnew*Math.cos(thetaG2)));
                  y2 = (double)(getSize().height/2+(int)(radiusnew*Math.sin(thetaG2)));
                  
                  // stem
                  MaestroG.drawLineThick(g,x1,y1,x2,y2,1,Color.gray); // thin line to center
                  
                  double arco = (double)state.s40;
                  double yup, ydown, xright, xold, xtot;
                  xright = cladding_layer * Math.tan(0.5*Math.PI-thetaG2);
                  ydown = cladding_layer; 
                  yup = 2*cladding_layer;
                  xold = x2;
                  xtot = x2;
                  
                  MaestroG.FillArcTransp(g,x2-arco/2,y2-arco/2,arco,90.0,180.0/Math.PI*thetaG3, Color.yellow, 0.5);
                  MaestroG.drawArcThick(g,x2-arco/2,y2-arco/2,arco,90.0,180.0/Math.PI*thetaG3, 3, Color.orange);
                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  g.setColor(Color.black);
                  MaestroG.subscripter3("\u03b8","3","","","",g,state.font18,(int)(x2-arco/20),(int)(yup-state.s30));
                  
                  if(state.Radiates){MaestroG.drawLineThick(g,x2,y2-state.s25,x2,y2+state.s25,1,Color.red);
                        MaestroG.FillArcTransp(g,x2-arco/2,y2-arco/2,arco,-90.0,180.0/Math.PI*thetaG4, Color.yellow, 0.2);
                        MaestroG.drawArcThick(g,x2-arco/2,y2-arco/2,arco,-90.0,180.0/Math.PI*thetaG4, 3, Color.orange);
                        g.setColor(Color.black);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        MaestroG.subscripter3("\u03b8","t","","","",g,state.s18,(int)(x2+arco/3),(int)(yup+state.s35));
                  }
                  else{MaestroG.drawLineThick(g,x2,y2,x2,y2-state.s25,1,Color.red);}
                  
                  // stem
                  MaestroG.drawLineThick(g,x1,y1,x2,y2,1,Color.gray); // thin line to center
                  
                  while(xtot <= getSize().width)
                  {
                      x2 = x2+ xright;
                      MaestroG.drawLineThick(g,xold,yup,x2,ydown,1,Color.gray);
                      if(state.Radiates){
                            MaestroG.drawLineThickB(g,xold,yup,xold+(int)(cladding_layer/Math.tan(Math.PI*0.5 - thetaG4)),getSize().height,1,5,5,Color.pink);
                      }
                      xold = x2;
                      x2 = x2+ xright;
                      MaestroG.drawLineThick(g,xold,ydown,x2,yup,1,Color.gray);
                      if(state.Radiates){
                        MaestroG.drawLineThickB(g,xold,ydown,xold+(int)(cladding_layer/Math.tan(Math.PI*0.5 - thetaG4)),0,1,5,5,Color.pink);
                      }
                      xold = x2;
                      xtot = x2 - 2*xright;
                  } 

                  g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                  
          }
          
          //--------------------------------------------------------------------
            //if(!IsFocusOn){drawY(g);}
          //--------------------------------------------------------------------          
    }
    
        
    public void drawArrowScaled(int x, int y, int tipo, double sfactor, Graphics g){
	Graphics2D g2d = (Graphics2D)g;
        double s;
        s = sfactor;
        
        switch (tipo){
	   
          case 1://ArrowUpSmooth
               g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
                g.drawLine(x,y-1,x,y+(int)(s*5));
		//draw oblique arrow head
		Polygon pH = new Polygon();
		pH.addPoint(x-(int)(s*2), y-(int)(s*2));
		pH.addPoint(x+(int)(s*2), y-(int)(s*2));
		pH.addPoint(x,y-(int)(s*8));
		g.drawPolygon(pH);
		g.fillPolygon(pH);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
        
          break;
          
          case 2://ArrowDownSmooth
               g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
                g.drawLine(x,y+1,x,y-(int)(s*5));
		//draw oblique arrow head
		Polygon pJ = new Polygon();
		pJ.addPoint(x-(int)(s*2), y+(int)(s*2));
		pJ.addPoint(x+(int)(s*2), y+(int)(s*2));
		pJ.addPoint(x,y+(int)(s*8));
		g.drawPolygon(pJ);
		g.fillPolygon(pJ);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	   break;
           
           case 3://ArrowRightSmooth
               g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
                g.drawLine(x+1,y,x-(int)(s*5),y);
		//draw oblique arrow head
		Polygon pK = new Polygon();
		pK.addPoint(x+(int)(s*2), y-(int)(s*2));
		pK.addPoint(x+(int)(s*2), y+(int)(s*2));
		pK.addPoint(x+(int)(s*8),y);
		g.drawPolygon(pK);
		g.fillPolygon(pK);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	   
	   break;
           
           case 4://ArrowLeftSmooth
               g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
                g.drawLine(x-1,y,x+(int)(s*5),y);
		//draw oblique arrow head
		Polygon pL = new Polygon();
		pL.addPoint(x-(int)(s*2), y-(int)(s*2));
		pL.addPoint(x-(int)(s*2), y+(int)(s*2));
		pL.addPoint(x-(int)(s*8),y);
		g.drawPolygon(pL);
		g.fillPolygon(pL);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
 
	   break;
           
           case 5://ArrowOblique 45 degrees pointing NE
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
		//draw oblique arrow head
		Polygon pM = new Polygon();
                
                pM.addPoint(x+(int)(s*6),y-(int)(s*6)); // longer arrow
		pM.addPoint(x+(int)(s*1),y-(int)(s*3));
		pM.addPoint(x+(int)(s*2),y+(int)(s*1));
		
                g.drawPolygon(pM);
		g.fillPolygon(pM);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
                 
           break;
           
           case 6://ArrowOblique 45 degrees pointing SW
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
		//draw oblique arrow head
		Polygon pN = new Polygon();
                
                pN.addPoint(x-(int)(s*6),y+(int)(s*6)); 
		pN.addPoint(x-(int)(s*1),y+(int)(s*3));
		pN.addPoint(x-(int)(s*2),y-(int)(s*2));
		
                g.drawPolygon(pN);
		g.fillPolygon(pN);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
                 
           break;
           
           case 7://Larger ArrowRightSmooth
               g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
                Polygon pR = new Polygon();
		pR.addPoint(x, y-(int)(s*3));
		pR.addPoint(x, y+(int)(s*3));
		pR.addPoint(x+(int)(s*7),y);
		g.drawPolygon(pR);
		g.fillPolygon(pR);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	   
	   break;
           
           case 8://Larger ArrowLeftSmooth
               g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
                Polygon pS = new Polygon();
		pS.addPoint(x, y-(int)(s*3));
		pS.addPoint(x, y+(int)(s*3));
		pS.addPoint(x-(int)(s*7),y);
		g.drawPolygon(pS);
		g.fillPolygon(pS);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
 
	   break;
           
           case 9://ArrowUpSmooth
               g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
                Polygon pU = new Polygon();
		pU.addPoint(x-(int)(s*3), y);
		pU.addPoint(x+(int)(s*3), y);
		pU.addPoint(x,y-(int)(s*7));
		g.drawPolygon(pU);
		g.fillPolygon(pU);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
        
          break;
          
          case 10://ArrowDownSmooth
               g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
                Polygon pD = new Polygon();
                pD.addPoint(x-(int)(s*3), y);
		pD.addPoint(x+(int)(s*3), y);
		pD.addPoint(x,y+(int)(s*7));
		g.drawPolygon(pD);
		g.fillPolygon(pD);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	   break;
	}		
    }

    //-------------------------------------------------------------------------------------------------
    
    
    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){}
}
