import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//import java.lang.*;

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(170,190,190);
    private static final Color medium2Color = new Color(200,220,220);
    
    private static final int LeftMargin=10, RightMargin=10, TopMargin=10, BottomMargin=10;
    private static final Font labfont = new Font("SanSerif",Font.PLAIN,10);
    private static final Font axisfont = new Font("Serif",Font.ITALIC,18);
    public static final Font normalfont = new Font("Sanserif",Font.PLAIN,12);
    public static final Font normalfontB = new Font("Sanserif",Font.BOLD,12);
    public static final Font smallfont = new Font("Sanserif",Font.PLAIN,10);
    public static final Font symbfont = new Font("Serif",Font.PLAIN,12);
    private static final Font labfontbig=new Font("SanSerif",Font.PLAIN,14);
    private static final Font labfontbigI=new Font("Serif",Font.ITALIC,14);
    private double LenVert, LenHor;
    private static final int HGap=10, VGap=10;//for vectors fields.
    private Image im;
    private Graphics buf;
    
    private double vettoreL = 100.0;
    
    private double extra = 0.1;
    private Oblique_State state;
    private int radius, radius_power, radius_powernew;
    private boolean IsFocusOn, IsPrintOn;
    public boolean Field_Out, Field_Refl_Out, show_normal, show_tang;
    private Color incident = Color.red; 
    public ObliqueCanvasB(Oblique_State state){
	super();
	this.state = state;
	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;
    }

    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 drawAxis(Graphics g){
	g.setColor(Color.black);
	//horizontal axis
	g.drawLine(LeftMargin,getSize().height/2-1,getSize().width/6-15,getSize().height/2-1);
	g.drawLine(getSize().width/6+15,getSize().height/2-1,getSize().width-RightMargin-10,getSize().height/2-1);
	
        //vertical axis
	g.drawLine(getSize().width/6,getSize().height/2+15,getSize().width/6,getSize().height-BottomMargin-100);
        g.drawLine(getSize().width/6,getSize().height/2-16,getSize().width/6,getSize().height/2-120);
        
        MaestroG.drawArrow( getSize().width/6, getSize().height-BottomMargin-100, 6, g);
        MaestroG.drawArrow( getSize().width-RightMargin-10, getSize().height/2-1, 7, g);
       
    }

    private void drawY(Graphics g){
	double x1, y1, radiusy, radiusX; 
        //x1 = (double)(getSize().width/2);
        //y1 = (double)(getSize().height/2-1);

        x1 = (double)(getSize().width/6);
        y1 = (double)(getSize().height/2-1);
        
        radiusy = 12;
        radiusX = 8;
        Color colorX = new Color(100,100,100);
        
        int rule; float alpha;
        rule = AlphaComposite.SRC_OVER;
        alpha = 0.4f;
        
        Graphics2D g2d = (Graphics2D)g;
        g2d.setComposite(AlphaComposite.getInstance(rule, alpha));
        
        MaestroG.drawLineThick(g, x1-(double)(radiusX),y1-(double)(radiusX),
                                    x1+(double)(radiusX),y1+(double)(radiusX), 1,colorX);
        MaestroG.drawLineThick(g, x1+(double)(radiusX), y1-(double)(radiusX),
                                    x1-(double)(radiusX), y1+(double)(radiusX), 1,colorX);
        MaestroG.drawCircleThick(g, x1, y1, (double)radiusy,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-5*VGap/2-20;
        radius_power = radius*6/10;
        
        Graphics2D g2d = (Graphics2D)g;
        //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(Color.blue.darker());
	
	tmp = "x";
	g.drawString(tmp,(getSize().width-RightMargin-fm.stringWidth(tmp))/6-15,getSize().height-BottomMargin-95);
	tmp = "z";
	g.drawString(tmp,getSize().width-RightMargin-fm.stringWidth(tmp)-5,getSize().height/2-fm.getHeight());
	
        g.setColor(new Color(100,100,100));
        tmp = "y";
	g.drawString(tmp,(getSize().width-RightMargin-fm.stringWidth(tmp))/6-15,getSize().height/2+fm.getHeight()-5);
	
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
        
    }
    
    private void drawBackLabels(Graphics g){
	int mediumX, mediumY;
	radius = getSize().height/2-TopMargin-5*VGap/2-20;
	FontMetrics fm = g.getFontMetrics();
	//For medium 1
	//g.setColor(medium1Color.darker());
	mediumX = LeftMargin+5;
	mediumY = TopMargin+VGap;
	
        g.setColor(Color.red.darker());
	
        MaestroG.subscripter("\u03b5","r 1"," = "+state.epsilon_r1,g,12,mediumX+7,mediumY); 
	g.setFont(normalfont);   
	fm = g.getFontMetrics();
        
	mediumY += fm.getHeight();
	MaestroG.subscripter("\u03bc","r 2"," = "+state.mu_r1,g,12,mediumX+5,mediumY);
	g.setFont(normalfont); 
	fm = g.getFontMetrics();
        
	mediumY += fm.getHeight();
        MaestroG.subscripter("  \u03c3","1"," = "+state.conductivity1,g,12,mediumX+5,mediumY);  
	g.setFont(normalfontB); 
	fm = g.getFontMetrics();
        
        mediumY += fm.getHeight()*2;
        if(state.isPolarizationParallel){
            g.setColor(Color.blue);
            g.drawString("     Parallel",mediumX,mediumY);
            mediumY += fm.getHeight();
            g.drawString("  Polarization",mediumX,mediumY);
            mediumY += fm.getHeight();
            g.drawString("       (TM)",mediumX,mediumY);
        }
        else{
            g.setColor(Color.magenta.darker());
            g.drawString("Perpendicular",mediumX,mediumY);
            mediumY += fm.getHeight();
            g.drawString("  Polarization",mediumX,mediumY);
            mediumY += fm.getHeight();
            g.drawString("       (TE)",mediumX,mediumY);
        }
        
	//For medium 2
	//g.setColor(medium2Color.darker());
	g.setColor(Color.blue.darker());
	mediumX = LeftMargin+5;
	//mediumY = getSize().height/2+3*fm.getHeight();
        
        mediumY = getSize().height-45;
        MaestroG.subscripter("\u03b5","r 2"," = "+state.epsilon_r2,g,12,mediumX+7,mediumY);    
	g.setFont(normalfont); 
	fm = g.getFontMetrics();
        
	mediumY += fm.getHeight();
	MaestroG.subscripter("\u03bc","r 2"," = "+state.mu_r2,g,12,mediumX+5,mediumY);
	g.setFont(normalfont); 
	fm = g.getFontMetrics();
        
	mediumY += fm.getHeight();
	MaestroG.subscripter("  \u03c3","2"," = "+state.conductivity2,g,12,mediumX+5,mediumY);  
	g.setFont(normalfont); 
	fm = g.getFontMetrics();
        
    }
    
    private void drawMedium(Graphics g){
	g.setColor(medium1Color);
	g.fillRect(1,1,getSize().width-2,getSize().height/2-2);
	g.setColor(medium2Color);
	g.fillRect(1,getSize().height/2,getSize().width-2,getSize().height/2-1);
    }
    
    
        private void drawWaves2(Graphics g){
        double x1, y1, x2, y2;
        int thickness = 6;
            
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
	boolean Scaled_Waves = true;
	radius = getSize().height/2-TopMargin-6*VGap-20;
        radius_powernew = radius*40/100;
        
	//radius = getSize().height/2-TopMargin;
	//drawing in medium 1
	g.setColor(incident);
	//incident ray
	
        double Len = 15;
        
        LenVert = 15.0 * Math.pow(Math.sin(state.theta1),0.5);
        LenHor = 15.0 * 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 = 4;
        radiusH = 16;
        
        
        // center coordinates
          x1 = (double)(getSize().width/2-2);
          y1 = (double)(getSize().height/2-1);
        
        // 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 - 15.0;
          yo = y1 - 50.0;
          rado = Math.sqrt(15.0*15.0 + 50.0*50.0)+5;
          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 AND TRANSMITTED WAVE
            drawRefTrans2(g, incident, thickness);
            //if(IsFocusOn){drawScaleRefTrans2(g, incident, thickness);}
	}
	
             int radiusnewx = 50;// these two lines position normal field near interface
             int radiusnewy = 20;
        
        // DRAW INCIDENT WAVE HERE =========================================================
             double y1shiftup = y1-10.0;
             double y1shiftup2 = y1-radiusH-5.0;
             
    	if(state.isPolarizationParallel){ // Parallel Polarization - Incident Wave
                double yshift = 5.0;
                Color colorino;
                
                if(show_normal){
                    colorino = colorH;
                }
                else{
                    colorino = colorE;
                }
                
                if(show_normal){
                // make a plus sign
                MaestroG.drawLineThick(g, x1-(double)(radiusH/2)+2,
                                    y1-(double)(radiusH)-yshift, 
                                    x1+(double)(radiusH/2)+2,
                                    y1-(radiusH)-yshift, 4,colorino);
                MaestroG.drawLineThick(g, x1+2, y1-(double)(3*radiusH/2)-yshift, 
                                    x1+2, y1-(double)(radiusH/2)-yshift, 4,colorino);
                // make an equal sign
                MaestroG.drawLineThick(g, x1-3*radiusH, y1+(double)(3*radiusH/2), 
                                    x1-2*radiusH, y1+(double)(3*radiusH/2), 4,colorino);
                MaestroG.drawLineThick(g, x1-3*radiusH, y1+(double)(radiusH), 
                                    x1-2*radiusH, y1+(double)(radiusH), 4,colorino);
                                    
                }
                else{
                    if(show_tang){
                        // make a plus sign
                        MaestroG.drawLineThick(g, x1-(double)(radiusH/2)+2,
                                            y1-(double)(radiusH)-yshift, 
                                            x1+(double)(radiusH/2)+2,
                                            y1-(radiusH)-yshift, 4,colorino);
                        MaestroG.drawLineThick(g, x1+2, y1-(double)(3*radiusH/2)-yshift, 
                                            x1+2, y1-(double)(radiusH/2)-yshift, 4,colorino);
                        // make an equal sign
                        MaestroG.drawLineThick(g, x1-3*radiusH-vettoreL/2, y1+(double)(3*radiusH/2), 
                                            x1-2*radiusH-vettoreL/2, y1+(double)(3*radiusH/2), 4,colorino);
                        MaestroG.drawLineThick(g, x1-3*radiusH-vettoreL/2, y1+(double)(radiusH), 
                                            x1-2*radiusH-vettoreL/2, y1+(double)(radiusH), 4,colorino);
                    }
                    else{
                        // make a plus sign
                        MaestroG.drawLineThick(g, x1-(double)(radiusH/2)+2,
                                            y1-(double)(radiusH)-yshift-vettoreL/2, 
                                            x1+(double)(radiusH/2)+2,
                                            y1-(radiusH)-yshift-vettoreL/2, 4,colorino);
                        MaestroG.drawLineThick(g, x1+2, y1-(double)(3*radiusH/2)-yshift-vettoreL/2, 
                                            x1+2, y1-(double)(radiusH/2)-yshift-vettoreL/2, 4,colorino);
                        // make an equal sign
                        MaestroG.drawLineThick(g, x1-3*radiusH, y1+(double)(3*radiusH/2)+vettoreL/2, 
                                            x1-2*radiusH, y1+(double)(3*radiusH/2)+vettoreL/2, 4,colorino);
                        MaestroG.drawLineThick(g, x1-3*radiusH, y1+(double)(radiusH)+vettoreL/2, 
                                            x1-2*radiusH, y1+(double)(radiusH)+vettoreL/2, 4,colorino);
                    }
                }
                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){
                            
                            ArrowRIGHT(g, x1-radiusnewx-vettoreL, y1shiftup2, vettoreL, colorE, thicknessV);
                            
                            g.setColor(colorE);                   
                            MaestroG.subscripterBs("E","","z","",g,16,(int)((0.9 * x1) - (radiusnewx))-35, 
                                                     (int)((0.91 * y1) - (radiusnewy)));
                        
                                                     
                            MaestroG.subscripter("Incident Wave","","",g,14,(int)((0.9 * x1) - (radiusnewx))-55, 
                                                     (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));
                        }
                        else{
                            
                            ArrowUP(g, x1-radiusnewx, y1-radiusnewy, vettoreL, colorE, thicknessV);
                            
                            g.setColor(colorE);                   
                            MaestroG.subscripterBs2("\u03b5","1","E","","x","",g,16,(int)((0.90 * x1) - (radiusnewx))-35, 
                                                     (int)((0.82 * y1) - (radiusnewy)));
                            
                            MaestroG.subscripter("Incident Wave","","",g,14,(int)((0.9 * x1) - (radiusnewx))-55, 
                                                     (int)((0.96 * y1) - 6.0*(radiusnewy) - 30));
                        }
                        
                        
                    }
                }
                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){
                            
                            ArrowLEFT(g, x1-radiusnewx-vettoreL, y1shiftup2, vettoreL, colorE, thicknessV);
                            
                            g.setColor(colorE);                   
                            MaestroG.subscripterBs("E","","z","",g,16,(int)((0.9 * x1) - (radiusnewx))-35, 
                                                     (int)((0.91 * y1) - (radiusnewy)));
                            
                            MaestroG.subscripter("Incident Wave","","",g,14,(int)((0.9 * x1) - (radiusnewx))-55, 
                                                     (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));
                            
                        }
                        else{
                            
                            ArrowDOWN(g, x1-radiusnewx, y1-radiusnewy, vettoreL, colorE, thicknessV);
                            
                            g.setColor(colorE);                   
                            MaestroG.subscripterBs2("\u03b5","1","E","","x","",g,16,(int)((0.90 * x1) - (radiusnewx))-35, 
                                                     (int)((0.82 * y1) - (radiusnewy)));
                            
                            MaestroG.subscripter("Incident Wave","","",g,14,(int)((0.9 * x1) - (radiusnewx))-55, 
                                                     (int)((0.96 * y1) - 6.0*(radiusnewy) - 30));

                        }
                    }
                }
            
                
             if(show_normal){    
                //An oval for magnetic field
                
                if(Field_Out){
                        MaestroG.fillCircleThick(g, x1-radiusnewx, y1-(radiusnewy), (double)radiusH, 2, Color.white);
                
                        MaestroG.fillCircleThick(g, x1-radiusnewx, y1-radiusnewy, 5.0,2,colorH);
                        MaestroG.drawCircleThick(g, x1-radiusnewx, y1-radiusnewy, (double)radiusH,4,colorH);
                
                        g.setColor(Color.blue);  
                        
                        // INCIDENT 
                        
                        MaestroG.subscripter("Incident Wave","","",g,14,(int)((0.9 * x1) - (radiusnewx))-55, 
                                                     (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));
                        
                        MaestroG.subscripterBs("H","","y","",g,16,(int)((0.9 * x1) - (radiusnewx))-35, 
                                                     (int)((0.96 * y1) - (radiusnewy)));
                                                     
                        //MaestroG.subscripter("- H","0","",g,14,(int)((0.9 * x1) - (radiusnewx))-55, 
                          //                           (int)((0.96 * y1) - 3.0*(radiusnewy) - 10));
                }
                else{
                        MaestroG.fillCircleThick(g, x1-(radiusnewx), y1-(radiusnewy), (double)radiusH, 2, Color.white);
                
                        MaestroG.drawLineThick(g, x1-(radiusnewx)-(double)(radiusH/2),
                                    y1-(radiusnewy)-(double)(radiusH/2),
                                    x1-(radiusnewx) + (double)(radiusH/2),
                                    y1-(radiusnewy) + (double)(radiusH/2), 4,colorH);
                        MaestroG.drawLineThick(g, x1-(radiusnewx)+(double)(radiusH/2),
                                    y1-(radiusnewy)-(double)(radiusH/2),
                                    x1-(radiusnewx) - (double)(radiusH/2),
                                    y1-(radiusnewy) + (double)(radiusH/2), 4,colorH);
                        MaestroG.drawCircleThick(g, x1-(radiusnewx),
                                    y1-(radiusnewy),
                                    (double)radiusH,4,colorH);
                
                        g.setColor(Color.blue);                   
                        // INCIDENT 
                        
                        MaestroG.subscripter("Incident Wave","","",g,14,(int)((0.9 * x1) - (radiusnewx))-55, 
                                                     (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));
                        
                        
                        MaestroG.subscripterBs("H","","y","",g,16,(int)((0.9 * x1) - (radiusnewx))-35, 
                                                     (int)((0.96 * y1) - (radiusnewy)));
                                                     
                        //MaestroG.subscripter("H","0","",g,14,(int)((0.9 * x1) - (radiusnewx))-55, 
                          //                           (int)((0.96 * y1) - 3.0*(radiusnewy) - 10));
               }
            }
        }
             
	else{ // Perpendicular Polarization - Incident Wave
            
                double yshift = 5.0;
                Color colorino;
                
                if(show_normal){
                    colorino = colorE;
                }
                else{
                    colorino = colorH;
                }
                    
                if(show_normal){
                    // make a plus sign
                    MaestroG.drawLineThick(g, x1-(double)(radiusH/2)+2,
                                        y1-(double)(radiusH)-yshift, 
                                        x1+(double)(radiusH/2)+2,
                                        y1-(radiusH)-yshift, 4,colorino);
                    MaestroG.drawLineThick(g, x1+2, y1-(double)(3*radiusH/2)-yshift, 
                                        x1+2, y1-(double)(radiusH/2)-yshift, 4,colorino);
                    // make an equal sign
                    MaestroG.drawLineThick(g, x1-3*radiusH, y1+(double)(3*radiusH/2), 
                                        x1-2*radiusH, y1+(double)(3*radiusH/2), 4,colorino);
                    MaestroG.drawLineThick(g, x1-3*radiusH, y1+(double)(radiusH), 
                                        x1-2*radiusH, y1+(double)(radiusH), 4,colorino);
                }
                else{
                    if(show_tang){
                        // make a plus sign
                        MaestroG.drawLineThick(g, x1-(double)(radiusH/2)+2,
                                            y1-(double)(radiusH)-yshift, 
                                            x1+(double)(radiusH/2)+2,
                                            y1-(radiusH)-yshift, 4,colorino);
                        MaestroG.drawLineThick(g, x1+2, y1-(double)(3*radiusH/2)-yshift, 
                                            x1+2, y1-(double)(radiusH/2)-yshift, 4,colorino);
                        // make an equal sign
                        MaestroG.drawLineThick(g, x1-3*radiusH-vettoreL/2, y1+(double)(3*radiusH/2), 
                                            x1-2*radiusH-vettoreL/2, y1+(double)(3*radiusH/2), 4,colorino);
                        MaestroG.drawLineThick(g, x1-3*radiusH-vettoreL/2, y1+(double)(radiusH), 
                                            x1-2*radiusH-vettoreL/2, y1+(double)(radiusH), 4,colorino);
                    }
                    else{
                        // make a plus sign
                        MaestroG.drawLineThick(g, x1-(double)(radiusH/2)+2,
                                            y1-(double)(radiusH)-yshift-vettoreL/2, 
                                            x1+(double)(radiusH/2)+2,
                                            y1-(radiusH)-yshift-vettoreL/2, 4,colorino);
                        MaestroG.drawLineThick(g, x1+2, y1-(double)(3*radiusH/2)-yshift-vettoreL/2, 
                                            x1+2, y1-(double)(radiusH/2)-yshift-vettoreL/2, 4,colorino);
                        // make an equal sign
                        MaestroG.drawLineThick(g, x1-3*radiusH, y1+(double)(3*radiusH/2)+vettoreL/2, 
                                            x1-2*radiusH, y1+(double)(3*radiusH/2)+vettoreL/2, 4,colorino);
                        MaestroG.drawLineThick(g, x1-3*radiusH, y1+(double)(radiusH)+vettoreL/2, 
                                            x1-2*radiusH, y1+(double)(radiusH)+vettoreL/2, 4,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, y1shiftup2, vettoreL, colorH, thicknessV);
                            g.setColor(colorH);                   
                            MaestroG.subscripterBs("H","","z","",g,16,(int)((0.9 * x1) - (radiusnewx))-35, 
                                                     (int)((0.91 * y1) - (radiusnewy)));
                            
                            MaestroG.subscripter("Incident Wave","","",g,14,(int)((0.9 * x1) - (radiusnewx))-55, 
                                                     (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));
                        }
                        else{
                             
                            ArrowDOWN(g, x1-radiusnewx, y1-radiusnewy, vettoreL, colorH, thicknessV);
                            g.setColor(colorH);                   
                            MaestroG.subscripterBs2("\u03bc","1","H","","x","",g,16,(int)((0.90 * x1) - (radiusnewx))-35, 
                                                     (int)((0.82 * y1) - (radiusnewy)));
                            
                            MaestroG.subscripter("Incident Wave","","",g,14,(int)((0.9 * x1) - (radiusnewx))-55, 
                                                     (int)((0.96 * y1) - 6.0*(radiusnewy) - 30));
                        }
                    }
                    
                    
                }
                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){
                            
                            ArrowRIGHT(g, x1-radiusnewx-vettoreL, y1shiftup2, vettoreL, colorH, thicknessV);
                            g.setColor(colorH);                   
                            MaestroG.subscripterBs("H","","z","",g,16,(int)((0.9 * x1) - (radiusnewx))-35, 
                                                     (int)((0.91 * y1) - (radiusnewy)));
                            
                            MaestroG.subscripter("Incident Wave","","",g,14,(int)((0.9 * x1) - (radiusnewx))-55, 
                                                     (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));
                        }
                        else{
                            
                            ArrowUP(g, x1-radiusnewx, y1-radiusnewy, vettoreL, colorH, thicknessV);
                            g.setColor(colorH);                   
                            MaestroG.subscripterBs2("\u03bc","1","H","","x","",g,16,(int)((0.90 * x1) - (radiusnewx))-35, 
                                                     (int)((0.82 * y1) - (radiusnewy)));
                            
                            MaestroG.subscripter("Incident Wave","","",g,14,(int)((0.9 * x1) - (radiusnewx))-55, 
                                                     (int)((0.96 * y1) - 6.0*(radiusnewy) - 30));
                        }
                    }
                    
                }
                
                //An oval for electric field
                if(Field_Out){
                        if(show_normal){
                            MaestroG.fillCircleThick(g, x1-(radiusnewx), y1-(radiusnewy), (double)radiusH, 4, Color.white);
                            MaestroG.fillCircleThick(g, x1-(radiusnewx), y1-(radiusnewy), 5.0,2,colorE);
                            MaestroG.drawCircleThick(g, x1-(radiusnewx), y1-(radiusnewy), (double)radiusH,4,colorE);

                            g.setColor(colorE);                   
                            // INCIDENT 
                            MaestroG.subscripter("Incident Wave","","",g,14,(int)((0.9 * x1) - (radiusnewx))-55, 
                                                         (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));

                            MaestroG.subscripterBs("E","","y","",g,16,(int)((0.9 * x1) - (radiusnewx))-35, 
                                                         (int)((0.96 * y1) - (radiusnewy)));
                        }
                        
                }
                else{
                        if(show_normal){
                            MaestroG.fillCircleThick(g, x1-(radiusnewx), y1-(radiusnewy), (double)radiusH, 4, Color.white);

                            MaestroG.drawLineThick(g, x1-(radiusnewx)-(double)(radiusH/2),
                                        y1-(radiusnewy)-(double)(radiusH/2), 
                                        x1-(radiusnewx) + (double)(radiusH/2),
                                        y1-(radiusnewy) + (double)(radiusH/2), 4,colorE);
                            MaestroG.drawLineThick(g, x1-(radiusnewx)+(double)(radiusH/2),
                                        y1-(radiusnewy)-(double)(radiusH/2),
                                        x1-(radiusnewx) - (double)(radiusH/2),
                                        y1-(radiusnewy) + (double)(radiusH/2), 4,colorE);
                            MaestroG.drawCircleThick(g, x1-(radiusnewx),
                                        y1-(radiusnewy),
                                        (double)radiusH,4,colorE);

                            g.setColor(colorE);
                            // INCIDENT 

                            MaestroG.subscripter("Incident Wave","","",g,14,(int)((0.9 * x1) - (radiusnewx))-55, 
                                                         (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));

                            MaestroG.subscripterBs("E","","y","",g,16,(int)((0.9 * x1) - (radiusnewx))-35, 
                                                         (int)((0.96 * y1) - (radiusnewy)));

                                                         
                        }
                        
                }
        }
	
    }

    
    //-------------------------------------------------------------------------------------------
    
    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 = 15.0;
            
            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 = 15.0;
            
            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 = 15.0;
            
            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 = 15.0;
            
            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_OFF);
        
        double x1, y1, x2, y2, x2R, y2R, x2T, y2T;
        
        radius = getSize().height/2-TopMargin-6*VGap-20;
        radius_powernew = radius*40/100;
        double radius_powernew2;
        radius_powernew2 = radius_powernew+13;
        
	g.setColor(incident);
	double Len = 15.0;
        double LenT = 15.0;
        
        double LenR = 15.0 * Math.pow(Math.sin(state.Reflection_Coef.Magnitude()*Math.PI/2.0),0.5);
        
        if(state.Transmission_Coef.Magnitude()>1.0){
            LenT = 15.0;
        }
        else{
            LenT = 15.0 * Math.pow(Math.sin(state.Transmission_Coef.Magnitude()*Math.PI/2.0),0.5);
        }
        double LenTH = 15.0;
        if(state.Transmission_CoefH.Magnitude()>1.0){
            LenTH = 15.0;
        }
        else{
            LenTH = 15.0 * Math.pow(Math.sin(state.Transmission_CoefH.Magnitude()*Math.PI/2.0),0.5);
        }
        
        double radiusV, fieldL, x2_power, y2_power;
        int thicknessV, 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 = 4;
        radiusH = 16;
        
        // 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 = 50;// these two lines position normal field near interface
          int radiusnewy = 20;
               
          x1 = (double)(getSize().width/2+2);
          y1 = (double)(getSize().height/2-1);
          x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.sin(state.theta1))+2);
          y2 = (double)(getSize().height/2-(int)(radius_powernew*Math.cos(state.theta1))-1);
          x2R = (double)(getSize().width/2 +(int)((radius+10)*Math.sin(state.theta1))+2);
          y2R = (double)(getSize().height/2-(int)((radius+10)*Math.cos(state.theta1))-1);
          
          double vscaled;
          vscaled = state.beta_ratio*((radius+10.0) - radius_powernew2);
              
          if(state.theta1_deg < (state.total_angle+0.00005) && state.theta1_deg > (state.total_angle-0.00005) && state.isPolarizationParallel){ // don'tdraw if not total transmission
          }
          else if(state.wavelength1 == state.wavelength2){
          }
          else
          {
              //MaestroG.drawLineThick(g, x1, y1, x2, y2, thickness, colore);
          
              
          //-------------------------------------------
          }  
	  //transmitted ray
	  if(state.Reflection_Coef.Imaginary() == 0.0){
              
              x1 = (double)(getSize().width/2);
              y1 = (double)(getSize().height/2-1);
              x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.sin(state.theta2)));
              y2 = (double)(getSize().height/2+(int)(radius_powernew*Math.cos(state.theta2))-1);
              //x2T = (double)(getSize().width/2 +(int)((radius+10)*Math.sin(state.theta2)));
              //y2T = (double)(getSize().height/2+(int)((radius+10)*Math.cos(state.theta2))-1);
              x2T = x1+((radius_powernew2+vscaled)*Math.sin(state.theta2));
              y2T = y1+((radius_powernew2+vscaled)*Math.cos(state.theta2));
              
              //MaestroG.drawLineThick(g, x1, y1, x2, y2, thickness, colore);

          }
          else{
              angoloT2 = (Math.PI)+23.0*Math.PI/180.0;
              angoloT = (Math.PI)-23.0*Math.PI/180.0;
        
              x1 = (double)(getSize().width/2);
              y1 = (double)(getSize().height/2-1);
              x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.sin(Math.PI*0.5)));
              y2 = (double)(getSize().height/2+(int)(radius_powernew*Math.cos(Math.PI*0.5))-1);
              x2T = (double)(getSize().width/2 +(int)((radius+10)*Math.sin(Math.PI*0.5)));
              y2T = (double)(getSize().height/2+(int)((radius+10)*Math.cos(Math.PI*0.5))-1);
                x2R = (double)(getSize().width/2 +(int)((radius+10)*Math.sin(state.theta1))+2);
                y2R = (double)(getSize().height/2-(int)((radius+10)*Math.cos(state.theta1))-1);
          
              
              //MaestroG.drawLineThick(g, x1, y1, x2, y2, thickness, colore);

              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)
                {
                    // ------------------------ Transmitted Wave
                    if(Field_Out){
                        x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.sin(state.theta2)));
                        y2 = (double)(getSize().height/2+(int)(radius_powernew*Math.cos(state.theta2))-1);
                        x2T = (double)(getSize().width/2 +(int)((radius+10)*Math.sin(state.theta2)));
                        y2T = (double)(getSize().height/2+(int)((radius+10)*Math.cos(state.theta2))-1);

                        x2EE = x1+(radius_powernew2*Math.sin(state.theta2) + fieldL*(state.Transmission_Coef.Magnitude()+extra) * Math.cos(state.theta2));
                        y2EE = y1+(radius_powernew2*Math.cos(state.theta2) - fieldL*(state.Transmission_Coef.Magnitude()+extra) * Math.sin(state.theta2));

                        if(!show_normal){
                            
                            if(show_tang){
                                g.setColor(colorE);                   
                                MaestroG.subscripterBs("E","","zT","",g,16,(int)(x1)-10, 
                                                     (int)(1.2 * y1));
                                FontMetrics fm; int xspace;
                                g.setFont(new Font("SanSerif",Font.PLAIN,14));
                                fm = g.getFontMetrics();
                                xspace = fm.stringWidth("Transmitted Wave");
                                MaestroG.subscripter("Transmitted Wave","","",g,14,(int)(x1 - xspace/2),
                                                        (int)((1.04 * y1) + 3.0*(radiusnewy) + 44));
                            }
                            else{
                                g.setColor(colorE);                   
                                MaestroG.subscripterBs2("\u03b5","2C","E","","xT","",g,16,(int)(x1)+25, 
                                                     (int)((1.3 * y1) - (radiusnewy)));
                                FontMetrics fm; int xspace;
                                g.setFont(new Font("SanSerif",Font.PLAIN,14));
                                fm = g.getFontMetrics();
                                xspace = fm.stringWidth("Transmitted Wave");
                                MaestroG.subscripter("Transmitted Wave","","",g,14,(int)(x1 - xspace/2),
                                                                (int)((1.04 * y1) + 6.0*(radiusnewy) + 44));
                            }
                            
                            if(show_tang){
                            
                                if(state.Reflection_Coef.Real() > 0.0){
                                    ArrowRIGHT(g, x1-vettoreL/2.0, y1+radiusnewy, vettoreL, colorE, 4);
                                }
                                else{
                                    ArrowRIGHT(g, x1-vettoreL/2.0, y1+radiusnewy, vettoreL, colorE, 4);
                                }
                            
                            }
                            else{

                                if(state.Reflection_Coef.Real() > 0.0){
                                    ArrowUP(g, x1, y1+radiusnewy+vettoreL, vettoreL, colorE, 4);
                                }
                                else{
                                    ArrowUP(g, x1, y1+radiusnewy+vettoreL, vettoreL, colorE, 4);
                                }
                            
                            }
                            
                        }
                        else{
                            //An oval for magnetic field

                            MaestroG.fillCircleThick(g, x1, y1+(radiusnewy), (double)radiusH, 2, Color.white);

                            MaestroG.fillCircleThick(g, x1, y1+(radiusnewy), 5.0,2,colorH);
                            MaestroG.drawCircleThick(g, x1, y1+(radiusnewy), (double)radiusH,4,colorH);

                            g.setColor(Color.blue);                   
                            //MaestroG.subscripterB("H","\u22a5","",g,16,(int)((15.0 + 0.96*x1) + (1.05*radiusnewx)), 
                              //                                   (int)((1.1 * y1) + (radiusnewy)));
                            // Transmission ONE ACCA
                            FontMetrics fm; int xspace;
                            g.setFont(new Font("SanSerif",Font.PLAIN,14));
                            fm = g.getFontMetrics();
                            xspace = fm.stringWidth("Transmitted Wave");
                            MaestroG.subscripter("Transmitted Wave","","",g,14,(int)(x1 - xspace/2), 
                                                         (int)((1.04 * y1) + 3.0*(radiusnewy) + 44));

                            MaestroG.subscripterBs("H","","yT","",g,16,(int)(x1 + radiusH +10), 
                                                         (int)((1.04 * y1) + (radiusnewy))+16);
                        }
                    }
                    else{
                        x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.sin(state.theta2)));
                        y2 = (double)(getSize().height/2+(int)(radius_powernew*Math.cos(state.theta2))-1);
                        x2T = (double)(getSize().width/2 +(int)((radius+10)*Math.sin(state.theta2)));
                        y2T = (double)(getSize().height/2+(int)((radius+10)*Math.cos(state.theta2))-1);

                        x2EE = x1+(radius_powernew2*Math.sin(state.theta2) + fieldL*(state.Transmission_Coef.Magnitude()+extra) * Math.cos(state.theta2));
                        y2EE = y1+(radius_powernew2*Math.cos(state.theta2) - fieldL*(state.Transmission_Coef.Magnitude()+extra) * Math.sin(state.theta2));

                        if(!show_normal){
                            // E field vector
                            if(show_tang){
                                g.setColor(colorE);                   
                                MaestroG.subscripterBs("E","","zT","",g,16,(int)(x1)-10, 
                                                     (int)(1.2 * y1));
                                FontMetrics fm; int xspace;
                                g.setFont(new Font("SanSerif",Font.PLAIN,14));
                                fm = g.getFontMetrics();
                                xspace = fm.stringWidth("Transmitted Wave");
                                MaestroG.subscripter("Transmitted Wave","","",g,14,(int)(x1 - xspace/2),
                                                        (int)((1.04 * y1) + 3.0*(radiusnewy) + 44));
                            }
                            else{
                                g.setColor(colorE);                   
                                MaestroG.subscripterBs2("\u03b5","2C","E","","xT","",g,16,(int)(x1)+25, 
                                                     (int)((1.3 * y1) - (radiusnewy)));
                                FontMetrics fm; int xspace;
                                g.setFont(new Font("SanSerif",Font.PLAIN,14));
                                fm = g.getFontMetrics();
                                xspace = fm.stringWidth("Transmitted Wave");
                                MaestroG.subscripter("Transmitted Wave","","",g,14,(int)(x1 - xspace/2),
                                                        (int)((1.04 * y1) + 6.0*(radiusnewy) + 44));
                            }
                            
                            if(show_tang){
                            
                                if(state.Reflection_Coef.Real() > 0.0){
                                    ArrowLEFT(g, x1-vettoreL/2.0, y1+radiusnewy, vettoreL, colorE, 4);
                                }
                                else{
                                    ArrowLEFT(g, x1-vettoreL/2.0, y1+radiusnewy, vettoreL, colorE, 4);
                                }
                            
                            }
                            else{
                            
                                if(state.Reflection_Coef.Real() > 0.0){
                                    ArrowDOWN(g, x1, y1+radiusnewy+vettoreL, vettoreL, colorE, 4);
                                }
                                else{
                                    ArrowDOWN(g, x1, y1+radiusnewy+vettoreL, vettoreL, colorE, 4);
                                }
                            
                            }
                            
                        }
                        else{
                        //An oval for magnetic field

                         MaestroG.fillCircleThick(g, x1, y1+(radiusnewy), (double)radiusH, 2, Color.white);

                         MaestroG.drawLineThick(g, x1-(double)(radiusH/2),
                                        y1+(radiusnewy)-(double)(radiusH/2),
                                        x1+ (double)(radiusH/2),
                                        y1+(radiusnewy) + (double)(radiusH/2), 4,colorH);
                         MaestroG.drawLineThick(g, x1+(double)(radiusH/2),
                                        y1+(radiusnewy)-(double)(radiusH/2),
                                        x1- (double)(radiusH/2),
                                        y1+(radiusnewy) + (double)(radiusH/2), 4,colorH);

                         MaestroG.drawCircleThick(g, x1, y1+(radiusnewy), (double)radiusH,4,colorH);

                        g.setColor(colorH);                   
                        //MaestroG.subscripterB("H","\u22a5","",g,16, (int)((1.03 * x1) + (radiusnewx)), 
                          //                     (int)((0.96 * y1) + (radiusnewy)));
                        
                        // TWO ACCA
                        FontMetrics fm; int xspace;
                        g.setFont(new Font("SanSerif",Font.PLAIN,14));
                        fm = g.getFontMetrics();
                        xspace = fm.stringWidth("Transmitted Wave");
                        MaestroG.subscripter("Transmitted Wave","","",g,14,(int)(x1 - xspace/2),
                                                        (int)((1.04 * y1) + 3.0*(radiusnewy) + 44));
                        
                        MaestroG.subscripterBs("H","","yT","",g,16,(int)(x1 + radiusH +10), 
                                                     (int)((1.04 * y1) + (radiusnewy))+16);
                                                     
                        }
                        
                    }
                }
                
                // REFLECTED WAVE
                
                double y1shiftup = y1-10.0;
                double y1shiftup2 = y1-radiusH-5.0;
                radiusnewx = 50;// these two lines position normal field near interface
                radiusnewy = 20;
                
                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));
                    //double y1shiftup2 = y1-radiusH-5.0;
                    
                    if(!show_normal){
                        // E field vector
                            
                            if(show_tang){
                                g.setColor(colorE);                   
                                MaestroG.subscripterBs("E","","zR","",g,16,(int)(x1 + (radiusnewx))+35, 
                                                     (int)((0.91 * y1) - (radiusnewy)));
                                MaestroG.subscripter("Reflected Wave","","",g,14,(int)(x1 + 30), 
                                                     (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));
                            }
                            else{
                                g.setColor(colorE);                   
                                MaestroG.subscripterBs2("\u03b5","1","E","","xR","",g,16,(int)(x1 + (radiusnewx))+25, 
                                                     (int)((0.82 * y1) - (radiusnewy)));
                                MaestroG.subscripter("Reflected Wave","","",g,14,(int)(x1 + 30), 
                                                     (int)((0.96 * y1) - 6.0*(radiusnewy) - 30));
                            }
                        
                        if(show_tang){
                            
                            if(state.Reflection_Coef.Real() > 0.0){
                                ArrowLEFT(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorE, 4);
                            }
                            else{
                                ArrowLEFT(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorE, 4);
                            }
                            
                        }
                        else{
                            
                            if(state.Reflection_Coef.Real() > 0.0){
                                ArrowUP(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorE, 4);
                            }
                            else{
                                ArrowUP(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorE, 4);
                            }
                            
                        }
                        
                    }
                    else{
                        //An oval for magnetic field

                        MaestroG.fillCircleThick(g, x1+(radiusnewx), y1-(radiusnewy), (double)radiusH, 2, Color.white);

                        MaestroG.fillCircleThick(g, x1+(radiusnewx), y1-(radiusnewy), 5.0,2,colorH);
                        MaestroG.drawCircleThick(g, x1+(radiusnewx), y1-(radiusnewy), (double)radiusH,4,colorH);

                        g.setColor(Color.blue);                   
                        //MaestroG.subscripterB("H","\u22a5","",g,16,(int)(18+(x1) + (1.05*radiusnewx)), 
                          //                                   (int)((y1+18) - (1.05*radiusnewy)));

                        // FIVE ACCA
                        MaestroG.subscripter("Reflected Wave","","",g,14,(int)(x1 + 30), 
                                                         (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));

                        MaestroG.subscripterBs("H","","yR","",g,16,(int)((35.0 + 0.96*x1) + (1.05*radiusnewx)), 
                                                             (int)((0.96 * y1) - (radiusnewy)));

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

                    if(!show_normal){
                        // E field vector
                        
                            if(show_tang){
                                g.setColor(colorE);                   
                                MaestroG.subscripterBs("E","","zR","",g,16,(int)(x1 + (radiusnewx))+35, 
                                                     (int)((0.91 * y1) - (radiusnewy)));
                                MaestroG.subscripter("Reflected Wave","","",g,14,(int)(x1 + 30), 
                                                     (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));
                            }
                            else{
                                g.setColor(colorE);                   
                                MaestroG.subscripterBs2("\u03b5","1","E","","xR","",g,16,(int)(x1 + (radiusnewx))+25, 
                                                     (int)((0.82 * y1) - (radiusnewy)));
                                MaestroG.subscripter("Reflected Wave","","",g,14,(int)(x1 + 30), 
                                                     (int)((0.96 * y1) - 6.0*(radiusnewy) - 30));
                            }
                            
                        if(show_tang){
                             
                            if(state.Reflection_Coef.Real() > 0.0){
                                ArrowRIGHT(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorE, 4);
                            }
                            else{
                                ArrowRIGHT(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorE, 4);
                            }
                            
                        }
                        else{
                            
                            if(state.Reflection_Coef.Real() < 0.0){
                                ArrowDOWN(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorE, 4);
                            }
                            else{
                                ArrowDOWN(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorE, 4);
                            }
                            
                        }
                        
                    }
                    else{
                        //An oval for magnetic field

                        MaestroG.fillCircleThick(g, x1+(radiusnewx), y1-(radiusnewy), (double)radiusH, 2, Color.white);

                        MaestroG.drawLineThick(g, x1+(radiusnewx)-(double)(radiusH/2),
                                        y1-(radiusnewy)-(double)(radiusH/2),
                                        x1+(radiusnewx) + (double)(radiusH/2),
                                        y1-(radiusnewy) + (double)(radiusH/2), 4,colorH);
                         MaestroG.drawLineThick(g, x1+(radiusnewx)+(double)(radiusH/2),
                                        y1-(radiusnewy)-(double)(radiusH/2),
                                        x1+(radiusnewx) - (double)(radiusH/2),
                                        y1-(radiusnewy) + (double)(radiusH/2), 4,colorH);

                        MaestroG.drawCircleThick(g, x1+(radiusnewx), y1-(radiusnewy), (double)radiusH,4,colorH);

                        g.setColor(colorH);                   

                        //MaestroG.subscripterB("H","\u22a5","",g,16,5+(int)((0.9 * x1) + (1.08*radiusnewx))-7, 
                          //                               (int)((0.93 * y1) - (1.03*radiusnewy)));

                        // SIX ACCA
                        MaestroG.subscripter("Reflected Wave","","",g,14,(int)(x1 + 30), 
                                                         (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));

                        MaestroG.subscripterBs("H","","yR","",g,16,(int)((35.0 + 0.96*x1) + (1.05*radiusnewx)), 
                                                             (int)((0.96 * y1) - (radiusnewy)));
                    }
                    
                }
              }
                
        }
	else{  // Perpendicular Polarization
               
                double x2EE, y2EE;
                
                //if(state.Reflection_Coef.Imaginary() == 0.0)
                {
                //------------------ transmitted field
                    if(Field_Out){
                        x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.sin(state.theta2)));
                        y2 = (double)(getSize().height/2+(int)(radius_powernew*Math.cos(state.theta2))-1);
                        x2T = (double)(getSize().width/2 +(int)((radius+10)*Math.sin(state.theta2)));
                        y2T = (double)(getSize().height/2+(int)((radius+10)*Math.cos(state.theta2))-1);

                        x2EE = x1+(radius_powernew2*Math.sin(state.theta2) + fieldL*(state.Transmission_CoefH.Magnitude()+extra) * Math.cos(state.theta2));
                        y2EE = y1+(radius_powernew2*Math.cos(state.theta2) - fieldL*(state.Transmission_CoefH.Magnitude()+extra) * Math.sin(state.theta2));


                        if(!show_normal){
                            // H field vector
                            if(show_tang){
                                g.setColor(colorH);                   
                                MaestroG.subscripterBs("H","","zT","",g,16,(int)(x1)-10, 
                                                     (int)(1.2 * y1));
                                FontMetrics fm; int xspace;
                                g.setFont(new Font("SanSerif",Font.PLAIN,14));
                                fm = g.getFontMetrics();
                                xspace = fm.stringWidth("Transmitted Wave");
                                MaestroG.subscripter("Transmitted Wave","","",g,14,(int)(x1 - xspace/2),
                                                        (int)((1.04 * y1) + 3.0*(radiusnewy) + 44));
                            }
                            else{
                                g.setColor(colorH);                   
                                MaestroG.subscripterBs2("\u03bc","2","H","","xT","",g,16,(int)(x1)+25, 
                                                     (int)((1.3 * y1) - (radiusnewy)));
                                FontMetrics fm; int xspace;
                                g.setFont(new Font("SanSerif",Font.PLAIN,14));
                                fm = g.getFontMetrics();
                                xspace = fm.stringWidth("Transmitted Wave");
                                MaestroG.subscripter("Transmitted Wave","","",g,14,(int)(x1 - xspace/2),
                                                        (int)((1.04 * y1) + 6.0*(radiusnewy) + 44));
                            }
                            
                            if(show_tang){
                                    ArrowLEFT(g, x1-vettoreL/2.0, y1+radiusnewy, vettoreL, colorH, 4);    
                            }
                            else{
                                    ArrowDOWN(g, x1, y1+radiusnewy+vettoreL, vettoreL, colorH, 4);                                
                            }
                        }
                        else{
                            //An oval for electric field
                            g.setColor(colorE);
                            FontMetrics fm; int xspace;
                            g.setFont(new Font("SanSerif",Font.PLAIN,14));
                            fm = g.getFontMetrics();
                            xspace = fm.stringWidth("Transmitted Wave");
                            MaestroG.subscripter("Transmitted Wave","","",g,14,(int)(x1 - xspace/2), 
                                                         (int)((1.04 * y1) + 3.0*(radiusnewy) + 44));


                             MaestroG.fillCircleThick(g, x1, y1+(radiusnewy), (double)radiusH, 2, Color.white);

                            MaestroG.fillCircleThick(g, x1, y1+(radiusnewy), 5.0,2,colorE);
                            MaestroG.drawCircleThick(g, x1, y1+(radiusnewy), (double)radiusH,4,colorE);

                            g.setColor(colorE);                   
                            MaestroG.subscripterBs("E","","yT","",g,16,(int)(x1 + radiusH +10), 
                                                         (int)((1.04 * y1) + (radiusnewy))+16);
                        }
                        
                    }
                    else{
                        x2 = (double)(getSize().width/2 +(int)(radius_powernew*Math.sin(state.theta2)));
                        y2 = (double)(getSize().height/2+(int)(radius_powernew*Math.cos(state.theta2))-1);
                        x2T = (double)(getSize().width/2 +(int)((radius+10)*Math.sin(state.theta2)));
                        y2T = (double)(getSize().height/2+(int)((radius+10)*Math.cos(state.theta2))-1);
                        // WORK ON THESE
                        x2EE = x1+(radius_powernew2*Math.sin(state.theta2) + fieldL*(state.Transmission_CoefH.Magnitude()+extra) * Math.cos(state.theta2));
                        y2EE = y1+(radius_powernew2*Math.cos(state.theta2) - fieldL*(state.Transmission_CoefH.Magnitude()+extra) * Math.sin(state.theta2));
                        
                            
                        
                        if(!show_normal){
                            if(show_tang){
                                g.setColor(colorH);                   
                                MaestroG.subscripterBs("H","","zT","",g,16,(int)(x1)-10, 
                                                     (int)(1.2 * y1));
                                FontMetrics fm; int xspace;
                                g.setFont(new Font("SanSerif",Font.PLAIN,14));
                                fm = g.getFontMetrics();
                                xspace = fm.stringWidth("Transmitted Wave");
                                MaestroG.subscripter("Transmitted Wave","","",g,14,(int)(x1 - xspace/2),
                                                        (int)((1.04 * y1) + 3.0*(radiusnewy) + 44));
                            }
                            else{
                                g.setColor(colorH);                   
                                MaestroG.subscripterBs2("\u03bc","2","H","","xT","",g,16,(int)(x1)+25, 
                                                     (int)((1.3 * y1) - (radiusnewy)));
                                FontMetrics fm; int xspace;
                                g.setFont(new Font("SanSerif",Font.PLAIN,14));
                                fm = g.getFontMetrics();
                                xspace = fm.stringWidth("Transmitted Wave");
                                MaestroG.subscripter("Transmitted Wave","","",g,14,(int)(x1 - xspace/2),
                                                        (int)((1.04 * y1) + 6.0*(radiusnewy) + 44));
                            }
                            
                            if(show_tang){
                                    ArrowRIGHT(g, x1-vettoreL/2.0, y1+radiusnewy, vettoreL, colorH, 4);    
                            }
                            else{
                                    ArrowUP(g, x1, y1+radiusnewy+vettoreL, vettoreL, colorH, 4);                                
                            }
                        }
                        else{
                            //An oval for electric field
                            g.setColor(colorE);
                            FontMetrics fm; int xspace;
                            g.setFont(new Font("SanSerif",Font.PLAIN,14));
                            fm = g.getFontMetrics();
                            xspace = fm.stringWidth("Transmitted Wave");
                            MaestroG.subscripter("Transmitted Wave","","",g,14,(int)(x1 - xspace/2), 
                                                         (int)((1.04 * y1) + 3.0*(radiusnewy) + 44));

                             MaestroG.fillCircleThick(g, x1, y1+(radiusnewy), (double)radiusH, 2, Color.white);

                             MaestroG.drawLineThick(g, x1-(double)(radiusH/2),
                                            y1+(radiusnewy)-(double)(radiusH/2),
                                            x1+(double)(radiusH/2),
                                            y1+(radiusnewy) + (double)(radiusH/2), 4,colorE);
                             MaestroG.drawLineThick(g, x1+(double)(radiusH/2),
                                            y1+(radiusnewy)-(double)(radiusH/2),
                                            x1-(double)(radiusH/2),
                                            y1+(radiusnewy) + (double)(radiusH/2), 4,colorE);

                             MaestroG.drawCircleThick(g, x1, y1+(radiusnewy),(double)radiusH,4,colorE);

                            g.setColor(colorE);                   
                            MaestroG.subscripterBs("E","","yT","",g,16,(int)(x1 + radiusH +10), 
                                                         (int)((1.04 * y1) + (radiusnewy))+16);
                        }
                    }
                }
                
                
                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));
                    
                    if(!show_normal){
                        // H field vector
                        
                            if(show_tang){
                                g.setColor(colorH);                   
                                MaestroG.subscripterBs("H","","zR","",g,16,(int)(x1 + (radiusnewx))+35, 
                                                     (int)((0.91 * y1) - (radiusnewy)));
                                MaestroG.subscripter("Reflected Wave","","",g,14,(int)(x1 + 30), 
                                                     (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));
                            }
                            else{
                                g.setColor(colorH);                   
                                MaestroG.subscripterBs2("\u03bc","1","H","","xR","",g,16,(int)(x1 + (radiusnewx))+25, 
                                                     (int)((0.82 * y1) - (radiusnewy)));
                                MaestroG.subscripter("Reflected Wave","","",g,14,(int)(x1 + 30), 
                                                     (int)((0.96 * y1) - 6.0*(radiusnewy) - 30));
                            }
                        
                        
                        
                        if(show_tang){
                             
                            if(state.Reflection_CoefH.Real() > 0.0){
                                ArrowRIGHT(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorH, 4);
                            }
                            else{
                                ArrowRIGHT(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorH, 4);
                            }
                            
                        }
                        else{
                            
                            if(state.Reflection_CoefH.Real() < 0.0){
                                ArrowDOWN(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorH, 4);
                            }
                            else{
                                ArrowDOWN(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorH, 4);
                            }
                            
                        }
                    }
                    else{
                        //An oval for electric field

                        MaestroG.fillCircleThick(g, x1+(radiusnewx), y1-(radiusnewy), (double)radiusH, 2, Color.white);

                        MaestroG.fillCircleThick(g, x1+(radiusnewx), y1-(radiusnewy), 5.0,2,colorE);

                        MaestroG.drawCircleThick(g, x1+(radiusnewx), y1-(radiusnewy), (double)radiusH,4,colorE);

                        g.setColor(colorE);                   
                        MaestroG.subscripter("Reflected Wave","","",g,14,(int)(x1 + 30), 
                                                         (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));

                        //MaestroG.subscripterB("E","\u22a5","",g,16,5+(int)((0.9 * x1) + (1.08*radiusnewx))-5, 
                          //                               (int)((0.93 * y1) - (1.03*radiusnewy)));

                        MaestroG.subscripterBs("E","","yR","",g,16,(int)((35.0 + 0.96*x1) + (1.05*radiusnewx)), 
                                                             (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));
                    
                    if(!show_normal){
                            if(show_tang){
                                g.setColor(colorH);                   
                                MaestroG.subscripterBs("H","","zR","",g,16,(int)(x1 + (radiusnewx))+35, 
                                                     (int)((0.91 * y1) - (radiusnewy)));
                                MaestroG.subscripter("Reflected Wave","","",g,14,(int)(x1 + 30), 
                                                     (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));
                            }
                            else{
                                g.setColor(colorH);                   
                                MaestroG.subscripterBs2("\u03bc","1","H","","xR","",g,16,(int)(x1 + (radiusnewx))+25, 
                                                     (int)((0.82 * y1) - (radiusnewy)));
                                MaestroG.subscripter("Reflected Wave","","",g,14,(int)(x1 + 30), 
                                                     (int)((0.96 * y1) - 6.0*(radiusnewy) - 30));
                            }
                        
                        if(show_tang){
                             
                            if(state.Reflection_CoefH.Real() > 0.0){
                                ArrowLEFT(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorH, 4);
                            }
                            else{
                                ArrowLEFT(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorH, 4);
                            }
                        }
                        else{
                            
                            if(state.Reflection_CoefH.Real() < 0.0){
                                ArrowUP(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorH, 4);
                            }
                            else{
                                ArrowUP(g, x1+radiusnewx, y1-radiusnewy, vettoreL, colorH, 4);
                            }   
                        }
                    }
                    else{
                        //An oval for electric field

                        MaestroG.fillCircleThick(g, x1+(radiusnewx),
                                            y1-(radiusnewy),
                                            (double)radiusH, 2, Color.white);

                        MaestroG.drawLineThick(g, x1+(radiusnewx)-(double)(radiusH/2),
                                        y1-(radiusnewy)-(double)(radiusH/2),
                                        x1+(radiusnewx) + (double)(radiusH/2),
                                        y1-(radiusnewy) + (double)(radiusH/2), 4,colorE);
                        MaestroG.drawLineThick(g, x1+(radiusnewx)+(double)(radiusH/2),
                                        y1-(radiusnewy)-(double)(radiusH/2),
                                        x1+(radiusnewx) - (double)(radiusH/2),
                                        y1-(radiusnewy) + (double)(radiusH/2), 4,colorE);

                        MaestroG.drawCircleThick(g, x1+(radiusnewx), y1-(radiusnewy), (double)radiusH,4,colorE);

                        g.setColor(colorE);                   
                        MaestroG.subscripter("Reflected Wave","","",g,14,(int)(x1 + 30), 
                                                         (int)((0.96 * y1) - 3.0*(radiusnewy) - 30));

                        MaestroG.subscripterBs("E","","yR","",g,16,(int)((35.0 + 0.96*x1) + (1.05*radiusnewx)), 
                                                             (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){}
}
