import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ObliqueCanvas extends Canvas implements MouseListener{
    private static final Color bgcolor = new Color(200,200,200);
    private static final Color medium1Color = new Color(250,250,250);
    private static final Color medium2Color = new Color(200,220,220);
    
    private int LeftMargin, RightMargin, TopMargin, BottomMargin;
    private Font labfont;
    private Font axisfont;
    public Font normalfont;
    public Font normalfontB;
    public Font smallfont;
    public Font symbfont;
    private Font labfontbig;
    private Font labfontbigI;
    
    private int HGap, VGap;//for vectors fields.
    private Image im;
    private Graphics buf;
    private double extra = 0.1;
    
    public int offset;
    
    private Oblique_State state;
    private int radius, radius_power, radius_powernew, scale_radius;
    private boolean IsFocusOn, IsPrintOn;
    public boolean Field_Out, Field_Refl_Out, ShowTR, ShowREF;
    private Color incident = Color.red; 

    private BufferedImage arrow_inf;
    
    public ObliqueCanvas(Oblique_State state){
	super();
	this.state = state;
        offset = state.s10;
	getImages();

        labfont = new Font("SanSerif",Font.PLAIN,state.font10);
        axisfont = new Font("Serif",Font.ITALIC,state.font20);
        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);
        
        LeftMargin = state.s10; RightMargin = state.s10; 
        TopMargin = state.s10; BottomMargin = state.s10;
        HGap = state.s10;
        VGap = state.s10;
        
	IsFocusOn = false;
        IsPrintOn = false;
        Field_Out = false;
        Field_Refl_Out = true;
        ShowTR = false;
        ShowREF = false;
        
	setBackground(bgcolor);
	//Listeners
	this.addMouseListener(this);
        
        scale_radius = 100; // 150;
        radius = getSize().height/2-TopMargin-scale_radius*VGap/20;
        radius_power = radius * 6/10;
    }


    private void getImages() {
        // read in the 'arrow-inf' image, because CHEERPJ does not render these
	//  symbols in any font!  (java to javascript converter)


	System.out.printf("1. scale-factor %f%n",state.sfactor);
	
        try {
            //arrow_inf = ImageIO.read(getClass().getResource("arrow_inf.png"));
	    BufferedImage img =
		ImageIO.read(getClass().getResource("arrow_inf.png"));
	    int targetWidth = (int) (state.sfactor * img.getWidth());
	    int targetHeight = (int) (state.sfactor * img.getHeight());
	    arrow_inf =
		new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
	    Graphics2D graphics2D = arrow_inf.createGraphics();
	    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
					RenderingHints.VALUE_INTERPOLATION_BILINEAR);
	    graphics2D.drawImage(img, 0, 0, targetWidth, targetHeight, 0, 0,
				 img.getWidth(), img.getHeight(), null);
	    graphics2D.dispose();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void drawCanvas(Graphics g){
	g.setColor(Color.black);
	g.draw3DRect(0,0,getSize().width-1,getSize().height-1,false);
	drawMedium(g);
	drawAxis(g);
        //if(!IsFocusOn){drawY(g);}

	drawLabels(g);
	
        drawBackLabels(g);
        drawWaves2(g);
        
        if(IsFocusOn){drawScaleRefTrans2(g, Color.red, 6);}
        //else{drawY(g);}
    }
    
    private void drawMedium(Graphics g){/// OK WITH ULABY's BOOK CONVENTIONS
	int x11 = 3*getSize().width/4;
        g.setColor(medium1Color);
	g.fillRect(1,1,x11-1,getSize().height-2);
	g.setColor(medium2Color);
	g.fillRect(x11,1,getSize().width/4-1,getSize().height-2);
    }
    
    private void drawAxis(Graphics g){/// OK WITH ULABY's BOOK CONVENTIONS
	int x11 = 3*getSize().width/4;
        g.setColor(Color.black);
	//horizontal axis - z
	g.drawLine(LeftMargin,getSize().height/2,getSize().width-RightMargin-state.s10,getSize().height/2);
	//MaestroG.drawArrow( getSize().width-RightMargin-state.s10, getSize().height/2, 7, g);
        MaestroG.drawArrowScaled(getSize().width-RightMargin-state.s10, getSize().height/2, 3, state.sfactor, g);
        
        //vertical axis - x (pointing up in Ulaby's book)
	g.drawLine(x11,TopMargin,x11,getSize().height-BottomMargin-state.s10);
        //MaestroG.drawArrow(x11, TopMargin+state.s10, 5, g);
        MaestroG.drawArrowScaled(x11, TopMargin+state.s7, 1, state.sfactor, g);
    }

    private void drawY(Graphics g){/// OK WITH ULABY's BOOK CONVENTIONS
	double x1, y1, radiusy, radiusX, radiusdot; 
        //x1 = (double)(getSize().width/2);
        x1 = (double)(3*getSize().width/4);
        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());
	int x11 = 3*getSize().width/4;
	tmp = "x";
	g.drawString(tmp,x11-2*fm.stringWidth(tmp)-state.s5,fm.getHeight());
	tmp = "z";
	g.drawString(tmp,getSize().width-RightMargin-fm.stringWidth(tmp)-state.s5,getSize().height/2+fm.getHeight());
	tmp = "y";
	g.drawString(tmp,(x11+fm.stringWidth(tmp)),getSize().height/2+fm.getHeight());
	
        //g.setFont(normalfontB);
        g.setFont(new Font("Sanserif",Font.BOLD,state.font12));
        g.setColor(Color.red.darker());
	tmp =  "medium 1";
	g.drawString(tmp,LeftMargin + state.s10,state.s5 + VGap);
	g.setColor(Color.blue.darker());
	tmp  = "medium 2";
	g.drawString(tmp,getSize().width - state.s95,state.s5 + VGap);
        
        if(state.theta1 == 0.0){
            g.setColor(Color.black);
            g.drawString("At normal incidence there is no longer distinction",LeftMargin + state.s10,4*getSize().height/5);
            g.drawString("between parallel and perpendicular polarization",LeftMargin + state.s10,4*getSize().height/5 + state.s20);
        }
        
        int x0 = getSize().width/4;
        int x1 = 2*getSize().width/3 + state.s20;
        int y0 = getSize().height - state.s15;
	int y1 = getSize().height - state.s30;
        g.setFont(smallfont);
        g.setColor(Color.red.darker());
        
        if(IsFocusOn){
                g.setFont(normalfontB);
                g.setColor(Color.red);
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                g.drawString("Power Flow",x1 + state.s40,y0);
                MaestroG.drawLineThick(g, x1, y0-state.s5, x1+state.s30, y0-state.s5, 3, Color.red);
        }
        
        g.setColor(Color.black);
        //g.setFont(labfontbig);
        g.setFont(new Font("SanSerif",Font.PLAIN,state.font14));
        
        //frequency
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
	x0 = state.s25;
        y0 = getSize().height-state.s70;
        
                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);
                
                //==============================================================
                
                int fonto = state.font14;
                double test;
                y0 = getSize().height-state.s45;
                g.setColor(Color.black);
                test = state.wavelength1;
                MaestroG.subscripterSS2("\u03bb","1","",g,fonto,x0-state.s2,y0);
                Write(g,test,fonto,x0,y0);
            
                //x0 = getSize().width/2-state.s30;
                y0 = getSize().height-state.s20;
                
                test = state.wavelength1x;
                MaestroG.subscripter3("\u03bb","1","z","","",g,fonto,x0-state.s12,y0);
                
                if(state.theta1 == Math.PI*0.5){
                    g.setFont(normalfont);
                    g.drawString("    =  \u221e [m]",x0,y0);
                }
                else{
                    Write(g,test,fonto,x0,y0);
                }
                //==============================================================
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    }
    
    public void Write(Graphics g, double test, int fonto, int x, int y){
            Graphics2D g2d = (Graphics2D)g;
            //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
            //if(test < 1.0e-15){
		//MaestroG.superscripter("  =  "+MaestroA.rounder(test*1.0e16,5)+"  x  10","-16"," ",g,fonto,x,y);
	    //}
            if(test < 1.0e-15){
		MaestroG.superscripter("    =  0.0",""," [m]",g,fonto,x,y);
	    }
            else if(test >= 1.0e-15 && test < 1.0e-14){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e15,5)+"  x  10","-15"," [m]",g,fonto,x,y);
	    }
            else if(test >= 1.0e-14 && test < 1.0e-13){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e14,5)+"  x  10","-14"," [m]",g,fonto,x,y);
	    }
            else if(test >= 1.0e-13 && test < 1.0e-12){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e13,5)+"  x  10","-13"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e-12 && test < 1.0e-11){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e12,5)+"  x  10","-12"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e-11 && test < 1.0e-10){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e11,5)+"  x  10","-11"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e-10 && test < 1.0e-9){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e10,5)+"  x  10","-10"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e-9 && test < 1.0e-8){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e9,5)+"  x  10","-9"," [m]",g,fonto,x,y);
	    }
	    
	    else if(test >= 1.0e-8 && test < 1.0e-7){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e8,5)+"  x  10","-8"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e-7 && test < 1.0e-6){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e7,5)+"  x  10","-7"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e-6 && test < 1.0e-5){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e6,5)+"  x  10","-6"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e-5 && test < 1.0e-4){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e5,5)+"  x  10","-5"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e-4 && test < 1.0e-3){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e4,5)+"  x  10","-4"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e-3 && test < 1.0e-2){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e3,5)+"  x  10","-3"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e-2 && test < 1.0e-1){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e2,5)+"  x  10","-2"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e-1 && test < 1.0){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e1,5)+"  x  10","-1"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0 && test < 1.0e1){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test,5)+"  ",""," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e1 && test < 1.0e2){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test,5)+"  ",""," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e2 && test < 1.0e3){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-2,5)+"  x  10","2"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e3 && test < 1.0e4){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-3,5)+"  x  10","3"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e4 && test < 1.0e5){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-4,5)+"  x  10","4"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e5 && test < 1.0e6){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-5,5)+"  x  10","5"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e6 && test < 1.0e7){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-6,5)+"  x  10","6"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e7 && test < 1.0e8){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-7,5)+"  x  10","7"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e8 && test < 1.0e9){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-8,5)+"  x  10","8"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e9 && test < 1.0e10){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-9,5)+"  x  10","9"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e10 && test < 1.0e11){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-10,5)+"  x  10","10"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e11 && test < 1.0e12){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-11,5)+"  x  10","11"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e12 && test < 1.0e13){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-12,5)+"  x  10","12"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e13 && test < 1.0e14){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-13,5)+"  x  10","13"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e14 && test < 1.0e15){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-14,5)+"  x  10","14"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e15 && test < 1.0e16){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-15,5)+"  x  10","15"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e16 && test < 1.0e17){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-16,5)+"  x  10","16"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e17 && test < 1.0e18){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-17,5)+"  x  10","17"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e18 && test < 1.0e19){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-18,5)+"  x  10","18"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e19 && test < 1.0e20){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-19,5)+"  x  10","19"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e20 && test < 1.0e21){
		MaestroG.superscripter("    =  "+MaestroA.rounder(test*1.0e-20,5)+"  x  10","20"," [m]",g,fonto,x,y);
	    }
	    else if(test >= 1.0e21){
                MaestroG.superscripter("    =  "+test+"",""," [m]",g,fonto,x,y);
            }
            
            //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
        }

    
    private void drawBackLabels(Graphics g){
	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 = TopMargin+2*VGap+state.s5;
	
        g.setColor(Color.red.darker());
	
        MaestroG.subscripter("\u03b5","r 1"," = "+state.epsilon_r1,g,state.font14,mediumX+state.s7,mediumY); 
	g.setFont(normalfont);   
	fm = g.getFontMetrics();
        
	mediumY += 4*fm.getHeight()/3;
        
	MaestroG.subscripter("\u03bc","r 1"," = "+state.mu_r1,g,state.font14,mediumX+state.s5,mediumY);
	g.setFont(normalfont); 
	fm = g.getFontMetrics();
        
	mediumY += 4*fm.getHeight()/3;
        MaestroG.subscripter("\u03c3","1"," = 0.0 [S/m]",g,state.font14,mediumX+state.s5,mediumY);  
	
        g.setFont(new Font("Sanserif",Font.BOLD,state.font12));
        fm = g.getFontMetrics();
        
        mediumY = fm.getHeight();
        
        int Xpolar = state.s200+state.s80;
        if(state.isPolarizationParallel){
            g.setColor(Color.blue);
            g.drawString("     Parallel",Xpolar,mediumY);
            mediumY += fm.getHeight();
            g.drawString("  Polarization",Xpolar,mediumY);
            mediumY += fm.getHeight();
            g.drawString("       (TM)",Xpolar,mediumY);
        }
        else{
            g.setColor(Color.magenta.darker());
            g.drawString("Perpendicular",Xpolar,mediumY);
            mediumY += fm.getHeight();
            g.drawString("  Polarization",Xpolar,mediumY);
            mediumY += fm.getHeight();
            g.drawString("       (TE)",Xpolar,mediumY);
        }
        
	//For medium 2
	g.setColor(Color.blue.darker());
	mediumX = getSize().width-state.s120;
        mediumY = TopMargin+2*VGap;
        
        //g.setFont(normalfont); 
	g.setFont(new Font("Sanserif",Font.PLAIN,state.font12));
        fm = g.getFontMetrics();
        
	mediumY += fm.getHeight();
	//MaestroG.subscripter("\u03c3","2"," \u2192 \u221e  [S/m]", g, state.font14, mediumX,mediumY);
	MaestroG.SubTxtImgTxt("\u03c3","2",arrow_inf,"  [S/m]", g, state.font14, mediumX,mediumY,this);
    }
    
    //------------------------------------------------------------------------------------------    
    
        private void drawWaves2(Graphics g){
        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/20;
        radius_powernew = 93*radius/233;
        
	g.setColor(incident);
	//incident ray
	
        double Len = (double)state.s15;
        double radiusV, fieldL, x2_power, y2_power;
        int thicknessV, thicknessV2, thicknessV3, radiusH;
        double angolo, angolo2, angolobeta, angolobeta2, angolobetaT, angolobetaT2,x2V, y2V, x2E, y2E;
        Color colorV = Color.black;
        Color colorE = Color.magenta.darker();
        Color colorH = Color.blue;
        
        angolobeta2 = (-state.theta1+Math.PI)+23.0*Math.PI/180.0; 
        angolobeta = (-state.theta1+Math.PI)-23.0*Math.PI/180.0;  
        angolobetaT2 = (-state.theta2+Math.PI)+23.0*Math.PI/180.0; 
        angolobetaT = (-state.theta2+Math.PI)-23.0*Math.PI/180.0;  
        
        angolo2 = (state.theta1+Math.PI)+23.0*Math.PI/180.0; 
        angolo = (state.theta1+Math.PI)-23.0*Math.PI/180.0;  
        
        radiusV = radius * 0.4;
        fieldL = 1.1*(radius * 0.4);
        thicknessV = state.s3;
        thicknessV2 = state.s2;
        thicknessV3 = 0;
        radiusH = state.s8;
        
        
        // center coordinates
          //x1 = (double)(getSize().width/2);
          x1 = (double)(3*getSize().width/4);
          y1 = (double)(getSize().height/2);
        
        // coordinates of beginning of incident line
          x2 = (double)(x1+(int)(radius*Math.cos(Math.PI - state.theta1)));
          y2 = (double)(y1+(int)(radius*Math.sin(Math.PI - state.theta1)));
          
          x2_power = (double)(x1-(int)(radius_powernew*Math.cos(state.theta1)));
          y2_power = (double)(y1+(int)(radius_powernew*Math.sin(state.theta1)));
          
          double xo, yo, ango, ango2, ango3, rado;
          xo = x1 - (double)state.s15;
          yo = y1 - (double)state.s50;
          //rado = Math.sqrt(15.0*15.0 + 50.0*50.0)+5;
          rado = state.sfactor*(Math.sqrt(15.0*15.0 + 50.0*50.0)+5);
          ango = Math.PI + state.theta1*0.5;
          ango3 = Math.PI - state.theta1*0.5;
          
          ango2 = state.theta2*0.5;
          
          // Represent angles
          // incident wave
          
          MaestroG.FillArcTransp(g,x1-(double)state.s40,y1-(double)state.s40,(double)state.s80,180.0,180.0/Math.PI*state.theta1, Color.orange, 0.2);
          
          //if(180.0/Math.PI*state.theta1 < 0.99 || 180.0/Math.PI*state.theta1 > 1.01 ){// to remove a little numerical glitch at 1.0 degree
                  MaestroG.drawArcThick(g, x1-(double)state.s40, y1-(double)state.s40, (double)state.s80, 180.0, 180.0/Math.PI*state.theta1, state.s3, Color.orange.darker());
          //}
          
          if(180.0/Math.PI*state.theta1 >= 20.0){
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter("\u03b8","i","",g,state.font15,(int)(x1+rado*Math.cos(ango))-state.s5, (int)(y1+state.s2-rado*Math.sin(ango)));
          }
          else{
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter("\u03b8","i","",g,state.font15,(int)x1-(int)(50.0*Math.cos(state.theta1+Math.PI/7.0)),
                                                            (int)y1 + (int)(50.0*Math.sin(state.theta1+Math.PI/7.0)));
          }
        // reflected wave
          MaestroG.FillArcTransp(g,x1-(double)state.s40,y1-(double)state.s40,(double)state.s80,180.0,-180.0/Math.PI*state.theta1, Color.cyan, 0.1);
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
          if(180.0/Math.PI*state.theta1 >= 20.0){
              MaestroG.drawArcThick(g, x1-(double)state.s40, y1-(double)state.s40, (double)state.s80, 180.0, -180.0/Math.PI*state.theta1, state.s3, Color.cyan.darker());
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter("\u03b8","r","",g,state.font15,(int)(x1+rado*Math.cos(ango3))-state.s5, (int)(y1+state.s6-rado*Math.sin(ango3)));
          }
          else{
              MaestroG.drawArcThick(g, x1-(double)state.s40, y1-(double)state.s40, (double)state.s80, 180.0, -180.0/Math.PI*state.theta1, state.s3, Color.cyan.darker());
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripter("\u03b8","r","",g,state.font15,(int)x1-(int)(50.0*Math.cos(state.theta1+Math.PI/7.0)),
                                                (int)y1+state.s10-(int)(50.0*Math.sin(state.theta1+Math.PI/7.0)));
          }
          
        // coordinates of incident vector tip
          x2V = (double)(x1+(int)(radiusV*Math.cos(Math.PI - state.theta1)));
          y2V = (double)(y1+(int)(radiusV*Math.sin(Math.PI - state.theta1)));
          
          
          drawAxis(g);
          
          
          //vectors
          //---------------------------------------------------------------
          // incident beta vector
            // arrow tip
          double Len2 = (double)state.s15;
          
          if(state.theta1 == 0.0){x2 = x2 - offset; x2V = x2V - offset; y2 = y2 + offset; y2V = y2V + offset;}
          
          MaestroG.drawLineThick(g,x2V,y2V,x2V+Len2*Math.cos(angolobeta),y2V+Len2*Math.sin(angolobeta),thicknessV,colorV);
          MaestroG.drawLineThick(g,x2V,y2V,x2V+Len2*Math.cos(angolobeta2),y2V+Len2*Math.sin(angolobeta2),thicknessV,colorV);
            // stem
          if(state.theta1 != 0.0){MaestroG.drawLineThick(g,x1,y1,x2,y2,1,Color.gray);}
          MaestroG.drawLineThick(g,x2,y2,x2V,y2V,thicknessV,colorV);
          //MaestroG.subscripter("\u03b2","i","",g,18,(int)(x2+x2V)/2-20,(int)(y2+y2V)/2-15);
          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
          //MaestroG.subscripterBHat("k","i","",g,18,(int)(x2+x2V)/2-20,(int)(y2+y2V)/2-15); // Ulaby's book notation
          if(state.theta1 != 0.0){MaestroG.subscripterB("k","i","",g,state.font18,(int)(x2+x2V)/2-state.s20,(int)(y2+y2V)/2-state.s15); // Ulaby's book notation
          }
          else{
              MaestroG.subscripterB("k","i","",g,state.font18,(int)(x2+x2V)/2-state.s20,(int)(y2+y2V)/2+state.s20);
          }
          // z-component
          MaestroG.drawLineThick(g,x2,y2,x2V,y2,1,colorV);
          //MaestroG.drawArrow((int)x2V-state.s8,(int)(y2),7,g);
          MaestroG.drawArrowScaled((int)x2V-state.s5,(int)(y2), 3, state.sfactor, g);
          // x-component
          MaestroG.drawLineThick(g,x2-state.s1,y2,x2-state.s1,y2V,1,colorV);
          //MaestroG.drawArrow((int)(x2-state.s1),(int)y2V+state.s8,5,g);
          MaestroG.drawArrowScaled((int)(x2-state.s1),(int)y2V+state.s5, 1, state.sfactor, g);
          //---------------------------------------------------------------
          
          if(IsFocusOn){MaestroG.drawLineThick(g, x1, y1+state.s2, x2_power, y2_power+state.s2, thickness, incident);}
          
        {
	    drawRefTrans2(g, incident, thickness);
            
	}
	
                
    	if(state.isPolarizationParallel){ // Parallel Polarization - Incident Wave
                x2 = (double)(x1+(int)(radius*Math.cos(Math.PI - state.theta1)));
                y2 = (double)(y1+(int)(radius*Math.sin(Math.PI - state.theta1)));
                if(state.theta1 == 0.0){x2 = x2 - offset; y2 = y2+offset;}
                 if(Field_Out){
                    x2E = x1+(radius*Math.cos(Math.PI - state.theta1) - fieldL * Math.sin(state.theta1));
                    y2E = y1+(radius*Math.sin(Math.PI - state.theta1) - fieldL * Math.cos(state.theta1));
                    if(state.theta1 == 0.0){x2E = x2E - offset; y2E = y2E + offset;}
                    // E field vector
                    MaestroG.drawLineThick(g,x2, y2, x2E, y2E,thicknessV, colorE);

                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo+(Math.PI/2.0)),y2E-Len*Math.sin(angolo+(Math.PI/2.0)),thicknessV,colorE);
                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo2+(Math.PI/2.0)),y2E-Len*Math.sin(angolo2+(Math.PI/2.0)),thicknessV,colorE);
                    
                }
                else{
                    x2E = x1+(radius*Math.cos(Math.PI - state.theta1) + fieldL * Math.sin(state.theta1));
                    y2E = y1+(radius*Math.sin(Math.PI - state.theta1) + fieldL * Math.cos(state.theta1));
                    if(state.theta1 == 0.0){x2E = x2E - offset; y2E = y2E + offset;}
                    // E field vector
                    MaestroG.drawLineThick(g,x2,y2, x2E,y2E,thicknessV, colorE);

                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo+(3.0*Math.PI/2.0)),y2E-Len*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV,colorE);
                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2E-Len*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV,colorE);
                    
                }
                //An oval for magnetic field
               y2 = (double)(y1+(int)(radius*Math.sin(Math.PI - state.theta1)));
               if(state.theta1 == 0.0){x1 = x1 - offset; y2 = y2 + offset;} 
               
               if(Field_Out){
                        
                        //MaestroG.fillCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),
                        //            y2,(double)radiusH, 2, Color.white);
                        //MaestroG.drawCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),
                        //            y2,(double)state.s2,state.s4,colorH);
                        //MaestroG.drawCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),
                        //            y2,(double)radiusH,state.s2,colorH);
                        
                        MaestroG.fillCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)), y2,(double)radiusH+state.s1,2,colorE);
                        MaestroG.fillCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),y2,(double)(radiusH-state.s1), 2, Color.white);            
                        MaestroG.fillCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),y2,(double)state.s2,4,colorE);
                        
                       
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup2("H","//","i","",g,state.font16,
                                                    (int)(x1 + (radius*Math.cos(Math.PI - state.theta1))-lab_pos), 
                                                    (int)(y2)+lab_pos);
                                                     
                        
                        //g.setColor(colorE);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","i","",g,state.font16,
                                                    (int)(x2E-Math.sin(Math.PI*0.5-state.theta1)*lab_pos),
                                                    (int)(y2E+Math.cos(Math.PI*0.5-state.theta1)*lab_pos));
                }
                else{
                        MaestroG.fillCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),
                                    y2,(double)radiusH, state.s2, Color.white);
                
                        MaestroG.drawLineThick(g, x1+(radius*Math.cos(Math.PI - state.theta1))-(double)(radiusH/2),
                                    y2-(double)(radiusH/2),
                                    x1+(radius*Math.cos(Math.PI - state.theta1)) + (double)(radiusH/2),
                                    y2 + (double)(radiusH/2), state.s2,colorH);
                        MaestroG.drawLineThick(g, x1+(radius*Math.cos(Math.PI - state.theta1))+(double)(radiusH/2),
                                    y2-(double)(radiusH/2),
                                    x1+(radius*Math.cos(Math.PI - state.theta1)) - (double)(radiusH/2),
                                    y2 + (double)(radiusH/2), state.s2,colorH);
                        MaestroG.drawCircleThick(g, x1+(radius*Math.cos(Math.PI - state.theta1)),
                                    y2,(double)radiusH,state.s2,colorH);
                
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup2("H","//","i","",g,state.font16,
                                                    (int)(x1 + (radius*Math.cos(Math.PI - state.theta1))-lab_pos), 
                                                    (int)(y2)+lab_pos);
                        
                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","i","",g,state.font16,
                                                    (int)(x2E-Math.sin(Math.PI*0.5-state.theta1)*lab_pos),
                                                    (int)(y2E+Math.cos(Math.PI*0.5-state.theta1)*lab_pos));
             }
        }
	else{ // Perpendicular Polarization - Incident Wave
                x2 = (double)(x1+(int)(radius*Math.cos(Math.PI - state.theta1)));
                y2 = (double)(y1+(int)(radius*Math.sin(Math.PI - state.theta1)));
                if(state.theta1 == 0.0){x2 = x2 - offset; y2 = y2 + offset;}
                
                if(Field_Out){
                    g.setColor(Color.blue);
                    x2E = x1+(radius*Math.cos(Math.PI - state.theta1) + fieldL * Math.sin(state.theta1)); 
                    y2E = y1+(radius*Math.sin(Math.PI - state.theta1) + fieldL * Math.cos(state.theta1));
                    if(state.theta1 == 0.0){x2E = x2E - offset; y2E = y2E + offset;}
                    // H field vector
                    MaestroG.drawLineThick(g,x2,y2,x2E,y2E,thicknessV, colorH);

                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo+(3.0*Math.PI/2.0)),y2E-Len*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV,colorH);
                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2E-Len*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV,colorH);
                }
                else{
                    x2E = x1+(radius*Math.cos(Math.PI - state.theta1) - fieldL * Math.sin(state.theta1));
                    y2E = y1+(radius*Math.sin(Math.PI - state.theta1) - fieldL * Math.cos(state.theta1));
                    if(state.theta1 == 0.0){x2E = x2E - offset; y2E = y2E+offset;}
                    // H field vector
                    MaestroG.drawLineThick(g,x2,y2,x2E, y2E, thicknessV, colorH);

                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo+(Math.PI/2.0)),y2E-Len*Math.sin(angolo+(Math.PI/2.0)),thicknessV,colorH);
                    MaestroG.drawLineThick(g,x2E,y2E,x2E+Len*Math.cos(angolo2+(Math.PI/2.0)),y2E-Len*Math.sin(angolo2+(Math.PI/2.0)),thicknessV,colorH);
                }
                //An oval for electric field
                if(Field_Out){
                        if(state.theta1 == 0.0){x1 = x1 - offset;}
                        //MaestroG.fillCircleThick(g, x2,y2,(double)radiusH, state.s2, Color.white);
                        //MaestroG.drawCircleThick(g, x2,y2,(double)state.s2,state.s4,colorE);
                        //MaestroG.drawCircleThick(g, x2, y2,(double)radiusH,state.s2,colorE);
                        
                        MaestroG.fillCircleThick(g, x2, y2,(double)radiusH+state.s1,state.s2,colorE);
                        MaestroG.fillCircleThick(g, x2,y2,(double)(radiusH-state.s1), state.s2, Color.white);            
                        MaestroG.fillCircleThick(g, x2,y2,(double)state.s2,state.s4,colorE);
                        
                        g.setColor(colorE);                   
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        MaestroG.subscripterBsubsup("E","\u22a5","i","",g,state.font16,
                                                    (int)(x1 + (radius*Math.cos(Math.PI - state.theta1))) - lab_pos, 
                                                    (int)(y2) + lab_pos);
                                                     
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup("H","\u22a5","i","",g,state.font16,
                                                    (int)(x2E-Math.sin(Math.PI*0.5-state.theta1)*lab_pos),
                                                    (int)(y2E+Math.cos(Math.PI*0.5-state.theta1)*lab_pos));
                }
                else{
                        if(state.theta1 == 0.0){x1 = x1 - offset;}
                        MaestroG.fillCircleThick(g, x2,y2,(double)radiusH, state.s2, Color.white);
                                    
                        MaestroG.drawLineThick(g, x2-(double)(radiusH/2),
                                    y2-(double)(radiusH/2),x2 + (double)(radiusH/2),
                                    y2 + (double)(radiusH/2), state.s2,colorE);
                        MaestroG.drawLineThick(g, x2+(double)(radiusH/2),
                                    y2-(double)(radiusH/2),x2 - (double)(radiusH/2),
                                    y2 + (double)(radiusH/2), state.s2,colorE);
                        MaestroG.drawCircleThick(g, x2, y2,(double)radiusH,2,colorE);
                
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(colorE);
                        MaestroG.subscripterBsubsup("E","\u22a5","i","",g,state.font16,
                                                    (int)(x1 + (radius*Math.cos(Math.PI - state.theta1))-lab_pos), 
                                                    (int)(y2)+lab_pos);
                        
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup("H","\u22a5","i","",g,state.font16,
                                                    (int)(x2E-Math.sin(Math.PI*0.5-state.theta1)*lab_pos),
                                                    (int)(y2E+Math.cos(Math.PI*0.5-state.theta1)*lab_pos));
                }
        }
    }
    //-------------------------------------------------------------------------------------------
    
    public void paint(Graphics g){
	    if(im == null){
		im = createImage(getSize().width,getSize().height);
		buf = im.getGraphics();
		drawCanvas(buf);
	    }
	    else{
		drawCanvas(buf);
	    }
	    g.drawImage(im,0,0,null);
    }
    
    //------------------------------------------------------------------------------------------------
    
        private void drawRefTrans2(Graphics g, Color colore, int thickness){
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        int lab_pos = state.s28;
        double x1, y1, x2, y2, x2R, y2R, x2T, y2T;
        double angolobetaT, angolobetaT2;
        angolobetaT2 = (-state.theta2+Math.PI)+23.0*Math.PI/180.0; // THIS IS GOOD!!!
        angolobetaT = (-state.theta2+Math.PI)-23.0*Math.PI/180.0;  //     "   "
        
        radius = getSize().height/2-TopMargin-scale_radius*VGap/20;
        radius_powernew = 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 = (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 = (-state.theta1+Math.PI)+23.0*Math.PI/180.0;
        angolo = (-state.theta1+Math.PI)-23.0*Math.PI/180.0;
        
        angoloT2 = (state.theta2+Math.PI*0.5)+23.0*Math.PI/180.0;
        angoloT = (state.theta2+Math.PI*0.5)-23.0*Math.PI/180.0;
        
        radiusV = radius * 0.6;
        fieldL = radius * 0.4;
        thicknessV = state.s3;
        thicknessV2 = state.s2;
        thicknessV3 = 0;
        radiusH = state.s8;
        
        // Find Direction of normal field for the Reflected Component
        if(state.isPolarizationParallel){// Parallel Polarization
          
                if(Field_Out){
                    if(state.Reflection_Coef.Real() < 0.0){
                        Field_Refl_Out = true;
                    }
                    else{
                        Field_Refl_Out = false;
                    }
                }
                else{
                    if(state.Reflection_Coef.Real() < 0.0){
                        Field_Refl_Out = false;
                    }
                    else{
                        Field_Refl_Out = true;
                    }
                }
        }
        else{// Perpendicular Polarization
            
                if(Field_Out){
                    if(state.Reflection_Coef.Real() > 0.0){
                        Field_Refl_Out = true;
                    }
                    else{
                        Field_Refl_Out = false;
                    }
                }
                else{
                    if(state.Reflection_Coef.Real() < 0.0){
                        Field_Refl_Out = true;
                    }
                    else{
                        Field_Refl_Out = false;
                    }
                } 
        }
	  //reflected ray
	    
          //center coordinates
          x1 = (double)(3*getSize().width/4);
          y1 = (double)(getSize().height/2);
          
          // coordinates of beta vector root
          x2 = (double)(x1 +(int)(radius_powernew*Math.cos(Math.PI-state.theta1)));
          y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI-state.theta1)));
          if(state.theta1 == 0.0){y2 = y2-offset;}
          
          // coordinates of beta vector tip
          x2R = (double)(x1 +(int)((radius)*Math.cos(Math.PI-state.theta1)));
          y2R = (double)(getSize().height/2-(int)((radius)*Math.sin(Math.PI-state.theta1)));
          if(state.theta1 == 0.0){y2R = y2R - offset;}
          
          double vscaled;
          vscaled = state.beta_ratio*((radius + (double)state.s10) - radius_powernew2);
          
          {    
              // beta - reflected ray
              // Arrow head of vector, thick
              MaestroG.drawLineThick(g,x2R,y2R,x2R-Len*Math.cos(angolo),y2R+Len*Math.sin(angolo),thicknessV,colorV);
              MaestroG.drawLineThick(g,x2R,y2R,x2R-Len*Math.cos(angolo2),y2R+Len*Math.sin(angolo2),thicknessV,colorV);
                // stem
              if(state.theta1 != 0.0){MaestroG.drawLineThick(g,x1,y1,x2R,y2R,1,Color.gray);} // thin line to center
              MaestroG.drawLineThick(g,x2,y2,x2R,y2R,thicknessV,colorV);// vector line, thick
              g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
              MaestroG.subscripterB("k","r","",g,state.font18,(int)(x2+x2R)/2+state.s15,(int)(y2+y2R)/2-state.s15); // Ulaby's book notation
              // z-component
              MaestroG.drawLineThick(g,x2,y2,x2R,y2,1,colorV);
              //MaestroG.drawArrow((int)x2R+state.s8,(int)y2,8,g);
              MaestroG.drawArrowScaled((int)x2R+state.s5,(int)y2, 4, state.sfactor, g);
              // x-component
              MaestroG.drawLineThick(g,x2,y2,x2,y2R,1,colorV);
              //MaestroG.drawArrow((int)x2,(int)y2R+state.s9,5,g);
              MaestroG.drawArrowScaled((int)x2,(int)y2R+state.s5, 1, state.sfactor, g);
          }  
	  
          //--------------------------------------------------------------------
          if(!IsFocusOn){drawY(g);}
          //--------------------------------------------------------------------
          
          //------------------------------------------------------------------------------------
          x2E = x1+(radius_powernew*Math.sin(state.theta1) - fieldL * Math.cos(state.theta1));
          y2E = y1-(radius_powernew*Math.cos(state.theta1) + fieldL * Math.sin(state.theta1));
          if(state.theta1 == 0.0){y2E = y2E - offset;}
          
          if(state.isPolarizationParallel){ // Parallel Polarization
                double x2EE, y2EE;
              
                
                {  // REFLECTED WAVE STARTS HERE  *************************
                if(Field_Refl_Out){ // reflected wave - Field_out
                    if(state.Reflection_Coef.Imaginary() == 0.0){
                        double x22, y22;
                        int x11 = 3*getSize().width/4;
                        
                        x22 = (double)(x11 +(int)(radius_powernew*Math.cos(Math.PI-state.theta1)));
                        y22 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI-state.theta1)));
                        if(state.theta1 == 0.0){y22 = y22 - offset;}
                        
                        x2EE = x22 - fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.sin(state.theta1);
                        y2EE = y22 + fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.cos(state.theta1);
                        //if(state.theta1 == 0.0){y2EE = y2EE-20;}
                        // E field vector
                        MaestroG.drawLineThick(g, x22, y22, x2EE, y2EE, thicknessV, colorE);

                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE-LenR*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV,colorE);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE-LenR*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV,colorE);

                        //An oval for magnetic field

                        //MaestroG.fillCircleThick(g, x22, y22, (double)radiusH, 2, Color.white);
                        //MaestroG.drawCircleThick(g, x22, y22, (double)state.s2,state.s4,colorH);
                        //MaestroG.drawCircleThick(g, x22, y22, (double)radiusH,state.s2,colorH);
                        
                        MaestroG.fillCircleThick(g, x22, y22, (double)radiusH+state.s1,2,colorH);
                        MaestroG.fillCircleThick(g, x22, y22, (double)(radiusH-state.s1), 2, Color.white);
                        MaestroG.fillCircleThick(g, x22, y22, (double)state.s2,4,colorH);
                        
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(Color.blue);                   
                        MaestroG.subscripterBsubsup2("H","//","r","",g,state.font16,(int)(x22)+lab_pos/2, (int)(y22)-lab_pos/2);

                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","r","",g,state.font16,
                                                    (int)(x2EE-Math.sin(state.theta1)*lab_pos),
                                                    (int)(y2EE+Math.cos(state.theta1)*lab_pos));
                                                    //(int)(-20.0 + x2EE),(int)(y2EE));                    
                    }
                }
                else{// reflected wave - Field_In
                    if(state.Reflection_Coef.Imaginary() == 0.0){ // no total reflection
                        double x22, y22;
                        int x11 = 3*getSize().width/4;
                        x22 = (double)(x11 +(int)(radius_powernew*Math.cos(Math.PI-state.theta1)));
                        y22 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI-state.theta1)));
                        if(state.theta1 == 0.0){y22 = y22 - offset;}
                        
                        x2EE = x22 + fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.sin(state.theta1);
                        y2EE = y22 - fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.cos(state.theta1);
                        //if(state.theta1 == 0.0){y2EE = y2EE - offset;}
                        // E field vector
                        MaestroG.drawLineThick(g,x22, y22, x2EE, y2EE, thicknessV, colorE);
                        // arrow head
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE+LenR*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV,colorE);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE+LenR*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV,colorE);
                        
                        //An oval for magnetic field
                        MaestroG.fillCircleThick(g, x22, y22, (double)radiusH, state.s2, Color.white);
                        MaestroG.drawLineThick(g, x22 - (double)(radiusH/2), y22-(double)(radiusH/2),
                                        x22 + (double)(radiusH/2), y22 + (double)(radiusH/2), state.s2,colorH);
                        MaestroG.drawLineThick(g, x22 +(double)(radiusH/2), y22 -(double)(radiusH/2),
                                        x22 - (double)(radiusH/2), y22 + (double)(radiusH/2), state.s2,colorH);
                        MaestroG.drawCircleThick(g, x22,y22,(double)radiusH,state.s2,colorH);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(colorH);                   
                        if(state.theta1 == 0.0){MaestroG.subscripterBsubsup2("H","//","r","",g,state.font16,(int)x22+lab_pos/2,(int)y22-lab_pos/2);}
                        else{MaestroG.subscripterBsubsup2("H","//","r","",g,state.font16,(int)x22-lab_pos,(int)y22+lab_pos);}

                        g.setColor(colorE);                   
                        MaestroG.subscripterBsubsup2("E","//","r","",g,state.font16,
                                                    (int)(x2EE+Math.sin(state.theta1)*lab_pos/2),
                                                    (int)(y2EE-Math.cos(state.theta1)*lab_pos/2));
                    }
                }
              }
        }
	else{  // Perpendicular Polarization
               
                double x2EE, y2EE;
                
                if(Field_Refl_Out){ // reflected wave
                    
                        if(ShowREF && state.theta1_deg < 90.0 ){// Legend
                            MaestroG.drawLineThickB(g,state.s20,state.s70,state.s60,state.s70, thicknessV2, state.s3, state.s3, Color.blue);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup("Re(H","\u22a5","r",")",g,state.font14,state.s65,state.s75);
                            MaestroG.drawLineThickB(g,state.s20,state.s95,state.s60,state.s95, thicknessV2, state.s3, state.s3, Color.green.darker());
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup("Im(H","\u22a5","r",")",g,state.font14,state.s65,state.s100);
                        }
                    
                    double x22, y22;
                    int x11 = 3*getSize().width/4;
                    x22 = (double)(x11 +(int)(radius_powernew*Math.cos(Math.PI-state.theta1)));
                    y22 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI-state.theta1)));
                    if(state.theta1 == 0.0){y22 = y22 - offset;}
                    if(state.Reflection_CoefH.Imaginary() == 0.0){
                        x2EE = x22 + fieldL*(state.Reflection_CoefH.Magnitude()+extra) * Math.sin(Math.PI-state.theta1);
                        y2EE = y22 + fieldL*(state.Reflection_CoefH.Magnitude()+extra) * Math.cos(Math.PI-state.theta1);
                        //if(state.theta1 == 0.0){y2EE = y2EE - offset;}
                        // H field vector
                        MaestroG.drawLineThick(g,x22,y22,x2EE, y2EE,thicknessV, colorH);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR*Math.cos(angolo+3.0*Math.PI/2.0),y2EE+LenR*Math.sin(angolo+3.0*Math.PI/2.0),thicknessV,colorH);
                        MaestroG.drawLineThick(g,x2EE,y2EE,x2EE-LenR*Math.cos(angolo2+3.0*Math.PI/2.0),y2EE+LenR*Math.sin(angolo2+3.0*Math.PI/2.0),thicknessV,colorH);
                        
                        g.setColor(colorH);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        MaestroG.subscripterBsubsup("H","\u22a5","r","",g,state.font16,
                                                (int)(x2EE - Math.cos(state.theta1)*lab_pos),
                                                (int)(y2EE - Math.sin(state.theta1)*lab_pos/2));
                    }
                    
                    //An oval for electric field

                    //MaestroG.fillCircleThick(g, x22,y22,(double)radiusH, state.s2, Color.white);
                    //MaestroG.drawCircleThick(g, x22,y22,(double)state.s2,state.s4,colorE);
                    //MaestroG.drawCircleThick(g, x22,y22,(double)radiusH,state.s2,colorE);

                    MaestroG.fillCircleThick(g, x22, y22,(double)radiusH+state.s1,2,colorH);
                    MaestroG.fillCircleThick(g, x22, y22,(double)(radiusH-state.s1), 2, Color.white);
                    MaestroG.fillCircleThick(g, x22, y22,(double)state.s2,4,colorH);
                            
                    g.setColor(colorE);                   
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    if(state.theta1 == 0.0){MaestroG.subscripterBsubsup("E","\u22a5","r","",g,state.font16,(int)x22+lab_pos/2,(int)y22-lab_pos/2);}
                    else{MaestroG.subscripterBsubsup("E","\u22a5","r","",g,state.font16,(int)x22-lab_pos,(int)y22+lab_pos);}
                    
                }
                else{ //Field IN
                    
                        if(ShowREF && state.theta1_deg < 90.0 ){// Legend
                            MaestroG.drawLineThickB(g,20,70,60,70, thicknessV2, state.s3, state.s3, Color.blue);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup("Re(H","\u22a5","r",")",g,state.font14,state.s65,state.s75);
                            MaestroG.drawLineThickB(g,20,95,60,95, thicknessV2, state.s3, state.s3, Color.green.darker());
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterBsubsup("Im(H","\u22a5","r",")",g,state.font14,state.s65,state.s100);
                        }
                    
                    double x22, y22;
                    int x11 = 3*getSize().width/4;
                    x22 = (double)(x11 +(int)(radius_powernew*Math.cos(Math.PI-state.theta1)));
                    y22 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI-state.theta1)));
                    if(state.theta1 == 0.0){y22 = y22 - offset;}
                    if(state.Reflection_CoefH.Imaginary() == 0.0){
                        x2EE = x22 - fieldL*(state.Reflection_CoefH.Magnitude()+extra) * Math.sin(Math.PI-state.theta1);
                        y2EE = y22 - fieldL*(state.Reflection_CoefH.Magnitude()+extra) * Math.cos(Math.PI-state.theta1);
                        //if(state.theta1 == 0.0){y2EE = y2EE - offset;}
                        
                        // H field vector
                        
                            MaestroG.drawLineThick(g,x22,y22,x2EE,y2EE,thicknessV, colorH);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR*Math.cos(angolo+(3.0*Math.PI/2.0)),y2EE-LenR*Math.sin(angolo+(3.0*Math.PI/2.0)),thicknessV,colorH);
                            MaestroG.drawLineThick(g,x2EE,y2EE,x2EE+LenR*Math.cos(angolo2+(3.0*Math.PI/2.0)),y2EE-LenR*Math.sin(angolo2+(3.0*Math.PI/2.0)),thicknessV,colorH);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            g.setColor(colorH);                   
                            MaestroG.subscripterBsubsup("H","\u22a5","r","",g,state.font16,
                                                (int)(x2EE-Math.sin(state.theta1)*lab_pos),
                                                (int)(y2EE+Math.cos(state.theta1)*lab_pos));
                    }
                    //----------------------------------------------------------
                    //An oval for electric field

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

                    MaestroG.drawLineThick(g, x22-(double)(radiusH/2), y22-(double)(radiusH/2),
                                    x22 + (double)(radiusH/2), y22 + (double)(radiusH/2), state.s2,colorE);
                    MaestroG.drawLineThick(g, x22+(double)(radiusH/2), y22-(double)(radiusH/2),
                                    x22 - (double)(radiusH/2), y22 + (double)(radiusH/2), state.s2,colorE);
                        
                    MaestroG.drawCircleThick(g, x22, y22, (double)radiusH,state.s2,colorE);
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    g.setColor(colorE);                   
                    MaestroG.subscripterBsubsup("E","\u22a5","r","",g,state.font16,(int)x22+lab_pos/2,(int)y22-lab_pos/2);
                }
         }
    }
    //-------------------------------------------------------------------------------------------------
    private void drawScaleRefTrans2(Graphics g, Color colore, int thickness){
            double x1, y1, x2, y2, x3, y3;
            
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    	
	  /*
          //transmitted ray
	  if(state.Reflection_Coef.Imaginary() == 0.0){
              x1 = (double)(getSize().width/2);
              y1 = (double)(getSize().height/2);
              x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(state.theta2)*(1.0-state.Reflection_Coef.Magnitude()*
                    state.Reflection_Coef.Magnitude())));
              y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(state.theta2)*(1.0-state.Reflection_Coef.Magnitude()*
                    state.Reflection_Coef.Magnitude())));
              x3 = (double)(getSize().width/2 +(int)(radius_powernew*Math.cos(state.theta2)));
              y3 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(state.theta2)));
              if(state.theta1_deg < 90.0){MaestroG.drawLineThick(g, x1, y1, x3, y1, thickness, Color.green);}
              MaestroG.drawLineThick(g, x1, y1, x2, y1, thickness, colore);
          }
          */
        
          //reflected ray
        
          //x1 = (double)(getSize().width/2);
          x1 = (double)(3*getSize().width/4);
          y1 = (double)(getSize().height/2);
          
          x2 = (double)(x1 +(int)(radius_powernew*Math.cos(Math.PI - state.theta1)*state.Reflection_Coef.Magnitude()*
		state.Reflection_Coef.Magnitude()));
          y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI - state.theta1)*state.Reflection_Coef.Magnitude()* 
		state.Reflection_Coef.Magnitude()));
          x3 = (double)(x1 +(int)(radius_powernew*Math.cos(Math.PI - state.theta1)));
          y3 = (double)(getSize().height/2-(int)(radius_powernew*Math.sin(Math.PI - state.theta1)));
          MaestroG.drawLineThick(g, x1, y1-state.s2, x3, y3-state.s2, thickness, Color.green);
          MaestroG.drawLineThick(g, x1, y1-state.s2, x2, y2-state.s2, thickness, colore);

    }
    
    
    public void update(Graphics g){
	paint(g);
    }
    
    public void mouseClicked(MouseEvent evt){
        
        // To make power flow appear
        //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){}
}
