import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ObliqueCanvasB 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 double LenVert, LenHor;
    private int HGap, VGap;//for vectors fields.
    private Image im;
    private Graphics buf;
    
    private double vettoreL;
    private double extra = 0.1;
    private Oblique_State state;
    private int radius, radius_power, radius_powernew, scale_radius;
    private boolean IsFocusOn, IsPrintOn;
    public boolean Field_Out, Field_Refl_Out, show_normal, show_tang;
    private Color incident = Color.red; 

    private BufferedImage arrow_inf;
    
    public ObliqueCanvasB(Oblique_State state){
	super();
	this.state = state;
	getImages();
        
        LeftMargin = state.s10;
        RightMargin = state.s10;
        TopMargin = state.s10;
        BottomMargin = state.s10;
        
        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);

        HGap = state.s10;
        VGap = state.s10;//for vectors fields.
    
    
        vettoreL = (double)state.s100;
        scale_radius = 150;
    
        
	IsFocusOn = false;
        IsPrintOn = false;
        Field_Out = false;
        Field_Refl_Out = true;
        show_normal = true; // selects field normal or parallel to plane of incidence
        show_tang = true; // selects components of the field parallel to plane of 
                          // incidence: true = show tangent component / false = show perp component  
        
	setBackground(bgcolor);
	//Listeners
	this.addMouseListener(this);
        
        radius = getSize().height/2-TopMargin-5*VGap/2-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("2. 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);
	drawLabels(g);
	drawBackLabels(g);
        {
            drawWaves2(g);
        }
        drawY(g);
    }
    
    private void drawMedium(Graphics g){/// OK WITH ULABY's BOOK CONVENTIONS
	g.setColor(medium1Color);
	g.fillRect(1,1,3*getSize().width/4-1,getSize().height-2);
	g.setColor(medium2Color);
	g.fillRect(3*getSize().width/4,1,getSize().width/4-1,getSize().height-2);
    }
    
    private void drawAxis(Graphics g){/// OK WITH ULABY's BOOK CONVENTIONS
	g.setColor(Color.black);
	//horizontal axis - z
	g.drawLine(LeftMargin,getSize().height/2,getSize().width-RightMargin-state.s10,getSize().height/2);
	//MaestroG.drawArrow( getSize().width-RightMargin-state.s10, getSize().height/2, 7, g);
        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(3*getSize().width/4,TopMargin,3*getSize().width/4,getSize().height-BottomMargin - state.s10);
        //MaestroG.drawArrow( 3*getSize().width/4, TopMargin + state.s10, 5, g);
        MaestroG.drawArrowScaled(3*getSize().width/4, TopMargin + state.s10, 1, state.sfactor, g);
    }
    
    private void drawY(Graphics g){/// OK WITH ULABY's BOOK CONVENTIONS
        double x1, y1, radiusy, radiusX, radiusdot; 
        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());
	
	tmp = "x";
	g.drawString(tmp,3*getSize().width/4-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,(3*getSize().width/4+fm.stringWidth(tmp)),getSize().height/2+fm.getHeight());
	
        //g.setFont(normalfontB);
        //g.setColor(Color.red.darker());
	//tmp =  "medium 1";
	//g.drawString(tmp,LeftMargin +10,getSize().height-7*VGap);
	//g.setColor(Color.blue.darker());
	//tmp  = "medium 2";
	//g.drawString(tmp,getSize().width-95,getSize().height-7*VGap);
        
        g.setFont(normalfontB);
        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);
    }
    
    private void drawBackLabels(Graphics g){/// DONE
	Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        int mediumX, mediumY;
	radius = getSize().height/2-TopMargin-scale_radius*VGap/20;
	FontMetrics fm = g.getFontMetrics();
	//For medium 1
	mediumX = LeftMargin+state.s5;
	mediumY = 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(normalfontB); 
	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;
        
        /*
        MaestroG.subscripter("\u03b5","r 2"," = "+state.epsilon_r2,g,12,mediumX+7,mediumY);    
	g.setFont(normalfont); 
	fm = g.getFontMetrics();
	mediumY += 4*fm.getHeight()/3;
	MaestroG.subscripter("\u03bc","r 2"," = "+state.mu_r2,g,12,mediumX+5,mediumY);
        */ 
        
        g.setFont(normalfont); 
	fm = g.getFontMetrics();
        
	mediumY += fm.getHeight();
	//MaestroG.subscripter("\u03c3","2"," \u2192 \u221e  [S/m]", g, state.font14, mediumX,mediumY);
	MaestroG.SubTxtImgTxt("\u03c3","2",arrow_inf,"  [S/m]", g, state.font14, mediumX,mediumY,this);
	
	g.setFont(normalfont);
	fm = g.getFontMetrics();
    }
    
    private void drawWaves2(Graphics g){
        double x1, y1, x2, y2;
        int thickness = state.s6;
            
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
	boolean Scaled_Waves = true;
	radius = 3*getSize().height/4 - TopMargin - 6*VGap - state.s20;
        radius_powernew = radius*40/100;
        
	//drawing in medium 1
	g.setColor(incident);
	//incident ray
        double Len = state.s15;
        LenVert = (double)state.s15 * Math.pow(Math.sin(state.theta1),0.5);
        LenHor = (double)state.s15 * Math.pow(Math.cos(state.theta1),0.5);
        
        double radiusV, fieldL, x2_power, y2_power;
        int thicknessV, radiusH;
        double angolo, angolo2, x2V, y2V, x2E, y2E;
        Color colorV = Color.black;
        Color colorE = Color.magenta.darker();
        Color colorH = Color.blue;
        
        angolo2 = (state.theta1+Math.PI*0.5)+23.0*Math.PI/180.0;
        angolo = (state.theta1+Math.PI*0.5)-23.0*Math.PI/180.0;
        radiusV = radius * 0.4;
        fieldL = 1.1*(radius * 0.4);
        thicknessV = state.s4;
        radiusH = state.s16;
        
        
        // center coordinates
          x1 = (double)(3*getSize().width/4);
          y1 = (double)(getSize().height/2);
        
        // coordinates of beginning of incident line
          x2 = (double)(x1-(int)(radius*Math.sin(state.theta1)));
          y2 = (double)(y1-(int)(radius*Math.cos(state.theta1)));
          
          x2_power = (double)(x1-(int)(radius_powernew*Math.sin(state.theta1)));
          y2_power = (double)(y1-(int)(radius_powernew*Math.cos(state.theta1)));
          
          double xo, yo, ango, ango2, rado;
          xo = x1 - (double)state.s15;
          yo = y1 - (double)state.s50;
          rado = Math.sqrt(xo*xo + yo*yo) + state.s5;
          ango = state.theta1 * 0.5;
          ango2 = state.theta2 * 0.5;
          
        // coordinates of incident vector tip
          x2V = (double)(x1-(int)(radiusV*Math.sin(state.theta1)));
          y2V = (double)(y1-(int)(radiusV*Math.cos(state.theta1)));
          
          //if(IsFocusOn){MaestroG.drawLineThick(g, x1, y1, x2_power, y2_power, thickness, incident);}
          
        {
	    // CALL ROUTINE TO DRAW REFLECTED WAVE
            drawRefTrans2(g, incident, thickness);
            //if(IsFocusOn){drawScaleRefTrans2(g, incident, thickness);}
	}
	
             int radiusnewx = state.s50;// these two lines position normal field near interface
             int radiusnewy = state.s50;
        
        // DRAW INCIDENT WAVE HERE =========================================================
             double y1shiftup = y1 - (double)state.s10;
             double y1shiftup2 = y1 + 2*radiusH;
             double xshift = (double)radiusnewx;
             double xmore = (double)state.s30;
             
    	if(state.isPolarizationParallel){ // Parallel Polarization - Incident Wave
                double yshift = (double)state.s5;
                double xshift2 = (double)state.s12;
                Color colorino;
                
                if(show_normal){
                    colorino = colorH;
                }
                else{
                    colorino = colorE;
                }
                g.setColor(colorE);
                MaestroG.subscriptersupBs("E = 0","","","",g,state.font16,(int)(x1 + 3*radiusnewx/2), 
                                                         (int)(y1 - 3*radiusnewy));
                g.setColor(colorH);
                MaestroG.subscriptersupBs("H = 0","","","",g,state.font16,(int)(x1 + 3*radiusnewx/2), 
                                                         (int)(y1 - 3*radiusnewy + state.s25));
                if(show_normal){
                    // make a plus sign
                    {
                    MaestroG.drawLineThick(g, x1-(double)(radiusH/2)-xshift-xmore,y1, 
                                    x1+(double)(radiusH/2)-xshift-xmore,y1, state.s4,colorino);
                    MaestroG.drawLineThick(g, x1-xshift-xmore, y1+(double)(radiusH/2), 
                                    x1-xshift-xmore, y1-(double)(radiusH/2), state.s4,colorino);
                    }
                    // make an equal sign
                    if(state.theta1_deg < (90.0-0.000005)){
                        MaestroG.drawLineThick(g, x1-radiusH-xshift2, y1-(double)(radiusH/2) + state.s2, 
                                    x1-2*radiusH-xshift2, y1-(double)(radiusH/2) + state.s2, state.s4,colorino);
                        MaestroG.drawLineThick(g, x1-radiusH-xshift2, y1+(double)(radiusH/2) - state.s2, 
                                    x1-2*radiusH-xshift2, y1+(double)(radiusH/2)-2, state.s4,colorino);
                    }
                }
                else{
                    if(show_tang){
                        // make a plus sign
                        {
                            MaestroG.drawLineThick(g, x1-(double)(radiusH/2)-xshift-xmore,y1, 
                                            x1+(double)(radiusH/2)-xshift-xmore,y1,state.s4,colorino);
                            MaestroG.drawLineThick(g, x1-xshift-xmore, y1-(double)(radiusH/2), 
                                            x1-xshift-xmore, y1+(double)(radiusH/2), state.s4,colorino);
                        }
                        // make an equal sign
                        if(state.theta1_deg < (90.0-0.000005)){
                            MaestroG.drawLineThick(g, x1-radiusH-xshift2, y1-(double)(radiusH/2) + state.s2, 
                                    x1-2*radiusH-xshift2, y1-(double)(radiusH/2) + state.s2, state.s4,colorino);
                            MaestroG.drawLineThick(g, x1-radiusH-xshift2, y1+(double)(radiusH/2)-state.s2, 
                                    x1-2*radiusH-xshift2, y1+(double)(radiusH/2) - state.s2, state.s4,colorino);
                        }
                    }
                    else{
                        // make a plus sign
                        {
                            // make a plus sign
                            MaestroG.drawLineThick(g, x1-(double)(radiusH/2)-2*xshift,y1, 
                                    x1+(double)(radiusH/2)-2*xshift,y1, state.s4,colorino);
                            MaestroG.drawLineThick(g, x1-2*xshift, y1+(double)(radiusH/2), 
                                    x1-2*xshift, y1-(double)(radiusH/2), state.s4,colorino);
                        }
                        // make an equal sign
                        if(state.theta1_deg < (90.0-0.000005)){
                            MaestroG.drawLineThick(g, x1-radiusH-xshift2, y1-(double)(radiusH/2) + state.s2, 
                                    x1 - 2*radiusH-xshift2, y1-(double)(radiusH/2) + state.s2, state.s4,colorino);
                            MaestroG.drawLineThick(g, x1-radiusH-xshift2, y1+(double)(radiusH/2) - state.s2, 
                                    x1-2*radiusH-xshift2, y1+(double)(radiusH/2) - state.s2, state.s4,colorino);
                        }
                    }
                }
                
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                if(Field_Out){
                        x2E = x1-(radius*Math.sin(state.theta1) - fieldL * Math.cos(state.theta1));
                        y2E = y1-(radius*Math.cos(state.theta1) + fieldL * Math.sin(state.theta1));
                    
                    if(!show_normal){
                        // E field vector        
                        if(show_tang){
                            
                            ArrowUP(g, x1-radiusnewx-xmore, y1+radiusnewy+vettoreL, vettoreL, colorE, thicknessV); //*
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            g.setColor(colorE);                   
                            MaestroG.subscriptersupBs2("E","i","//","x","",g,state.font16,
                                                    (int)(x1 - vettoreL-xmore),(int)(y1 + 2*radiusnewy ));
                            
                            MaestroG.subscripterB("0","","",g,state.font36,(int)(x1 + xmore),(int)(y1 + state.s12 ));
                            MaestroG.subscripterB("Incident Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                     (int)(y1 + 3.0*(radiusnewy)));
                        }
                        else{
                            ArrowLEFT(g, x1-radiusnewx-vettoreL, y1+radiusnewy, vettoreL, colorE, thicknessV);//*
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            g.setColor(colorE);                   
                            MaestroG.subscriptersupBss2("\u03b5","1","E","i","//","z","",g,state.font16,
                                                    (int)(x1 - 5*vettoreL/4),(int)(y1 + 13*radiusnewy/8 ));
                            
                            MaestroG.subscripterB("Incident Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                                            (int)y1 + (int)(3.0*radiusnewy));
                        }
                    }
                }
                else{
                    x2E = x1-(radius*Math.sin(state.theta1) + fieldL * Math.cos(state.theta1));
                    y2E = y1-(radius*Math.cos(state.theta1) - fieldL * Math.sin(state.theta1));
                    
                    if(!show_normal){
                        // E field vector
                        if(show_tang){
                            
                            ArrowDOWN(g, x1-radiusnewx-xmore, y1+radiusnewy+vettoreL, vettoreL, colorE, thicknessV);//*
                            
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            g.setColor(colorE);                   
                            MaestroG.subscriptersupBs2("E","i","//","x","",g,state.font16,
                                                    (int)(x1 - vettoreL-xmore),(int)(y1 + 2*radiusnewy ));
                            MaestroG.subscripterB("0","","",g,state.font36,(int)(x1 + xmore),(int)(y1 + state.s12 ));
                            
                            MaestroG.subscripterB("Incident Wave","","",g,state.font14,
                                                    (int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                     (int)( y1 + 3.0*(radiusnewy)));
                        }
                        else{
                            ArrowRIGHT(g, x1-radiusnewx-vettoreL, y1+radiusnewy, vettoreL, colorE, thicknessV);//*
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            g.setColor(colorE);                   
                            MaestroG.subscriptersupBss2("\u03b5","1","E","i","//","z","",g,state.font16,
                                                    (int)(x1 - 5*vettoreL/4),(int)(y1 + 13*radiusnewy/8 ));
                            
                            MaestroG.subscripterB("Incident Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore)-state.s85, 
                                                     (int)(y1 + 3.0*(radiusnewy)));
                        }
                    }
                }
            
             if(show_normal){    
                //An oval for magnetic field
                
                if(Field_Out){
                        MaestroG.fillCircleThick(g, x1-radiusnewx-xmore, y1+(radiusnewy), (double)radiusH, state.s2, Color.white);    
                        MaestroG.fillCircleThick(g, x1-radiusnewx-xmore, y1+radiusnewy, (double)state.s5, state.s2,colorH);
                        MaestroG.drawCircleThick(g, x1-radiusnewx-xmore, y1+radiusnewy, (double)radiusH, state.s4,colorH);
                
                        g.setColor(Color.blue);  
                        
                        // INCIDENT 
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        MaestroG.subscripterB("Incident Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                     (int)(y1 + 3.0*(radiusnewy)));
                        
                        MaestroG.subscriptersupBs2("H","i","//","y","",g,state.font16,(int)(x1 - 5*radiusnewx/2-xmore), 
                                                     (int)(y1 + radiusnewy));
                }
                else{
                        MaestroG.fillCircleThick(g, x1-(radiusnewx)-xmore, y1+(radiusnewy), (double)radiusH, state.s2, Color.white);
                
                        MaestroG.drawLineThick(g, x1-(radiusnewx)-(double)(radiusH/2)-xmore,
                                    y1+(radiusnewy)-(double)(radiusH/2),
                                    x1-(radiusnewx) + (double)(radiusH/2)-xmore,
                                    y1+(radiusnewy) + (double)(radiusH/2), state.s4,colorH);
                        MaestroG.drawLineThick(g, x1-(radiusnewx)+(double)(radiusH/2)-xmore,
                                    y1+(radiusnewy)-(double)(radiusH/2),
                                    x1-(radiusnewx) - (double)(radiusH/2)-xmore,
                                    y1+(radiusnewy) + (double)(radiusH/2), state.s4,colorH);
                        MaestroG.drawCircleThick(g, x1-(radiusnewx)-xmore,
                                    y1+(radiusnewy),
                                    (double)radiusH,state.s4,colorH);
                
                        g.setColor(Color.blue);                   
                        // INCIDENT 
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        MaestroG.subscripterB("Incident Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                     (int)(y1 + 3.0*(radiusnewy)));
                        
                        
                        MaestroG.subscriptersupBs2("H","i","//","y","",g,state.font16,(int)(x1 - 5*radiusnewx/2-xmore), 
                                                     (int)(y1 + radiusnewy));
               }
            }
        }
             
	else{ // Perpendicular Polarization - Incident Wave    
                double yshift = (double)state.s5;
                double xshift2 = (double)state.s12;
                Color colorino;
                
                if(show_normal){
                    colorino = colorE;
                }
                else{
                    colorino = colorH;
                }
                
                g.setColor(colorE);
                MaestroG.subscriptersupBs("E = 0","","","",g,state.font16,(int)(x1 + 3*radiusnewx/2), 
                                                         (int)(y1 - 3*radiusnewy));
                g.setColor(colorH);
                MaestroG.subscriptersupBs("H = 0","","","",g,state.font16,(int)(x1 + 3*radiusnewx/2), 
                                                         (int)(y1 - 3*radiusnewy + state.s25));
                
                if(show_normal){
                    // make a plus sign
                    MaestroG.drawLineThick(g, x1-(double)(radiusH/2)-xshift-xmore,y1, 
                                    x1+(double)(radiusH/2)-xshift-xmore,y1, state.s4,colorino);
                    MaestroG.drawLineThick(g, x1-xshift-xmore, y1+(double)(radiusH/2), 
                                    x1-xshift-xmore, y1-(double)(radiusH/2), state.s4,colorino);
                    
                    // make an equal sign
                    if(state.theta1_deg < (90.0-0.000005)){
                        MaestroG.drawLineThick(g, x1-radiusH-xshift2, y1-(double)(radiusH/2) + state.s2, 
                                    x1-2*radiusH-xshift2, y1-(double)(radiusH/2) + state.s2, state.s4,colorino);
                        MaestroG.drawLineThick(g, x1-radiusH-xshift2, y1+(double)(radiusH/2) - state.s2, 
                                    x1-2*radiusH-xshift2, y1+(double)(radiusH/2) - state.s2, state.s4,colorino);
                    }
                }
                else{
                    if(show_tang){
                        // make a plus sign
                        MaestroG.drawLineThick(g, x1-(double)(radiusH/2)-xshift-xmore,y1, 
                                    x1+(double)(radiusH/2)-xshift-xmore,y1, state.s4,colorino);
                        MaestroG.drawLineThick(g, x1-xshift-xmore, y1+(double)(radiusH/2), 
                                    x1-xshift-xmore, y1-(double)(radiusH/2), state.s4,colorino);
                        // make an equal sign
                        if(state.theta1_deg < (90.0-0.000005)){
                            MaestroG.drawLineThick(g, x1-radiusH-xshift2, y1-(double)(radiusH/2) + state.s2, 
                                    x1-2*radiusH-xshift2, y1-(double)(radiusH/2) + state.s2, state.s4,colorino);
                            MaestroG.drawLineThick(g, x1-radiusH-xshift2, y1+(double)(radiusH/2) - state.s2, 
                                    x1-2*radiusH-xshift2, y1+(double)(radiusH/2) - state.s2, state.s4,colorino);
                        }
                    }
                    else{
                        // make a plus sign
                            MaestroG.drawLineThick(g, x1-(double)(radiusH/2)-2*xshift,y1, 
                                    x1+(double)(radiusH/2)-2*xshift,y1, state.s4,colorino);
                            MaestroG.drawLineThick(g, x1-2*xshift, y1+(double)(radiusH/2), 
                                    x1-2*xshift, y1-(double)(radiusH/2), state.s4,colorino);
                    
                        // make an equal sign
                        if(state.theta1_deg < (90.0-0.000005)){
                            MaestroG.drawLineThick(g, x1-radiusH-xshift2, y1-(double)(radiusH/2) + state.s2, 
                                    x1-2*radiusH-xshift2, y1-(double)(radiusH/2) + state.s2, state.s4,colorino);
                            MaestroG.drawLineThick(g, x1-radiusH-xshift2, y1+(double)(radiusH/2) - state.s2, 
                                    x1-2*radiusH-xshift2, y1+(double)(radiusH/2) - state.s2, state.s4,colorino);
                        }
                    }
                }
                
                if(Field_Out){
                    g.setColor(Color.blue);
                    x2E = x1-(radius*Math.sin(state.theta1) + fieldL * Math.cos(state.theta1));
                    y2E = y1-(radius*Math.cos(state.theta1) - fieldL * Math.sin(state.theta1));

                    if(!show_normal){// H field vector
                        if(show_tang){
                            
                            //ArrowLEFT(g, x1-radiusnewx-vettoreL, y1+radiusnewy, vettoreL, colorH, thicknessV);
                            ArrowDOWN(g, x1-radiusnewx-xmore, y1+radiusnewy+vettoreL, vettoreL, colorH, thicknessV);//*
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            g.setColor(colorH);                   
                            MaestroG.subscriptersupBs("H","i","\u22a5x","",g,state.font16,
                                                    (int)(x1 - vettoreL-xmore),(int)(y1 + 2*radiusnewy ));
                            
                            MaestroG.subscripterB("Incident Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                     (int)(y1 + 3.0*(radiusnewy)));
                        }
                        else{
                             
                            ArrowRIGHT(g, x1-radiusnewx-vettoreL, y1+radiusnewy, vettoreL, colorH, thicknessV);//*
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            g.setColor(colorH);                   
                            MaestroG.subscriptersupBss("\u03bc","1","H","i","\u22a5z","",g,state.font16,
                                                    (int)(x1 - 5*vettoreL/4),(int)(y1 + 13*radiusnewy/8 ));
                            
                            MaestroG.subscripterB("0","","",g,state.font36,(int)(x1 + xmore),(int)(y1 + state.s12 ));
                            
                            MaestroG.subscripterB("Incident Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                     (int)(y1 + 3.0*(radiusnewy)));
                        }
                    }
                }
                else{
                    x2E = x1-(radius*Math.sin(state.theta1) - fieldL * Math.cos(state.theta1));
                    y2E = y1-(radius*Math.cos(state.theta1) + fieldL * Math.sin(state.theta1));

                    if(!show_normal){
                        // H field vector
                        if(show_tang){
                            ArrowUP(g, x1-radiusnewx-xmore, y1+radiusnewy+vettoreL, vettoreL, colorH, thicknessV);//*
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            g.setColor(colorH);                   
                            MaestroG.subscriptersupBs("H","i","\u22a5x","",g,state.font16,
                                                    (int)(x1 - vettoreL-xmore),(int)(y1 + 2*radiusnewy ));
                            
                            MaestroG.subscripterB("Incident Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                         (int)(y1 + 3.0*(radiusnewy)));
                        }
                        else{
                            ArrowLEFT(g, x1-radiusnewx-vettoreL, y1+radiusnewy, vettoreL, colorH, thicknessV);//*
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            g.setColor(colorH);                   
                            MaestroG.subscriptersupBss("\u03bc","1","H","i","\u22a5z","",g,state.font16,
                                                    (int)(x1 - 5*vettoreL/4),(int)(y1 + 13*radiusnewy/8 ));
                            
                            MaestroG.subscripterB("0","","",g,state.font36,(int)(x1 + xmore),(int)(y1 + state.s12 ));
                            
                            MaestroG.subscripterB("Incident Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                         (int)(y1 + 3.0*(radiusnewy)));
                        }
                    }
                }
                //An oval for electric field
                if(Field_Out){
                        if(show_normal){
                            MaestroG.fillCircleThick(g, x1-(radiusnewx)-xmore, y1+(radiusnewy), (double)radiusH, state.s4, Color.white);
                            MaestroG.fillCircleThick(g, x1-(radiusnewx)-xmore, y1+(radiusnewy), (double)state.s5, state.s2,colorE);
                            MaestroG.drawCircleThick(g, x1-(radiusnewx)-xmore, y1+(radiusnewy), (double)radiusH, state.s4,colorE);
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            g.setColor(colorE);                   
                            // INCIDENT 
                            MaestroG.subscripterB("Incident Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)) - state.s85, 
                                                         (int)(y1 + 3.0*(radiusnewy)));

                            MaestroG.subscriptersupBs("E","i","\u22a5y","",g,state.font16,(int)(x1 - 5*radiusnewx/2-xmore), 
                                                         (int)(y1 + radiusnewy));
                            
                            MaestroG.subscripterB("0","","",g,state.font36,(int)(x1 + xmore),(int)(y1 + state.s12 ));
                        }
                }
                else{
                        if(show_normal){
                            MaestroG.fillCircleThick(g, x1-(radiusnewx)-xmore, y1+(radiusnewy), (double)radiusH, state.s4, Color.white);

                            MaestroG.drawLineThick(g, x1-(radiusnewx)-(double)(radiusH/2)-xmore,
                                        y1+(radiusnewy)-(double)(radiusH/2), 
                                        x1-(radiusnewx) + (double)(radiusH/2)-xmore,
                                        y1+(radiusnewy) + (double)(radiusH/2), state.s4,colorE);
                            MaestroG.drawLineThick(g, x1-(radiusnewx)+(double)(radiusH/2)-xmore,
                                        y1+(radiusnewy)-(double)(radiusH/2),
                                        x1-(radiusnewx) - (double)(radiusH/2)-xmore,
                                        y1+(radiusnewy) + (double)(radiusH/2), state.s4,colorE);
                            MaestroG.drawCircleThick(g, x1-(radiusnewx)-xmore,
                                        y1+(radiusnewy), (double)radiusH,state.s4,colorE);

                            g.setColor(colorE);
                            // INCIDENT 
                            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                            MaestroG.subscripterB("Incident Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                         (int)(y1 + 3.0*(radiusnewy)));

                            MaestroG.subscriptersupBs("E","i","\u22a5y","",g,state.font16,(int)(x1 - 5*radiusnewx/2-xmore), 
                                                         (int)(y1 + radiusnewy));   
                            
                            MaestroG.subscripterB("0","","",g,state.font36,(int)(x1 + xmore),(int)(y1 + state.s12 ));
                        }
                }
        }
	
    }
    //-------------------------------------------------------------------------------------------
    
    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);
    }
    
    //------------------------------------------------------------------------------------------------
    //BOUNDARY CONDITIONS ARROWS
    //------------------------------------------------------------------------------------------------
    //
    
        private void ArrowUP(Graphics g, double xstart, double ystart, double xlength, Color colore, int thickness){
            double LenR = (double)state.s15;
            
            MaestroG.drawLineThick(g,xstart,ystart, xstart, ystart-xlength, thickness, colore);
    
            MaestroG.drawLineThick(g,xstart, ystart - xlength, xstart - LenR * Math.sin(Math.PI/9.0),
                      ystart - xlength + LenR*Math.cos(Math.PI/9.0),thickness,colore);
            MaestroG.drawLineThick(g,xstart, ystart - xlength, xstart + LenR * Math.sin(Math.PI/9.0),
                      ystart - xlength + LenR*Math.cos(Math.PI/9.0),thickness,colore);
        }
        
        private void ArrowDOWN(Graphics g, double xstart, double ystart, double xlength, Color colore, int thickness){
            double LenR = (double)state.s15;
            
            MaestroG.drawLineThick(g,xstart,ystart, xstart, ystart-xlength, thickness, colore);
    
            MaestroG.drawLineThick(g,xstart, ystart, xstart - LenR * Math.sin(Math.PI/9.0),
                      ystart - LenR*Math.cos(Math.PI/9.0),thickness,colore);
            MaestroG.drawLineThick(g,xstart, ystart, xstart + LenR * Math.sin(Math.PI/9.0),
                      ystart - LenR*Math.cos(Math.PI/9.0),thickness,colore);
        }
        
        private void ArrowLEFT(Graphics g, double xstart, double ystart, double xlength, Color colore, int thickness){
            double LenR = (double)state.s15;
            
            MaestroG.drawLineThick(g,xstart,ystart, xstart + xlength, ystart, thickness, colore);
    
            MaestroG.drawLineThick(g,xstart, ystart,xstart + LenR * Math.cos(Math.PI/9.0),
                      ystart - LenR*Math.sin(Math.PI/9.0),thickness,colore);
            MaestroG.drawLineThick(g,xstart, ystart ,xstart + LenR * Math.cos(Math.PI/9.0),
                      ystart + LenR*Math.sin(Math.PI/9.0),thickness,colore);
        }
        
        private void ArrowRIGHT(Graphics g, double xstart, double ystart, double xlength, Color colore, int thickness){
            double LenR = (double)state.s15;
            
            MaestroG.drawLineThick(g,xstart,ystart, xstart + xlength, ystart, thickness, colore);
    
            MaestroG.drawLineThick(g,xstart + xlength, ystart ,xstart + xlength - LenR * Math.cos(Math.PI/9.0),
                      ystart - LenR*Math.sin(Math.PI/9.0),thickness,colore);
            MaestroG.drawLineThick(g,xstart + xlength, ystart ,xstart + xlength - LenR * Math.cos(Math.PI/9.0),
                      ystart + LenR*Math.sin(Math.PI/9.0),thickness,colore);
        }
        
        private void drawRefTrans2(Graphics g, Color colore, int thickness){
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        double x1, y1, x2, y2, x2R, y2R, x2T, y2T;
        
        radius = 3*getSize().height/4-TopMargin-6*VGap - state.s20;
        radius_powernew = radius*40/100;
        double radius_powernew2;
        radius_powernew2 = radius_powernew + state.s13;
        
	g.setColor(incident);
	double Len = (double)state.s15;
        double LenT = (double)state.s15;
        
        double LenR = (double)state.s15 * Math.pow(Math.sin(state.Reflection_Coef.Magnitude()*Math.PI/2.0),0.5);
        
        if(state.Transmission_Coef.Magnitude()>1.0){
            LenT = (double)state.s15;
        }
        else{
            LenT = (double)state.s15 * Math.pow(Math.sin(state.Transmission_Coef.Magnitude()*Math.PI/2.0),0.5);
        }
        double LenTH = (double)state.s15;
        if(state.Transmission_CoefH.Magnitude()>1.0){
            LenTH = (double)state.s15;
        }
        else{
            LenTH = (double)state.s15 * Math.pow(Math.sin(state.Transmission_CoefH.Magnitude()*Math.PI/2.0),0.5);
        }
        
        double radiusV, fieldL, x2_power, y2_power;
        int thicknessV, 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*0.5)+23.0*Math.PI/180.0;
        angolo = (state.theta1+Math.PI*0.5)-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.s4;
        radiusH = state.s16;
        
        double xmore = (double)state.s30;
        
        // Find Direction of normal field for the Reflected Component
        if(state.isPolarizationParallel){// Parallel Polarization
          //if(state.Reflection_Coef.Imaginary() == 0.0){  
                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(state.Reflection_Coef.Imaginary() == 0.0){ 
                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
	  int radiusnewx = state.s50;// these two lines position normal field near interface
          int radiusnewy = state.s50;
               
          x1 = (double)(3*getSize().width/4+state.s2);
          y1 = (double)(getSize().height/2-state.s1);
          x2 = (double)(3*getSize().width/4 +(int)(radius_powernew*Math.sin(state.theta1))+state.s2);
          y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.cos(state.theta1))-state.s1);
          x2R = (double)(3*getSize().width/4 +(int)((radius+10)*Math.sin(state.theta1))+state.s2);
          y2R = (double)(getSize().height/2-(int)((radius+10)*Math.cos(state.theta1))-state.s1);
          
          double vscaled;
          vscaled = state.beta_ratio*((radius + (double)state.s10) - radius_powernew2);
              
          if(state.theta1_deg < (state.total_angle+0.00005) && state.theta1_deg > (state.total_angle-0.00005) && state.isPolarizationParallel){ // don'tdraw if not total transmission
          }
          else if(state.wavelength1 == state.wavelength2){
          }
          else
          {
              //MaestroG.drawLineThick(g, x1, y1, x2, y2, thickness, colore);
          //-------------------------------------------
          }  
	  //transmitted ray
	  if(state.Reflection_Coef.Imaginary() == 0.0){
              
              x1 = (double)(3*getSize().width/4);
              y1 = (double)(getSize().height/2-state.s1);
              x2 = (double)(3*getSize().width/4 +(int)(radius_powernew*Math.sin(state.theta2)));
              y2 = (double)(getSize().height/2+(int)(radius_powernew*Math.cos(state.theta2))-state.s1);
              x2T = x1+((radius_powernew2+vscaled)*Math.sin(state.theta2));
              y2T = y1+((radius_powernew2+vscaled)*Math.cos(state.theta2));
          }
          else{
              angoloT2 = (Math.PI)+23.0*Math.PI/180.0;
              angoloT = (Math.PI)-23.0*Math.PI/180.0;
        
              x1 = (double)(3*getSize().width/4);
              y1 = (double)(getSize().height/2-state.s1);
              x2 = (double)(3*getSize().width/4 +(int)(radius_powernew*Math.sin(Math.PI*0.5)));
              y2 = (double)(getSize().height/2+(int)(radius_powernew*Math.cos(Math.PI*0.5))-state.s1);
              x2T = (double)(3*getSize().width/4 +(int)((radius+10)*Math.sin(Math.PI*0.5)));
              y2T = (double)(getSize().height/2+(int)((radius+10)*Math.cos(Math.PI*0.5))-state.s1);
                x2R = (double)(3*getSize().width/4 +(int)((radius+10)*Math.sin(state.theta1))+state.s2);
                y2R = (double)(getSize().height/2-(int)((radius+10)*Math.cos(state.theta1))-state.s1);
          
              double xshifto;
              xshifto = radius_powernew2-radius_powernew2*Math.sin(state.theta1);    
          }
          
          //------------------------------------------------------------------------------------
          x2E = x1+(radius_powernew*Math.sin(state.theta1) - fieldL * Math.cos(state.theta1));
          y2E = y1-(radius_powernew*Math.cos(state.theta1) + fieldL * Math.sin(state.theta1));
          
          
          if(state.isPolarizationParallel){ // Parallel Polarization
                double x2EE, y2EE;
              
                //if(state.Reflection_Coef.Imaginary() == 0.0)
                {
                    if(state.theta1_deg < (90.0-0.000005)){
                    // ------------------------ Transmitted Wave
                    
                    }
                }
                
                // REFLECTED WAVE
                
                double y1shiftup = y1 -(double)state.s10;
                double y1shiftup2 = y1-radiusH-(double)state.s5;
                radiusnewx = state.s50;// these two lines position normal field near interface
                radiusnewy = state.s50;
                
                if(state.theta1_deg < (state.total_angle+0.00005) && state.theta1_deg > (state.total_angle-0.00005) ){
                }
                else if(state.wavelength1 == state.wavelength2){
                }
                else
                {  
                if(Field_Refl_Out){
                    x2EE = x1+(radius_powernew2*Math.sin(state.theta1) - fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.cos(state.theta1));
                    y2EE = y1-(radius_powernew2*Math.cos(state.theta1) + fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.sin(state.theta1));
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    if(!show_normal){
                        // E field vector        
                            if(show_tang){
                                g.setColor(colorE);                   
                                MaestroG.subscriptersupBs2("E","r","//","x","",g,state.font16,
                                                        (int)(x1 - vettoreL -xmore),(int)(y1 - 2*radiusnewy));
                                MaestroG.subscripterB("Reflected Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                     (int)(y1 - 3.0*(radiusnewy)));
                            }
                            else{
                                g.setColor(colorE);                   
                                MaestroG.subscriptersupBss2("\u03b5","1","E","r","//","z","",g,state.font16,
                                                        (int)(x1 - 5*vettoreL/4),(int)(y1 - 12*radiusnewy/8 ));
                                MaestroG.subscripterB("Reflected Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                     (int)(y1 - 3.0*(radiusnewy)));
                                
                                g.setColor(Color.red);
                                MaestroG.subscripterB("\u03c1","s","",g,state.font20,(int)(x1 + state.s50),(int)(y1 - state.s20));
                                MaestroG.drawLineThick(g,x1 + state.s22,y1 + state.s48, x1 + state.s22, y1 + state.s34, state.s4, Color.red);
                                MaestroG.drawLineThick(g,x1 + state.s15,y1 + state.s41, x1 + state.s29, y1 + state.s41, state.s4, Color.red);
                                MaestroG.drawLineThick(g,x1+state.s22,y1+state.s88, x1+state.s22, y1+state.s74, state.s4, Color.red);
                                MaestroG.drawLineThick(g,x1+state.s15,y1+state.s81, x1+state.s29, y1+state.s81, state.s4, Color.red);
                                
                                MaestroG.drawLineThick(g,x1+state.s22,y1-state.s5, x1+state.s22, y1+state.s9, state.s4, Color.red);
                                MaestroG.drawLineThick(g,x1+state.s15,y1+state.s1, x1+state.s29, y1+state.s1, state.s4, Color.red);
                                
                                MaestroG.drawLineThick(g,x1+state.s22,y1-state.s46, x1+state.s22, y1-state.s32, state.s4, Color.red);
                                MaestroG.drawLineThick(g,x1+state.s15,y1-state.s39, x1+state.s29, y1-state.s39, state.s4, Color.red);
                                
                                MaestroG.drawLineThick(g,x1+state.s22,y1-state.s86, x1+state.s22, y1-state.s72, state.s4, Color.red);
                                MaestroG.drawLineThick(g,x1+state.s15,y1-state.s79, x1+state.s29, y1-state.s79, state.s4, Color.red);
                            }
                        
                        if(show_tang){
                            if(state.Reflection_Coef.Real() > 0.0){
                                ArrowDOWN(g, x1-radiusnewx-xmore, y1-radiusnewy, vettoreL, colorE, 4);//*
                            }
                            else{
                                ArrowDOWN(g, x1-radiusnewx-xmore, y1-radiusnewy, vettoreL, colorE, 4);//*
                            }
                        }
                        else{
                            if(state.Reflection_Coef.Real() > 0.0){
                                //ArrowUP(g, x1-radiusnewx, y1-radiusnewy, vettoreL, colorE, 4);
                                ArrowLEFT(g, x1-radiusnewx-vettoreL, y1-radiusnewy, vettoreL, colorE, 4);
                            }
                            else{
                                //ArrowUP(g, x1-radiusnewx, y1-radiusnewy, vettoreL, colorE, 4);
                                ArrowLEFT(g, x1-radiusnewx-vettoreL, y1-radiusnewy, vettoreL, colorE, 4);
                            }
                        }
                    }
                    else{
                        //An oval for magnetic field
                        MaestroG.fillCircleThick(g, x1-(radiusnewx)-xmore, y1-radiusnewy, (double)radiusH, state.s2, Color.white);
                        MaestroG.fillCircleThick(g, x1-(radiusnewx)-xmore, y1-radiusnewy, (double)state.s5,state.s2,colorH);
                        MaestroG.drawCircleThick(g, x1-(radiusnewx)-xmore, y1-radiusnewy, (double)radiusH,state.s4,colorH);

                        g.setColor(Color.blue);                   
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        // FIVE ACCA
                        MaestroG.subscripterB("Reflected Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                         (int)(y1 - 3.0*(radiusnewy)));

                        MaestroG.subscriptersupBs2("H","r","//","y","",g,state.font16,(int)(x1 - 5*radiusnewx/2-xmore), 
                                                             (int)((0.96 * y1) - (radiusnewy)));
                        
                        //SURFACE CURRENT
                        ArrowUP(g, x1, y1 + state.s60, state.s120, Color.red, state.s4);
                        MaestroG.subscripterB("J","S","",g,state.font18,(int)(1.05 * x1),(int)(y1 - state.s30));
                    }
                }
                else{ // FIELD IN
                    x2EE = x1+(radius_powernew2*Math.sin(state.theta1) + fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.cos(state.theta1));
                    y2EE = y1-(radius_powernew2*Math.cos(state.theta1) - fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.sin(state.theta1));
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    if(!show_normal){
                        // E field vector
                            if(show_tang){
                                g.setColor(colorE);                   
                                MaestroG.subscriptersupBs2("E","r","//","x","",g,state.font16,
                                                        (int)(x1 - vettoreL-xmore),(int)(y1 - 2*radiusnewy ));
                                MaestroG.subscripterB("Reflected Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                     (int)(y1 - 3.0*(radiusnewy)));
                            }
                            else{
                                g.setColor(colorE);                   
                                MaestroG.subscriptersupBss2("\u03b5","1","E","r","//","z","",g,state.font16,
                                                        (int)(x1 - 5*vettoreL/4),(int)(y1 - 12*radiusnewy/8 ));
                                MaestroG.subscripterB("Reflected Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                     (int)(y1 - 3.0*(radiusnewy)));
                                
                                // Surface charge (negative)
                                g.setColor(Color.red);
                                MaestroG.subscripterB("\u03c1","s","",g,state.font20,(int)(x1 + state.s50),(int)(y1 - state.s20));
                                MaestroG.drawLineThick(g,x1+state.s15,y1+state.s41, x1+state.s29, y1+state.s41, state.s4, Color.red);
                                
                                MaestroG.drawLineThick(g,x1+state.s15,y1+state.s81, x1+state.s29, y1+state.s81, state.s4, Color.red);
                                MaestroG.drawLineThick(g,x1+state.s15,y1+state.s1, x1+state.s29, y1+state.s1, state.s4, Color.red);
                                
                                MaestroG.drawLineThick(g,x1+state.s15,y1-state.s39, x1+state.s29, y1-state.s39, state.s4, Color.red);
                                MaestroG.drawLineThick(g,x1+state.s15,y1-state.s79, x1+state.s29, y1-state.s79, state.s4, Color.red);
                            }
                            
                        if(show_tang){
                             
                            if(state.Reflection_Coef.Real() > 0.0){
                                ArrowUP(g, x1-radiusnewx-xmore, y1-radiusnewy, vettoreL, colorE, state.s4);//*
                            }
                            else{
                                ArrowUP(g, x1-radiusnewx-xmore, y1-radiusnewy, vettoreL, colorE, state.s4);//*
                            }
                        }
                        else{    
                            if(state.Reflection_Coef.Real() < 0.0){
                                ArrowRIGHT(g, x1-radiusnewx-vettoreL, y1-radiusnewy, vettoreL, colorE, state.s4);//*
                            }
                            else{
                                ArrowRIGHT(g, x1-radiusnewx-vettoreL, y1-radiusnewy, vettoreL, colorE, state.s4);//*
                            }
                        }
                    }
                    else{
                        //An oval for magnetic field
                        MaestroG.fillCircleThick(g, x1-(radiusnewx)-xmore, y1-radiusnewy, (double)radiusH, state.s2, Color.white);
                        MaestroG.drawLineThick(g, x1-(radiusnewx)-(double)(radiusH/2)-xmore,
                                        y1-radiusnewy-(double)(radiusH/2),
                                        x1-(radiusnewx) + (double)(radiusH/2)-xmore,
                                        y1-radiusnewy + (double)(radiusH/2), state.s4,colorH);
                         MaestroG.drawLineThick(g, x1-(radiusnewx)+(double)(radiusH/2)-xmore,
                                        y1-radiusnewy-(double)(radiusH/2),
                                        x1-(radiusnewx) - (double)(radiusH/2)-xmore,
                                        y1-radiusnewy + (double)(radiusH/2), state.s4,colorH);

                        MaestroG.drawCircleThick(g, x1-(radiusnewx)-xmore, y1-radiusnewy, (double)radiusH,state.s4,colorH);
                        g.setColor(colorH);                   
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        // SIX ACCA
                        MaestroG.subscripterB("Reflected Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                         (int)(y1 - 3.0*(radiusnewy)));

                        MaestroG.subscriptersupBs2("H","r","//","y","",g,state.font16,(int)(x1 - 5*radiusnewx/2-xmore), 
                                                             (int)((0.96 * y1) - (radiusnewy)));
                        
                        //SURFACE CURRENT
                        ArrowDOWN(g, x1, y1+state.s60, state.s120, Color.red, state.s4);
                        MaestroG.subscripterB("J","S","",g,state.font18,(int)(1.05 * x1),(int)(y1 - state.s30));
                    }
                }
              }
        }
	else{  // Perpendicular Polarization
                double x2EE, y2EE;
                //if(state.Reflection_Coef.Imaginary() == 0.0)
                {
                    if(state.theta1_deg < (90.0-0.000005)){
                    //------------------ transmitted field
                    }
                }
                
                //Reflected Wave
                if(Field_Refl_Out){
                    x2EE = x1+(radius_powernew2*Math.sin(state.theta1) + fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.cos(state.theta1));
                    y2EE = y1-(radius_powernew2*Math.cos(state.theta1) - fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.sin(state.theta1));
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    if(!show_normal){
                        // H field vector    
                            if(show_tang){
                                g.setColor(colorH);                   
                                MaestroG.subscriptersupBs("H","r","\u22a5x","",g,state.font16,
                                                        (int)(x1 - vettoreL-xmore),(int)(y1 - 2*radiusnewy ));
                                MaestroG.subscripterB("Reflected Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                     (int)(y1 - 3.0*(radiusnewy)));
                                
                                // Surface Current
                                MaestroG.fillCircleThick(g, x1,y1-3*(radiusnewy)/2,(double)radiusH, state.s2, Color.white);
                                MaestroG.drawLineThick(g, x1-(double)(radiusH/2),y1-3*(radiusnewy)/2-(double)(radiusH/2),
                                        x1 + (double)(radiusH/2),y1-3*(radiusnewy)/2 + (double)(radiusH/2), state.s4,Color.red);
                                MaestroG.drawLineThick(g, x1+(double)(radiusH/2),y1-3*(radiusnewy)/2-(double)(radiusH/2),
                                        x1 - (double)(radiusH/2),y1-3*(radiusnewy)/2 + (double)(radiusH/2), state.s4,Color.red);
                                MaestroG.drawCircleThick(g, x1,y1-3*(radiusnewy)/2,(double)radiusH, state.s4, Color.red);
                                
                                MaestroG.fillCircleThick(g, x1,y1+3*(radiusnewy)/2,(double)radiusH, state.s2, Color.white);
                                MaestroG.drawLineThick(g, x1-(double)(radiusH/2),y1+3*(radiusnewy)/2-(double)(radiusH/2),
                                        x1 + (double)(radiusH/2),y1+3*(radiusnewy)/2 + (double)(radiusH/2), state.s4,Color.red);
                                MaestroG.drawLineThick(g, x1+(double)(radiusH/2),y1+3*(radiusnewy)/2-(double)(radiusH/2),
                                        x1 - (double)(radiusH/2),y1+3*(radiusnewy)/2 + (double)(radiusH/2), state.s4,Color.red);
                                MaestroG.drawCircleThick(g, x1,y1+3*(radiusnewy)/2,(double)radiusH, state.s4, Color.red);
                                
                                MaestroG.subscripterB("J","S","",g,state.font18,(int)(1.05 * x1),(int)(y1+2*(radiusnewy)+2*(double)(radiusH/2)));
                            }
                            else{
                                g.setColor(colorH);                   
                                MaestroG.subscriptersupBss("\u03bc","1","H","r","\u22a5z","",g,16,
                                                        (int)(x1 - 5*vettoreL/4),(int)(y1 - 12*radiusnewy/8 ));
                                MaestroG.subscripterB("Reflected Wave","","",g,14,(int)((0.9 * x1) - (radiusnewx)-xmore)-85, 
                                                     (int)(y1 - 3.0*(radiusnewy)));
                            }
                                           
                        if(show_tang){     
                            if(state.Reflection_CoefH.Real() > 0.0){
                                ArrowUP(g, x1-radiusnewx-xmore, y1-radiusnewy, vettoreL, colorH, state.s4);//*
                            }
                            else{
                                ArrowUP(g, x1-radiusnewx-xmore, y1-radiusnewy, vettoreL, colorH, state.s4);//*
                            }
                        }
                        else{
                            if(state.Reflection_CoefH.Real() < 0.0){
                                ArrowRIGHT(g, x1-radiusnewx-vettoreL, y1-radiusnewy, vettoreL, colorH, state.s4);//*
                            }
                            else{
                                ArrowRIGHT(g, x1-radiusnewx-vettoreL, y1-radiusnewy, vettoreL, colorH, state.s4);//*
                            }
                        }
                    }
                    else{
                        //An oval for electric field
                        MaestroG.fillCircleThick(g, x1-(radiusnewx)-xmore, y1-(radiusnewy), (double)radiusH, state.s2, Color.white);
                        MaestroG.fillCircleThick(g, x1-(radiusnewx)-xmore, y1-(radiusnewy), (double)state.s5,state.s2,colorE);

                        MaestroG.drawCircleThick(g, x1-(radiusnewx)-xmore, y1-(radiusnewy), (double)radiusH,state.s4,colorE);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(colorE);                   
                        MaestroG.subscripterB("Reflected Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                         (int)(y1 - 3.0*(radiusnewy)));

                        MaestroG.subscriptersupBs("E","r","\u22a5y","",g,state.font16,(int)(x1 - 5*radiusnewx/2-xmore), 
                                                             (int)((0.96 * y1) - (radiusnewy)));
                    }
                }
                else{
                    x2EE = x1+(radius_powernew2*Math.sin(state.theta1) - fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.cos(state.theta1));
                    y2EE = y1-(radius_powernew2*Math.cos(state.theta1) + fieldL*(state.Reflection_Coef.Magnitude()+extra) * Math.sin(state.theta1));
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                    if(!show_normal){
                            if(show_tang){
                                g.setColor(colorH);                   
                                MaestroG.subscriptersupBs("H","r","\u22a5x","",g,state.font16,
                                                        (int)(x1 - vettoreL-xmore),(int)(y1 - 2*radiusnewy ));
                                MaestroG.subscripterB("Reflected Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                     (int)(y1 - 3.0*(radiusnewy)));
                                
                                // Surface current
                                MaestroG.fillCircleThick(g, x1, y1-3*(radiusnewy)/2, (double)radiusH, state.s2, Color.white);
                                MaestroG.fillCircleThick(g, x1, y1-3*(radiusnewy)/2, (double)state.s5,state.s2,Color.red);
                                MaestroG.drawCircleThick(g, x1, y1-3*(radiusnewy)/2, (double)radiusH,state.s4,Color.red);
                                
                                MaestroG.fillCircleThick(g, x1, y1+3*(radiusnewy)/2, (double)radiusH, state.s2, Color.white);
                                MaestroG.fillCircleThick(g, x1, y1+3*(radiusnewy)/2, (double)state.s5,state.s2,Color.red);
                                MaestroG.drawCircleThick(g, x1, y1+3*(radiusnewy)/2, (double)radiusH,state.s4,Color.red);
                                
                                MaestroG.subscripterB("J","S","",g,state.font18,(int)(1.05 * x1),(int)(y1+2*(radiusnewy)+2*(double)(radiusH/2)));
                            }
                            else{
                                g.setColor(colorH);                   
                                MaestroG.subscriptersupBss("\u03bc","1","H","r","\u22a5z","",g,state.font16,
                                                        (int)(x1 - 5*vettoreL/4),(int)(y1 - 12*radiusnewy/8 ));
                                MaestroG.subscripterB("Reflected Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                     (int)(y1 - 3.0*(radiusnewy)));
                            }
                        
                        if(show_tang){
                            if(state.Reflection_CoefH.Real() > 0.0){
                                ArrowDOWN(g, x1-radiusnewx-xmore, y1-radiusnewy, vettoreL, colorH, state.s4);//*
                            }
                            else{
                                ArrowDOWN(g, x1-radiusnewx-xmore, y1-radiusnewy, vettoreL, colorH, state.s4);//*
                            }
                        }
                        else{
                            if(state.Reflection_CoefH.Real() < 0.0){
                                ArrowLEFT(g, x1-radiusnewx-vettoreL, y1-radiusnewy, vettoreL, colorH, state.s4);//*
                            }
                            else{
                                ArrowLEFT(g, x1-radiusnewx-vettoreL, y1-radiusnewy, vettoreL, colorH, state.s4);//*
                            }   
                        }
                    }
                    else{
                        //An oval for electric field
                        MaestroG.fillCircleThick(g, x1-(radiusnewx)-xmore,
                                            y1-(radiusnewy), (double)radiusH, state.s2, Color.white);

                        MaestroG.drawLineThick(g, x1-(radiusnewx)-(double)(radiusH/2)-xmore,
                                        y1-(radiusnewy)-(double)(radiusH/2),
                                        x1-(radiusnewx) + (double)(radiusH/2)-xmore,
                                        y1-(radiusnewy) + (double)(radiusH/2), state.s4,colorE);
                        MaestroG.drawLineThick(g, x1-(radiusnewx)+(double)(radiusH/2)-xmore,
                                        y1-(radiusnewy)-(double)(radiusH/2),
                                        x1-(radiusnewx) - (double)(radiusH/2)-xmore,
                                        y1-(radiusnewy) + (double)(radiusH/2), state.s4,colorE);

                        MaestroG.drawCircleThick(g, x1-(radiusnewx)-xmore, y1-(radiusnewy), (double)radiusH,state.s4,colorE);
                        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                        g.setColor(colorE);                   
                        MaestroG.subscripterB("Reflected Wave","","",g,state.font14,(int)((0.9 * x1) - (radiusnewx)-xmore) - state.s85, 
                                                         (int)(y1 - 3.0*(radiusnewy)));

                        MaestroG.subscriptersupBs("E","r","\u22a5y","",g,state.font16,(int)(x1 - 5*radiusnewx/2-xmore), 
                                                             (int)((0.96 * y1) - (radiusnewy)));
                    }
                }
         }
    }
        
    //-------------------------------------------------------------------------------------------------
    
    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){}
}
