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 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 | Font.BOLD,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 Font labfontbigIB=new Font("Serif",Font.ITALIC | Font.BOLD,14);
    
    private static final int HGap=10, VGap=10;//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 = 150;
    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;
        
	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){
        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){
            drawWaves2(g);
        }
        else{
            drawWaves3(g);
        }
        
        drawY(g);
    }
    
    private void drawMedium(Graphics g){/// OK WITH ULABY's BOOK CONVENTIONS
	//cladding_layer = getSize().height/3;
        
        g.setColor(medium1Color);
	g.fillRect(1,1,xsize2-1,getSize().height-2);
	g.setColor(medium2Color);
	g.fillRect(xsize2,1,xsize-1,getSize().height-2);
        
        g.setColor(medium3Color);
	g.fillRect(xsize2,1,xsize-1,cladding_layer);
        g.fillRect(xsize2,2*cladding_layer,xsize-1,cladding_layer);
        
        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-40,cladding_layer,getSize().width-40,3*cladding_layer/2);
        MaestroG.drawArrow( getSize().width-40,cladding_layer+10, 5, g);
        MaestroG.drawArrow( getSize().width-40,3*cladding_layer/2-10, 6, g);
        g.setFont(labfontbigIB);
        g.drawString("R",getSize().width-35,5*cladding_layer/4+5);
        
        g.setFont(new Font("Sanserif",Font.BOLD,14));
        if(state.Radiates && (state.theta1_deg < 90.0 && state.theta1_deg > -90.0) ){
            g.setColor(Color.red);
            g.drawString("Radiating (Leaky) Mode", 200, 20);
        }
        else if(state.theta1_deg == 90.0 || state.theta1_deg == -90.0){ 
            g.setColor(Color.red);
            g.drawString("No Propagation", 200, 20);
        }
        else{  
            g.setColor(Color.red);
            g.drawString("Guided Mode", 200, 20);
        }
        
        g.setColor(Color.blue);
        MaestroG.subscripterBSym("\u03b8","imax"," = "+MaestroA.rounder(state.theta_range,4)+"\u00ba",g,14,10,11*getSize().height/12);
        
    }
    
    private void drawAxis(Graphics g){/// OK WITH ULABY's BOOK CONVENTIONS
	//cladding_layer = getSize().height/3;
        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(xsize2,TopMargin,xsize2,getSize().height-BottomMargin-10);
        MaestroG.drawArrow( xsize2, TopMargin+10, 5, 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 = 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,xsize2-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";
	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()+5);}
	
        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 +10,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-200,cladding_layer+3*VGap/2);
        
        g.setColor(Color.black);
	tmp  = "medium 3 - Cladding";
	g.drawString(tmp,getSize().width-200,getSize().height-4*VGap);
        g.drawString(tmp,getSize().width-200,20);
	
        int x0 = getSize().width/8 + 50;
        int y0 = getSize().height-15;
	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-2,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+5;
	//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,14,mediumX+7,mediumY);
        }
        else{
            MaestroG.subscripter("\u03b5","r0"," = "+MaestroA.rounder(state.epsilon_r1,3),g,14,mediumX+7,mediumY);
        } 
        
        //For medium 2
	g.setColor(Color.blue.darker());
	//mediumX = xsize2+40;
        //mediumY = 2*cladding_layer+3*VGap/2;
        mediumX = getSize().width-200;
        mediumY = cladding_layer+7*VGap/2;
        if(state.Show_index){
            MaestroG.subscripter("n","f"," = "+MaestroA.rounder(state.index2,6),g,14,mediumX,mediumY);
        }
        else{
            MaestroG.subscripter("\u03b5","rf"," = "+MaestroA.rounder(state.epsilon_r2,3),g,14,mediumX,mediumY);
        } 
	
	//For medium 3
	g.setColor(Color.black);
	mediumX = getSize().width-200;
        mediumY = getSize().height-2*VGap;
        if(state.Show_index){
            MaestroG.subscripter("n","c"," = "+MaestroA.rounder(state.index3,6),g,14,mediumX,mediumY);
            MaestroG.subscripter("n","c"," = "+MaestroA.rounder(state.index3,6),g,14,mediumX,20+2*VGap);
        }
        else{
            MaestroG.subscripter("\u03b5","rc"," = "+MaestroA.rounder(state.epsilon_r3,3),g,14,mediumX,mediumY);
            MaestroG.subscripter("\u03b5","rc"," = "+MaestroA.rounder(state.epsilon_r3,3),g,14,mediumX,20+2*VGap);
        }
        
	g.setFont(normalfont); 
	fm = g.getFontMetrics(); 
    }
    
    //------------------------------------------------------------------------------------------    
    
        private void drawWaves2(Graphics g){
        
        //cladding_layer = getSize().height/3;
        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/60;
        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 = (-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 = 3;
        thicknessV2 = 2;
        thicknessV3 = 0;
        radiusH = 8;
        
        // 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 - 15.0;
          yo = y1 - 50.0;
          
          ango = Math.PI + thetaG1*0.5;
          ango3 = Math.PI - thetaG1*0.5;
          
          ango2 = thetaG2*0.5;
          
          // Represent angle
          // incident wave
          
          double arco = 30.0;
          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,18,(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*thetaG1, 3, Color.orange.darker());
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter3("\u03b8","i","","","",g,18,(int)x1-(int)(50.0*Math.cos(thetaG1+Math.PI/7.0)),
                                                            (int)y1-5+(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,18,(int)x1-12+(int)(50.0*Math.cos(thetaG2+Math.PI/7.0)),
                                                                (int)y1+13-(int)(50.0*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",g,14,330,20);
                  MaestroG.subscripter("(Total Reflection)","","",g,14,330,40);

                  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,14,(int)(x1+rado*Math.sin(Math.PI*0.25))-5, (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 = 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)+1,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 = 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/60;
        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 = (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 = 3;
        thicknessV2 = 2;
        thicknessV3 = 0;
        radiusH = 8;
        
        // 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 - 15.0;
          yo = y1 - 50.0;
          
          ango = Math.PI - thetaG1*0.5;
          ango3 = Math.PI + thetaG1*0.5;
          
          ango2 = -thetaG2*0.5;
          
          // Represent angle
          // incident wave
          
          double arco = 30.0;
          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(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,18,(int)(x1+rado*Math.cos(ango))-5, (int)(y1+2-rado*Math.sin(ango))+5);
          }
          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,18,(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,18,(int)x1-12+(int)(50.0*Math.cos(thetaG2+Math.PI/7.0)),
                                                        (int)y1-5+(int)(50.0*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",g,14,330,20);
                    MaestroG.subscripter("(Total Reflection)","","",g,14,330,40);

                    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,14,(int)(x1+rado*Math.sin(Math.PI*0.25))-5, (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 = 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)+1,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){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 = 28;
        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 = 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 = (-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 = 3;
        thicknessV2 = 2;
        thicknessV3 = 0;
        radiusH = 8;
        
	  //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 = 40.0;
                  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,18,(int)(x2-arco/2),(int)(yup+35));
                  
                  
                  if(state.Radiates){MaestroG.drawLineThick(g,x2,y2-25,x2,y2+25,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,18,(int)(x2+arco/3),(int)(yup-20));
                  }
                  else{MaestroG.drawLineThick(g,x2,y2,x2,y2+25,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 = 28;
        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 = 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 = (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 = 3;
        thicknessV2 = 2;
        thicknessV3 = 0;
        radiusH = 8;
        
	  //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 = 40.0;
                  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,18,(int)(x2-arco/2),(int)(yup-30));
                  
                  if(state.Radiates){MaestroG.drawLineThick(g,x2,y2-25,x2,y2+25,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,18,(int)(x2+arco/3),(int)(yup+35));
                  }
                  else{MaestroG.drawLineThick(g,x2,y2,x2,y2-25,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 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){}
}
