import java.awt.*;
import java.awt.geom.*;

public class PlaneWaveCanvas extends Canvas{
    
    private static final Color bgcolor = new Color(255,255,245);
    private static final Color rotorcolor = Color.green;
   
    private double zmax, zmin, ymax, ymin, xmax, xmin, pmax, pmin;
    private boolean IsYRangeMaxSet, IsYRangeMinSet, IsXRangeMaxSet, IsXRangeMinSet; 
    private boolean IsZRangeMaxSet, IsZRangeMinSet, IsPRangeMaxSet, IsPRangeMinSet;
    private int Orth_Axis_Front, Orth_Axis_Back; //Size of orthogonal axis (y-direction);
    private int axis_one_ref, axis_two_ref, down, down2, down3;
    int yshift = 250;
    private int LeftMargin, RightMargin, TopMargin, TopMargin2, 
                BottomMargin, BottomMargin2;
    
    private boolean IsEquivalent = false;
    private boolean refz;
    
    private double zpos[];   //position of the reference planes
    private double alpha, beta, frequency, total_length, wt, wavelength, Ex, Hy, Pz, skin_depth;
    private static final String znames[] ={"A", "B"};
    private int WIDTH, HEIGHT, SkinZ1_z, SkinZ1_x, SkinZ2_z, SkinZ2_x;
    private int PSkinZ1_z, PSkinZ1_x, PSkinZ2_z, PSkinZ2_x;
    private String Z1, Z2, Z3, X1, X2, X3, X4, TITLE, legend1, legend2;
    private Image im;
    private Graphics buf;
    private int VPos = 0;
   
    private static final Font TitleFont = new Font("SanSerif",Font.BOLD,16);
    private static final Font TitleFont2 = new Font("SanSerif",Font.BOLD,20);
    private static final Font LabelFont = new Font("SanSerif",Font.PLAIN,11);
    private double[] z;
    //private double myangle = 135.0+45;
    private double[] EField_z, HField_z, PField_z;
    private int[] Field_zx, Field_zx_old, Decay1, Decay1B, Decay2, Decay2B; 
    private int[] Field_zy, Field_zy_old;
    private int[] Field_zp, Field_zp_old, Decay3; 
    
    private int[] zx, zx_old, zy, zy_old, zy2, zy2B;
    private int current_z;
    private double H_ratio, E_ratio, P_ratio;
    private int N;
    private static final double theta = 135.*Math.PI / 180.0;//0.75
    
    private double sintheta = Math.sin(theta);
    private double costheta = Math.cos(theta);
    private boolean IsTraceOn, IsCleanUpOn, IsDynamic,IsPhasorOn, IsSkinDepthOn, IsEon, IsHon;
    private int nlines = 8;
    private double angle = 135.0;
    private double costhetanew,sinthetanew;   
    public boolean SliceIt = false;
    PlaneWave_State state;
    
    public PlaneWaveCanvas(PlaneWave_State state){
	super();
        this.state = state;
        
	N = 360;
	setBackground(bgcolor);
	
        down = state.s165; 
        down2 = state.s100; 
        down3 = state.s160;
        
        Orth_Axis_Front = state.s63; 
        Orth_Axis_Back = state.s63; //Size of orthogonal axis (y-direction);   
        yshift = state.s250;
        
        LeftMargin = state.s90; 
        RightMargin = state.s85; 
        TopMargin = state.s60; 
        TopMargin2 = TopMargin+yshift;
        BottomMargin = state.s200 + state.s10; 
        BottomMargin2 = -state.s40;
    
	double frequency = 1.0E9;
	alpha = 0.0;
	wavelength = 1.0/Math.sqrt(8.8541878176E-12*1.25663706144E-6)/frequency;
	total_length =1.0;
	Ex = 1.0;
	Hy = Ex/Math.sqrt(1.25663706144/8.8541878176E-6/9.0);
        Pz = Ex;//normalize to Ex
        //costheta = Math.cos(myangle*Math.PI/180.0);    
	//sintheta = Math.cos(myangle*Math.PI/180.0);    
	costhetanew = Math.cos(angle*Math.PI/180.0);    
	sinthetanew = Math.sin(angle*Math.PI/180.0);
        wt = 0.0;
	Z1 = "0";
	Z2 = "L";
	Z3 = "     z";
	X1 = "  ";
	X2 = "  ";
	TITLE = "  ";
	IsYRangeMaxSet = false;
	IsYRangeMinSet = false;
	IsXRangeMaxSet = false;
	IsXRangeMinSet = false;
        IsPRangeMaxSet = true;
	IsPRangeMinSet = true;
	IsTraceOn = false;
	IsCleanUpOn = false;
	IsDynamic = false;
	IsPhasorOn = true;
        IsSkinDepthOn = true;
        
        xmax = 2.0;
	xmin = -2.0;
	ymax = 2.0;
	ymin = -2.0;
        pmax = Pz;
        pmin = -Pz;
        
	zmax = 1.0;
	zmin = 0.0;
        skin_depth = Double.POSITIVE_INFINITY;
        
        IsEon = true;
        IsHon = false;
        refz = false;
        
	axis_one_ref = (int)(0.85*getSize().height/2);
	axis_two_ref  = (int)(0.85*getSize().height/2);
        
        Orth_Axis_Front = (int)(0.55*getSize().height/2);
	Orth_Axis_Back  = (int)(0.55*getSize().height/2);
        
        H_ratio = 0.55/0.85;
        E_ratio = 0.75/0.85;
        P_ratio = 0.75/0.85;
        try{
	    z = new double[N];
	    EField_z = new double[N];
	    HField_z = new double[N];
            PField_z = new double[N];
	    Field_zx = new int[N];
	    Field_zx_old = new int[N];
	    Decay1 = new int[N];
	    Decay2 = new int[N];
            Decay1B = new int[N];
	    Decay2B = new int[N];
            
            Decay3 = new int[N];
	    Field_zy = new int[N];
	    Field_zy_old = new int[N];
            Field_zp = new int[N];
	    Field_zp_old = new int[N];
	    zx = new int[N];
	    zx_old = new int[N];
	    zy = new int[N];
	    zy2 = new int[N];
            zy2B = new int[N];
	    zy_old = new int[N];
	}
	catch(Exception e){e.printStackTrace();}
	for(int i = 0; i < z.length; i++){
	    z[i] = (double)i;
	    EField_z[i] = 0.0;
	    HField_z[i] = 0.0;
            PField_z[i] = 0.0;
	    Field_zx[i] = 0;
	    Field_zx_old[i] = 0;
            Field_zp[i] = 0;
	    Field_zp_old[i] = 0;
	    Decay1[i] = 0;
	    Decay2[i] = 0;
            Decay1B[i] = 0;
	    Decay2B[i] = 0;
            
            Decay3[i] = 0;
	    zx[i] = i;
	    zx_old[i] = i;
	    zy[i] = i;
	    zy_old[i] = i;
	    zy2[i] =i;
            zy2B[i] =i;
	}
	//Positions of reference planes
	zpos = new double[2];
	
	//Position of rotor
	current_z = 0;
	
    }
    
    public void paint(Graphics g){
	if(im == null){
	    im = createImage(getSize().width,getSize().height);
	    buf = im.getGraphics();
	    drawGraph(buf);
	}
	else{
	 drawGraph(buf);
	}
	g.drawImage(im,0,0,null);
    }
    
    public void drawGraph(Graphics g){
	g.draw3DRect(0,0,getSize().width-1,getSize().height-1,false);
	
	if(!IsDynamic){
	    g.clearRect(0,0,getSize().width,getSize().height);
	}
        g.clearRect(0,0,getSize().width,getSize().height);
	g.setColor(Color.black);
	g.draw3DRect(0,0,getSize().width-1,getSize().height-1,false);
	
        //drawAxis(g);
	//labelDetect();
	//drawZeroLine(g);
	//drawLabels(g);
        drawCircuit(g);
	drawPoints(g,1);
        drawCircuit2(g);
	//drawLabels(g);
	//drawRotor(g);
    }
       
    private synchronized void drawCircuit(Graphics g){
        
	int ytop = 20*getSize().height/100;
        int ybottom = 80*getSize().height/100;
        
        int xleft = getSize().width/8;
        int xright = 6*getSize().width/8;
        
        int plate1_y = ytop+25*(ybottom-ytop)/100 - state.s10;
        int plate2_y = ytop+75*(ybottom-ytop)/100 + state.s10;
        int xsize = 2*getSize().width/3;
        int ysize = getSize().height/10;
        
        Graphics2D g2d = (Graphics2D)g;
        // wire connecting to bottom plate - must be drawn first
        drawLineThick(g,(double)xright,(double)plate2_y,(double)xright,(double)ybottom,3,Color.black);
        drawCircleThick(g, xright, plate2_y, state.s3, 6, Color.black);
        
        g.setColor(new Color(200,200,200));
        int rule; 
        float alphaG = 0.5f;
        rule = AlphaComposite.SRC_OVER;
        
        int xx1 = xright-xsize/4;
        int xx2 = xright+xsize/4;
        int yy1 = plate1_y-ysize/2;
        int yy2 = plate1_y+ysize/2;
        int yy3 = plate2_y-ysize/2;
        int yy4 = plate2_y+ysize/2;
        //----------------------------------------------------------------------
        Color labcolor = new Color(180, 180, 180);
        g.setColor(labcolor);
        // thickness of capacitor arrows
        drawLineThick(g,(double)(xx2+state.s2),(double)(yy1+state.s5),(double)(xx2+state.s30),(double)(yy1+state.s5),1,labcolor);
        drawLineThick(g,(double)(xx2+state.s2),(double)(yy3-state.s5),(double)(xx2+state.s30),(double)(yy3-state.s5),1,labcolor);
        drawLineThick(g,(double)(xx2+state.s15),(double)(yy1+state.s5),(double)(xx2+state.s15),(double)(yy3-state.s5),1,labcolor);
        
        //draw arrow heads
		Polygon pX1 = new Polygon();
                pX1.addPoint(xx2+state.s15,yy1+state.s5);
		pX1.addPoint(xx2+state.s15+state.s2,yy1+state.s12);
		pX1.addPoint(xx2+state.s15-state.s2,yy1+state.s12);
                g.drawPolygon(pX1);
		g.fillPolygon(pX1);
                
                Polygon pY1 = new Polygon();
                pY1.addPoint(xx2+state.s15,yy3-state.s5);
		pY1.addPoint(xx2+state.s15+state.s2,yy3-state.s12);
		pY1.addPoint(xx2+state.s15-state.s2,yy3-state.s12);
                g.drawPolygon(pY1);
		g.fillPolygon(pY1);
                
        //MaestroG.drawArrowtip(xx2+state.s15, yy1+state.s15, 5, g);//***********
        //MaestroG.drawArrowtip(xx2+state.s15, yy3-state.s15, 6, g); //************
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        MaestroG.subscripterSansItalic3("d","","",g,state.font24,xx2+state.s20,yy1+(plate2_y-plate1_y)/2);
        
        drawLineThick(g,(double)xx1,(double)yy2,(double)xx1,(double)yy2-state.s30,1,labcolor);
        drawLineThick(g,(double)xx1+xsize/8,(double)yy1,(double)xx1+xsize/8,(double)yy1-state.s30,1,labcolor);
        drawLineThick(g,(double)xx2,(double)yy1,(double)xx2,(double)yy1-state.s30,1,labcolor);
        
        MaestroG.subscripterSansItalic3("w","","",g,state.font24,xx1+xsize/16-state.s20,yy2-(yy2-yy1)/2-state.s25);
        drawLineThick(g,(double)xx1,(double)yy2-state.s15,(double)xx1+xsize/8,(double)yy1-state.s15,1,labcolor);
        //draw oblique arrow heads
		Polygon pX = new Polygon();
                //draw oblique arrow head        
                pX.addPoint(xx1+xsize/8,yy1-state.s15);
		pX.addPoint(xx1+xsize/8-state.s4,yy1-state.s9);
		pX.addPoint(xx1+xsize/8-state.s6,yy1-state.s13);
                g.drawPolygon(pX);
		g.fillPolygon(pX);
                
                Polygon pY = new Polygon();
                //draw oblique arrow head        
                pY.addPoint(xx1,yy2-state.s15);
		pY.addPoint(xx1+state.s6,yy2-state.s17);
		pY.addPoint(xx1+state.s4,yy2-state.s21);
                g.drawPolygon(pY);
		g.fillPolygon(pY);
        
        MaestroG.subscripterSansItalic3("l","","",g,state.font24,xx1+xsize/8+(xx2-xx1-xsize/8)/2,yy1-state.s20);
        drawLineThick(g,(double)xx1+xsize/8,(double)yy1-state.s15,(double)xx2,(double)yy1-state.s15,1,labcolor);
        //MaestroG.drawArrowtip(xx1+xsize/8+state.s10, yy1-state.s15, 8, g);
        //MaestroG.drawArrowtip(xx2-state.s10, yy1-state.s15, 7, g);
        
        //draw arrow heads
		Polygon pX2 = new Polygon();
                pX2.addPoint(xx2,yy1-state.s15);
		pX2.addPoint(xx2-state.s7,yy1-state.s13);
		pX2.addPoint(xx2-state.s7,yy1-state.s17);
                g.drawPolygon(pX2);
		g.fillPolygon(pX2);
                
                Polygon pY2 = new Polygon();
                pY2.addPoint(xx1+xsize/8,yy1-state.s15);
		pY2.addPoint(xx1+xsize/8+state.s7,yy1-state.s13);
		pY2.addPoint(xx1+xsize/8+state.s7,yy1-state.s17);
                g.drawPolygon(pY2);
		g.fillPolygon(pY2);
                
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        //----------------------------------------------------------------------               
        //Thickness of capacitor plates (hidden edges)           
        g.setColor(new Color(190,140,80));
        // top plate
        //g.fillRect(xx1+xsize/8,yy1+1,38*xsize/100-1,5);
        
        // bottom plate
        g.fillRect(xx1+xsize/8,yy3-state.s5,38*xsize/100-state.s1,state.s5);
        
        // Side plate
        g.setColor(new Color(220,230,245));
        Polygon face4 = new Polygon();
                    face4.addPoint(xx1,yy2);
                    face4.addPoint(xx1+xsize/8,yy1);
                    face4.addPoint(xx1+xsize/8,yy3);
                    face4.addPoint(xx1,yy4);
                    g.drawPolygon(face4);
                    g.fillPolygon(face4);
        g.setColor(Color.black);
        //g.drawPolygon(face4);
                    
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        g2d.setComposite(AlphaComposite.getInstance(rule, alphaG));
        
        // Bottom plate
        g.setColor(new Color(190,140,80));
        Polygon plate2 = new Polygon();
                    plate2.addPoint(xx1,yy4);
                    plate2.addPoint(xx1+xsize/8,yy3);
                    plate2.addPoint(xx2,yy3);
                    plate2.addPoint(xx2-xsize/8,yy4);
                    g.drawPolygon(plate2);
                    g.fillPolygon(plate2);
                    g.setColor(Color.black);
                    g.drawPolygon(plate2);
        g2d.setComposite(AlphaComposite.getInstance(rule,1.0f));
        
        g.setColor(new Color(80,60,20));
        Polygon pla3 = new Polygon();
                    pla3.addPoint(xx1,yy2);
                    pla3.addPoint(xx1+xsize/8,yy1);
                    pla3.addPoint(xx1+xsize/8,yy1+state.s5);
                    pla3.addPoint(xx1,yy2+state.s5);
                    //g.drawPolygon(pla3);
                    //g.fillPolygon(pla3);
                             
        Polygon pla4 = new Polygon();
                    pla4.addPoint(xx1,yy4-state.s5);
                    pla4.addPoint(xx1+xsize/8,yy3-state.s5);
                    pla4.addPoint(xx1+xsize/8,yy3);
                    pla4.addPoint(xx1,yy4);
                    g.drawPolygon(pla4);
                    g.fillPolygon(pla4);
        
        g2d.setComposite(AlphaComposite.getInstance(rule, 0.85f));
        g.setColor(new Color(190,140,80));
        Polygon plate1 = new Polygon();
                    plate1.addPoint(xx1,yy2);
                    plate1.addPoint(xx1+xsize/8,yy1);
                    plate1.addPoint(xx2,yy1);
                    plate1.addPoint(xx2-xsize/8,yy2);
                    //g.drawPolygon(plate1);
                    //g.fillPolygon(plate1);
                    g.setColor(Color.black);
                    //g.drawPolygon(plate1);
        
        g2d.setComposite(AlphaComposite.getInstance(rule, 0.8f));            
        g.setColor(new Color(230,230,255));
        Polygon face1 = new Polygon();
                    face1.addPoint(xx1,yy2);
                    face1.addPoint(xx2-xsize/8,yy2);
                    face1.addPoint(xx2-xsize/8,yy4);
                    face1.addPoint(xx1,yy4);
                    g.drawPolygon(face1);
                    g.fillPolygon(face1);
                    g.setColor(Color.black);
                    g.drawPolygon(face1);
                    
        g2d.setComposite(AlphaComposite.getInstance(rule, 0.8f));            
        g.setColor(new Color(200,200,225));
        Polygon face2 = new Polygon();
                    face2.addPoint(xx2-xsize/8,yy2);
                    face2.addPoint(xx2,yy1);
                    face2.addPoint(xx2,yy3);
                    face2.addPoint(xx2-xsize/8,yy4);
                    //g.drawPolygon(face2);
                    //g.fillPolygon(face2);
                    g.setColor(Color.black);
                    //g.drawPolygon(face2);
                    
        //Thickness of capacitor plates (visible edges)            
        g.setColor(new Color(220,170,110));
        //g.fillRect(xx1,yy2+1,38*xsize/100-1,5);
        
        g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));            
        g.fillRect(xx1,yy4-state.s5,38*xsize/100-state.s1,state.s5);
        g2d.setComposite(AlphaComposite.getInstance(rule, 0.8f));            
        
        g.setColor(new Color(150,100,40));
        Polygon pla1 = new Polygon();
                    pla1.addPoint(xx2-xsize/8,yy2);
                    pla1.addPoint(xx2,yy1);
                    pla1.addPoint(xx2,yy1+state.s5);
                    pla1.addPoint(xx2-xsize/8,yy2+state.s5);
                    //g.drawPolygon(pla1);
                    //g.fillPolygon(pla1);
                             
        Polygon pla2 = new Polygon();
                    pla2.addPoint(xx2-xsize/8,yy4-state.s5);
                    pla2.addPoint(xx2,yy3-state.s5);
                    pla2.addPoint(xx2,yy3);
                    pla2.addPoint(xx2-xsize/8,yy4);
                    g.drawPolygon(pla2);
                    g.fillPolygon(pla2);
         
        if(SliceIt){
            g.setColor(new Color(220,220,220));
            g2d.setComposite(AlphaComposite.getInstance(rule, 0.85f));

            Polygon Slice = new Polygon();
                        Slice.addPoint(xx1+xsize/16,((yy2+(yy4-yy2)/2)+(yy1+(yy3-yy1)/2))/2);
                        Slice.addPoint(xx1+xsize/8,yy1+(yy3-yy1)/2);
                        Slice.addPoint(xx2,yy1+(yy3-yy1)/2);
                        Slice.addPoint(xx2-xsize/16,((yy2+(yy4-yy2)/2)+(yy1+(yy3-yy1)/2))/2);
                        g.drawPolygon(Slice);
                        g.fillPolygon(Slice);
            
            g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
            //drawLineThickDashed(g,(double)xx1,yy2+(yy4-yy2)/2,xx2-xsize/8,yy2+(yy4-yy2)/2,1,Color.black);
            //drawLineThickDashed(g,(double)xx1,yy2+(yy4-yy2)/2,xx1+xsize/8,yy1+(yy3-yy1)/2,1,Color.black);
            //drawLineThickDashed(g,xx2,yy1+(yy3-yy1)/2,xx1+xsize/8,yy1+(yy3-yy1)/2,1,Color.black);
            //drawLineThickDashed(g,xx2,yy1+(yy3-yy1)/2,xx2-xsize/8,yy2+(yy4-yy2)/2,1,Color.black);
        }            
                    
        g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
        double VRadius = (double)state.s35; //for the voltage generator
        double VCentery = ytop + (ybottom - ytop)/2; // vertical position of voltage generator 
        
        // circuit wires -------------------------------------------------------
        // Wires connecting to the voltage generator
        drawLineThick(g,(double)xleft,(double)ytop,(double)xleft,(double)VCentery - VRadius,3,Color.black);
        drawLineThick(g,(double)xleft,(double)VCentery + VRadius,(double)xleft,(double)ybottom,3,Color.black);
        
        drawLineThick(g,(double)xleft,(double)ytop,(double)xright,(double)ytop,3,Color.black);
        drawLineThick(g,(double)xleft,(double)ybottom,(double)xright,(double)ybottom,3,Color.black);
        // wire connecting to top plate
        drawLineThick(g,(double)xright,(double)ytop,(double)xright,(double)plate1_y,3,Color.black);
        drawCircleThick(g, xright, plate1_y, state.s3, 6, Color.black);
        
        //----------------------------------------------------------------------
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        if(SliceIt){
            g.setColor(Color.red.darker());
            MaestroG.subscripterSansItalic3("S","2","",g,state.font22,xx1-state.s40,yy1+60*(plate2_y-plate1_y)/80);
        }
        g.setColor(Color.red);
        MaestroG.subscripterSansItalic3("E","","",g,state.font22,xx1+xsize/8,yy4+state.s25);
        MaestroG.subscripterSansItalic3("V","S","",g,state.font22,state.s40,yy1+70*(plate2_y-plate1_y)/80);
        
        g.setColor(Color.blue.darker());
        MaestroG.subscripterSansItalic3("I","2d","",g,state.font22,xright+state.s10,yy4+state.s25);
        //Voltage generator
        drawCircleThick(g, xleft, VCentery, VRadius, 6, Color.blue);
        
        drawLineThick(g,(double)xx2,(double)yy3+state.s10,(double)xx2,(double)yy3+state.s50,2,Color.black);
        drawLineThick(g,(double)xx2,(double)yy3+state.s50,(double)xx2-state.s5,(double)yy3+state.s40,2,Color.black);
        drawLineThick(g,(double)xx2,(double)yy3+state.s50,(double)xx2+state.s5,(double)yy3+state.s40,2,Color.black);
        MaestroG.subscripterSansItalic3("y","","",g,state.font22,xx2+state.s15,yy3+state.s55);
        
        g.setColor(Color.gray);
        MaestroG.subscripternew2("C = ","\u03b5","o","\u03b5","r"," (","l \u00d7 w",")","/d","",g,state.font20,xright,state.s45); // NOTE "l" is a lower case L, not "one"
        //int delx = (xright+xsize/8-xx1)/100;
        int delx = (xx2 - xx1)/100;
        
        int yA = yy2+state.s17; int yB = yy4-state.s10;
        if(Math.sin(wt) > 0){
            //g.setColor(new Color(255,255-(int)(Math.sin(wt)*255),255-(int)(Math.sin(wt)*255)));
            g2d.setComposite(AlphaComposite.getInstance(rule, (float)(Math.abs(Math.sin(wt)))));
            g.setColor(Color.red);
            g.setFont(TitleFont);
            g.drawString("+", xx1+15*delx, yA);
            g.drawString("+", xx1+29*delx, yA);
            g.drawString("+", xx1+43*delx, yA);
            g.drawString("+", xx1+57*delx, yA);
            g.drawString("+", xx1+71*delx, yA);
            g.drawString("+", xx1+85*delx, yA);
            
            //g.setColor(new Color(255-(int)(Math.sin(wt)*255),255-(int)(Math.sin(wt)*255),255));
            g.setColor(Color.blue);
            g.setFont(TitleFont2);
            g.drawString("-", xx1+15*delx, yB);
            g.drawString("-", xx1+29*delx, yB);
            g.drawString("-", xx1+43*delx, yB);
            g.drawString("-", xx1+57*delx, yB);
            g.drawString("-", xx1+71*delx, yB);
            g.drawString("-", xx1+85*delx, yB);
            
        }
        else if(Math.sin(wt) < 0){
            g2d.setComposite(AlphaComposite.getInstance(rule, (float)(Math.abs(Math.sin(wt)))));
            //g.setColor(new Color(255+(int)(Math.sin(wt)*255),255+(int)(Math.sin(wt)*255),255));
            g.setColor(Color.blue);
            g.setFont(TitleFont2);
            g.drawString("-", xx1+15*delx, yA);
            g.drawString("-", xx1+29*delx, yA);
            g.drawString("-", xx1+43*delx, yA);
            g.drawString("-", xx1+57*delx, yA);
            g.drawString("-", xx1+71*delx, yA);
            g.drawString("-", xx1+85*delx, yA);
            
            //g.setColor(new Color(255,255+(int)(Math.sin(wt)*255),255+(int)(Math.sin(wt)*255)));
            g.setColor(Color.red);
            g.setFont(TitleFont);
            g.drawString("+", xx1+15*delx, yB);
            g.drawString("+", xx1+29*delx, yB);
            g.drawString("+", xx1+43*delx, yB);
            g.drawString("+", xx1+57*delx, yB);
            g.drawString("+", xx1+71*delx, yB);
            g.drawString("+", xx1+85*delx, yB);
            
        }
        g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
        //==============================================================
                // surface cross-section
                g.setColor(Color.blue.darker());
                int newzpos1 = state.s60;
                int myX2 = plate2_y + 220*(plate2_y-plate1_y)/300;
                double shiftup = 100*(plate2_y-plate1_y)/100-state.s1;
                
                MaestroG.subscripterSansItalic3("I","1c","",g,state.font22,(int)(myX2-shiftup+state.s40),newzpos1-state.s25);
                if(SliceIt){
                    g.setColor(Color.red.darker());
                    MaestroG.subscripterSansItalic3("S","1","",g,state.font22,(int)(myX2-shiftup),newzpos1+state.s120);
                }
                newzpos1 = getSize().height - state.s60;
                g.setColor(Color.blue.darker());
                MaestroG.subscripterSansItalic3("I","3c","",g,state.font22,(int)(myX2-shiftup+state.s40),newzpos1+state.s30);
                if(SliceIt){
                    g.setColor(Color.red.darker());
                    MaestroG.subscripterSansItalic3("S","3","",g,state.font22,(int)(myX2-shiftup),newzpos1-state.s120);
                }
                
                g.setColor(Color.blue.darker());
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                MaestroG.subsubsubSansItalic3("I","1c"," = I","3c"," = I","2d",g,state.font22,(int)(xright),newzpos1+state.s30);
                //==============================================================
                
    }
    
    
    private synchronized void drawCircuit2(Graphics g){
        
	int ytop = 20*getSize().height/100;
        int ybottom = 80*getSize().height/100;
        
        int xleft = getSize().width/8;
        int xright = 6*getSize().width/8;
        
        int plate1_y = ytop+25*(ybottom-ytop)/100 - state.s10;
        int plate2_y = ytop+75*(ybottom-ytop)/100 + state.s10;
        int xsize = 2*getSize().width/3;
        int ysize = getSize().height/10;
        
        Graphics2D g2d = (Graphics2D)g;
        
        g.setColor(new Color(200,200,200));
        int rule; 
        float alphaG = 0.5f;
        rule = AlphaComposite.SRC_OVER;
        
        int xx1 = xright-xsize/4;
        int xx2 = xright+xsize/4;
        int yy1 = plate1_y-ysize/2;
        int yy2 = plate1_y+ysize/2;
        int yy3 = plate2_y-ysize/2;
        int yy4 = plate2_y+ysize/2;
        
        //----------------------------------------------------------------------
        
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        //----------------------------------------------------------------------
        
        g.setColor(new Color(200,200,225));
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        if(SliceIt){
            g.setColor(new Color(220,220,220));
            g2d.setComposite(AlphaComposite.getInstance(rule, 0.85f));

            Polygon Slice = new Polygon();

                        Slice.addPoint(xx1,yy2+(yy4-yy2)/2);
                        Slice.addPoint(xx1+xsize/16,((yy2+(yy4-yy2)/2)+(yy1+(yy3-yy1)/2))/2);
                        Slice.addPoint(xx2-xsize/16,((yy2+(yy4-yy2)/2)+(yy1+(yy3-yy1)/2))/2);
                        Slice.addPoint(xx2-xsize/8,yy2+(yy4-yy2)/2);
                        g.drawPolygon(Slice);
                        g.fillPolygon(Slice);
            g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
            if(SliceIt){
                drawLineThickDashed(g,(double)xx1,yy2+(yy4-yy2)/2,xx1+xsize/8,yy1+(yy3-yy1)/2,1,Color.gray);
                drawLineThickDashed(g,xx2,yy1+(yy3-yy1)/2,xx1+xsize/8,yy1+(yy3-yy1)/2,1,Color.gray);
            }
            //drawLineThickDashed(g,(double)xx1,yy2+(yy4-yy2)/2,xx2-xsize/8,yy2+(yy4-yy2)/2,1,Color.black);
            //drawLineThickDashed(g,(double)xx1,yy2+(yy4-yy2)/2,xx1+xsize/8,yy1+(yy3-yy1)/2,1,Color.black);
            //drawLineThickDashed(g,xx2,yy1+(yy3-yy1)/2,xx1+xsize/8,yy1+(yy3-yy1)/2,1,Color.black);
            //drawLineThickDashed(g,xx2,yy1+(yy3-yy1)/2,xx2-xsize/8,yy2+(yy4-yy2)/2,1,Color.black);
        }
        
        // vertical right side
        g2d.setComposite(AlphaComposite.getInstance(rule, 0.75f));            
        g.setColor(new Color(200,200,225));
        Polygon face2 = new Polygon();
                    face2.addPoint(xx2-xsize/8,yy2);
                    face2.addPoint(xx2,yy1);
                    face2.addPoint(xx2,yy3);
                    face2.addPoint(xx2-xsize/8,yy4);
                    g.drawPolygon(face2);
                    g.fillPolygon(face2);
                    g.setColor(Color.black);
                    g.drawPolygon(face2);
                    
        g2d.setComposite(AlphaComposite.getInstance(rule, 0.8f));            
        
        g.setColor(new Color(150,100,40));
        Polygon pla2 = new Polygon();
                    pla2.addPoint(xx2-xsize/8,yy4-state.s5);
                    pla2.addPoint(xx2,yy3-state.s5);
                    pla2.addPoint(xx2,yy3);
                    pla2.addPoint(xx2-xsize/8,yy4);
                    g.drawPolygon(pla2);
                    g.fillPolygon(pla2);
        g2d.setComposite(AlphaComposite.getInstance(rule, 0.8f));
        if(SliceIt){
            drawLineThickDashed(g,xx2,yy1+(yy3-yy1)/2,xx2-xsize/8,yy2+(yy4-yy2)/2,1,Color.black);
            drawLineThickDashed(g,(double)xx1,yy2+(yy4-yy2)/2,xx2-xsize/8,yy2+(yy4-yy2)/2,1,Color.black);
        }
        //----------------------------------------------------------------------
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        // permittivity symbol
        g.setColor(Color.black);
        MaestroG.subscripterSansItalic3("\u03b5","r","",g,state.font24,xx2-state.s30,yy1+(plate2_y-plate1_y)/2-state.s20);
        
        g2d.setComposite(AlphaComposite.getInstance(rule, alphaG));
        
        // Bottom plate
        g.setColor(new Color(190,140,80));
        Polygon plate2 = new Polygon();
                    plate2.addPoint(xx1,yy4);
                    plate2.addPoint(xx1+xsize/8,yy3);
                    plate2.addPoint(xx2,yy3);
                    plate2.addPoint(xx2-xsize/8,yy4);
                    //g.drawPolygon(plate2);
                    //g.fillPolygon(plate2);
        g.setColor(Color.black);
                    //g.drawPolygon(plate2);
        
        
        g2d.setComposite(AlphaComposite.getInstance(rule,1.0f));
        
        // left inside edge of top plate
        g.setColor(new Color(80,60,20));
        Polygon pla3 = new Polygon();
                    pla3.addPoint(xx1,yy2);
                    pla3.addPoint(xx1+xsize/8,yy1);
                    pla3.addPoint(xx1+xsize/8,yy1+state.s5);
                    pla3.addPoint(xx1,yy2+state.s5);
                    //g.drawPolygon(pla3);
                    //g.fillPolygon(pla3);
                             
        Polygon pla4 = new Polygon();
                    pla4.addPoint(xx1,yy4-state.s5);
                    pla4.addPoint(xx1+xsize/8,yy3-state.s5);
                    pla4.addPoint(xx1+xsize/8,yy3);
                    pla4.addPoint(xx1,yy4);
                    //g.drawPolygon(pla4);
                    //g.fillPolygon(pla4);
        
        g2d.setComposite(AlphaComposite.getInstance(rule, 0.65f));
        g.setColor(new Color(200,150,90));
        Polygon plate1 = new Polygon();
                    plate1.addPoint(xx1,yy2);
                    plate1.addPoint(xx1+xsize/8,yy1);
                    plate1.addPoint(xx2,yy1);
                    plate1.addPoint(xx2-xsize/8,yy2);
                    g.drawPolygon(plate1);
                    g.fillPolygon(plate1);
                    g.setColor(Color.black);
                    g.drawPolygon(plate1);
        
        g2d.setComposite(AlphaComposite.getInstance(rule, 0.6f));            
        Polygon face1 = new Polygon();
                    face1.addPoint(xx1,yy2);
                    face1.addPoint(xx2-xsize/8,yy2);
                    face1.addPoint(xx2-xsize/8,yy4);
                    face1.addPoint(xx1,yy4);
                    g.setColor(Color.black);
                    g.drawPolygon(face1);
        
        g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));            
                    
        //Thickness of capacitor plates (visible edges)            
        g.setColor(new Color(220,170,110));
        g.fillRect(xx1,yy2+1,38*xsize/100-state.s1,state.s5);
        
        g.setColor(new Color(150,100,40));
        Polygon pla1 = new Polygon();
                    pla1.addPoint(xx2-xsize/8,yy2);
                    pla1.addPoint(xx2,yy1);
                    pla1.addPoint(xx2,yy1+state.s5);
                    pla1.addPoint(xx2-xsize/8,yy2+state.s5);
                    g.drawPolygon(pla1);
                    g.fillPolygon(pla1);
                             
        double VRadius = (double)state.s35; //for the voltage generator
        double VCentery = ytop + (ybottom - ytop)/2; // vertical position of voltage generator 
        
        // wire connecting to top plate
        drawLineThick(g,(double)xright,(double)ytop,(double)xright,(double)plate1_y,3,Color.black);
        drawCircleThick(g, xright, plate1_y, state.s3, 6, Color.black);
    }
    //==========================================================================
    
    public synchronized void ignition(){
	Orth_Axis_Front = (int)(0.55*getSize().height/2);
	Orth_Axis_Back  = (int)(0.55*getSize().height/2);
         
        VPos = getSize().height - BottomMargin;
	WIDTH = getSize().width - LeftMargin -RightMargin;
	HEIGHT = VPos - TopMargin;
		
	N = z.length;
	//Find the boundaries for data
	if(!IsXRangeMaxSet){ xmax = MaestroA.getMax(EField_z);}
	if(!IsXRangeMinSet){ xmin = MaestroA.getMin(EField_z);}
		
    	if(!IsYRangeMaxSet){ ymax = MaestroA.getMax(HField_z);}
	if(!IsYRangeMinSet){ ymin = MaestroA.getMin(HField_z);}
	
        if(!IsZRangeMaxSet){ zmax = z[z.length-1];}
	if(!IsZRangeMinSet){ zmin = z[0]; }
	
	//Confine data
	MaestroA.confiner(z,zmax,zmin);
	MaestroA.confiner(HField_z,ymax,ymin);
	MaestroA.confiner(EField_z,xmax,xmin);
        MaestroA.confiner(PField_z,pmax,pmin);
	int myY;
	
        for(int i = 0; i < zx.length; i++){
		zx[i] = (int)MaestroA.mapper(z[i],(double)(getSize().width-RightMargin),(double)LeftMargin,zmax,zmin);
		Field_zx[i] = (int)MaestroA.mapper(EField_z[i],(double)TopMargin,(double)(getSize().height-BottomMargin),xmax,xmin);
		Field_zp[i] = (int)MaestroA.mapper(PField_z[i],(double)TopMargin2,
                              (double)(getSize().height-BottomMargin2),pmax,pmin);
                
		zy[i] = zx[i] + (int)MaestroA.mapper(costhetanew*HField_z[i],Orth_Axis_Front,-Orth_Axis_Back,ymax,ymin);	
    		Field_zy[i] = (int)MaestroA.mapper(sinthetanew*HField_z[i],(double)TopMargin,(double)getSize().height-BottomMargin,ymin,ymax);
        } 	
        
    }
    
    private synchronized void drawRotor(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
        
	int myX, myZ;
	int current_z;
	int yref = TopMargin+(getSize().height-BottomMargin-TopMargin)/2;
        
        g.setColor(rotorcolor);
	for(int i = 0; i< 1 ; i++){ // i<1 only shows one thick line ; i<2 also shows thick line at end of domain
            current_z = (int)(zpos[i]*(zx.length)/(zmax-zmin));
            if(current_z>=zx.length){current_z = zx.length-1;}
            if(current_z<0){current_z=0;}

                myX = (int)MaestroA.mapper(0,(double)TopMargin,(double)VPos,xmax,xmin);
                myZ = (int)MaestroA.mapper(z[current_z],(double)(getSize().width-RightMargin),(double)LeftMargin,zmax,zmin);
                if(IsEon){// electric field
                    if(Field_zx[current_z] < yref){
                        drawLineThick(g,zx[current_z],Field_zx[current_z]+1,myZ,myX,3,Color.red.brighter());
                    }
                    else{
                        drawLineThick(g,zx[current_z],Field_zx[current_z]-1,myZ,myX,3,Color.red.brighter());
                    }
                }
                
                int axis2 = state.s100;
                // cursor line
                float[] dashPattern2 = {5,5};
                g2d.setStroke(new BasicStroke(1.0F,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,10.0F,dashPattern2,0));
                if(i==0){
                    g.setColor(Color.lightGray);
                   
                    if(refz){
                        g.drawLine(myZ-state.s1,state.s60,myZ-state.s1,yref + axis2); 
                        g.drawLine(myZ-state.s1,yref + axis2 + state.s35,myZ-state.s1,getSize().height-state.s55); 
                    }
                }
                g2d.setStroke(new BasicStroke(1.0F,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER));
        }
    }
    
    private synchronized void drawPoints(Graphics g, int tipo){
        Graphics2D g2d = (Graphics2D)g;
          
	switch(tipo){
	    case 1:
		{
		   g.setColor(Color.black);
		   g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
                      //g.drawPolyline(zx,Field_zx,zx.length);
                        for(int i = 0; i < zx.length-1; i++){
			    //g.drawLine(zx[i],Field_zx[i],zx[i+1],Field_zx[i+1]);
                            drawLineThick(g,(double)zx[i],(double)Field_zx[i],(double)zx[i+1],(double)Field_zx[i+1],2,Color.black);
                        }
                    drawRefPoints(g);
                    g.setColor(Color.black);
		}
		break;
	    case 2:
		g.setColor(bgcolor);
                if(IsEon){g.drawPolyline(zx_old,Field_zx_old,zx_old.length);} // draw electri field
                if(IsHon){g.drawPolyline(zy_old,Field_zy_old,zy_old.length);} // draw magnetic field
		g.drawPolyline(zx_old,Field_zp_old,zx_old.length); // draw power
                break;
	}
    }
    
    private synchronized void drawRefPoints(Graphics g){
	Graphics2D g2d = (Graphics2D)g;
        int rule; 
        float alphaG = 0.7f;
        rule = AlphaComposite.SRC_OVER;
        g2d.setComposite(AlphaComposite.getInstance(rule, alphaG));
        
        int myX2;
        int Len = state.s20;
        int ytop = 0;
        int ybottom = 0;
        //int ytop = 20*getSize().height/100;
        //int ybottom = 80*getSize().height/100;
        //int plate1_y = ytop+25*(ybottom-ytop)/100;
        //int plate2_y = ytop+75*(ybottom-ytop)/100;
        
        if(state.sfactor1 >= 950){
            ytop = 13*getSize().height/100;
            ybottom = 87*getSize().height/100;
        }
        else if(state.sfactor1 < 950 && state.sfactor1 > 700){
            ytop = 14*getSize().height/100;
            ybottom = 86*getSize().height/100;
        }
        else if(state.sfactor1 <= 700){
            ytop = 15*getSize().height/100;
            ybottom = 85*getSize().height/100;
        }
        
        int plate1_y = ytop+(ybottom-ytop)/3;
        int plate2_y = ytop+2*(ybottom-ytop)/3;
        
        //int ytop = getSize().height/6;
        //int ybottom = 5*getSize().height/6;
        //int plate1_y = ytop+(ybottom-ytop)/3;
        //int plate2_y = ytop+2*(ybottom-ytop)/3;
        
        int xleft = getSize().width/8;
        int xright = 6*getSize().width/8;
        int xsize = 2*getSize().width/8;
        int ysize = getSize().height/12;
        
        int xx1 = xright-xsize/4;
        int xx2 = xright+xsize/4;
        int yy1 = plate1_y-ysize/2;
        int yy2 = plate1_y+ysize/2;
        int yy3 = plate2_y-ysize/2;
        int yy4 = plate2_y+ysize/2;
        
        myX2 = plate2_y + 220*(plate2_y-plate1_y)/300;
        
        int Narrows = 7;
        int zpos1 = xright - xsize/2;
        
        for(int i = 0; i < Narrows; i++){    
            g.setColor(Color.magenta.darker()); // this is for current
            zpos1 = xright - 60*xsize/100 + i*xsize/Narrows;
            
            int myE = 90;
            double checkE = Math.abs(Field_zp[myE]- myX2);
            double check2E = checkE/(double)Len;
            double check = Math.abs(Field_zp[0]- myX2);
            double check2 = check/(double)Len;
            int mythick = 2;
            
            double Ax = 3.0;
            double Ay = 6.0;
            int tx = (int)(Ax*check2);
            int ty = (int)(Ay*check2);
            
            int txE = (int)(Ax*check2E);
            int tyE = (int)(Ay*check2E);
            
            double shiftup = 100*(plate2_y-plate1_y)/100-state.s1;
            
            Color mycolor = Color.red;
            Color mycolor2 = Color.blue;
            
            int tune1, tune2;
            tune1 = xsize/8+state.s11;
            tune2 = (yy4-yy2)/4-state.s2;
            
            // ELECTRIC FIELD VECTOR LINES IN CAPACITOR
            if(i != 3){
                g2d.setComposite(AlphaComposite.getInstance(rule, alphaG));
                if(Field_zp[myE] < myX2){
                    if(checkE > Len){
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)zpos1-Ax+tune1,(double)Field_zp[myE]+Ay-shiftup-tune2,mythick,mycolor);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)zpos1+Ax+tune1,(double)Field_zp[myE]+Ay-shiftup-tune2,mythick,mycolor);
                    }
                    else{
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)(zpos1-txE)+tune1,(double)(Field_zp[myE]+tyE)-shiftup-tune2,mythick,mycolor);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)(zpos1+txE)+tune1,(double)(Field_zp[myE]+tyE)-shiftup-tune2,mythick,mycolor);
                    }
                }
                else{
                    if(checkE > Len){
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)zpos1-Ax+tune1,(double)Field_zp[myE]-Ay-shiftup-tune2,mythick,mycolor);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)zpos1+Ax+tune1,(double)Field_zp[myE]-Ay-shiftup-tune2,mythick,mycolor);
                    }
                    else{
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)(zpos1-txE)+tune1,(double)(Field_zp[myE]-tyE)-shiftup-tune2,mythick,mycolor);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)(zpos1+txE)+tune1,(double)(Field_zp[myE]-tyE)-shiftup-tune2,mythick,mycolor);
                    }
                }
                drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)zpos1+tune1,myX2-shiftup-((double)Field_zp[myE]-myX2)-1-tune2,mythick,mycolor);
            }
            else{ // DISPLACEMENT CURRENT VECTOR IN CAPACITOR
                g2d.setComposite(AlphaComposite.getInstance(rule, alphaG));
                if(Field_zp[0] < myX2){
                    if(check > Len){
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)zpos1-2*Ax+tune1,(double)Field_zp[0]+2*Ay-shiftup-tune2,4,mycolor2);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)zpos1+2*Ax+tune1,(double)Field_zp[0]+2*Ay-shiftup-tune2,4,mycolor2);
                    }
                    else{
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)(zpos1-2*tx)+tune1,(double)(Field_zp[0]+2*ty)-shiftup-tune2,4,mycolor2);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)(zpos1+2*tx)+tune1,(double)(Field_zp[0]+2*ty)-shiftup-tune2,4,mycolor2);
                    }
                }
                else{
                    if(check > Len){
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)zpos1-2*Ax+tune1,(double)Field_zp[0]-2*Ay-shiftup-tune2,4,mycolor2);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)zpos1+2*Ax+tune1,(double)Field_zp[0]-2*Ay-shiftup-tune2,4,mycolor2);
                    }
                    else{
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)(zpos1-2*tx)+tune1,(double)(Field_zp[0]-2*ty)-shiftup-tune2,4,mycolor2);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)(zpos1+2*tx)+tune1,(double)(Field_zp[0]-2*ty)-shiftup-tune2,4,mycolor2);
                    }
                }
                drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)zpos1+tune1,myX2-shiftup-((double)Field_zp[0]-myX2)-1-tune2,4,mycolor2);
                
            }
            
            g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
            
            //  CURRENT DIRECT PATH ON TOP WIRE
            int newzpos1 = state.s60;
                
                if(Field_zp[0] < myX2){
                    if(check > Len){
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)Field_zp[0]+2*Ay-shiftup,(double)newzpos1-2*Ax,4,mycolor2);
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)Field_zp[0]+2*Ay-shiftup,(double)newzpos1+2*Ax,4,mycolor2);
                    }
                    else{
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)(Field_zp[0]+2*ty)-shiftup,(double)(newzpos1-2*tx),4,mycolor2);
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)(Field_zp[0]+2*ty)-shiftup,(double)(newzpos1+2*tx),4,mycolor2);
                    }
                }
                else{
                    if(check > Len){
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)Field_zp[0]-2*Ay-shiftup,(double)newzpos1-2*Ax,4,mycolor2);
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)Field_zp[0]-2*Ay-shiftup,(double)newzpos1+2*Ax,4,mycolor2);
                    }
                    else{
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)(Field_zp[0]-2*ty)-shiftup,(double)(newzpos1-2*tx),4,mycolor2);
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)(Field_zp[0]-2*ty)-shiftup,(double)(newzpos1+2*tx),4,mycolor2);
                    }
                }
                
                drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,myX2-shiftup-((double)Field_zp[0]-myX2)-1,(double)newzpos1,4,mycolor2);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                if(SliceIt){
                    drawLineThickDashed(g,myX2-shiftup,(double)newzpos1-(double)state.s40,myX2-shiftup,(double)newzpos1+(double)state.s90,1,Color.green.darker());
                    
                    g2d.setComposite(AlphaComposite.getInstance(rule, 0.75f));            
                    g.setColor(new Color(230,230,230));
                    Polygon surf1 = new Polygon();
                    surf1.addPoint((int)(myX2-shiftup-state.s20),(int)(newzpos1-state.s30));
                    surf1.addPoint((int)(myX2-shiftup+state.s20),(int)(newzpos1-state.s50));
                    surf1.addPoint((int)(myX2-shiftup+state.s20),(int)(newzpos1+state.s80));
                    surf1.addPoint((int)(myX2-shiftup-state.s20),(int)(newzpos1+state.s100));
                    g.drawPolygon(surf1);
                    g.fillPolygon(surf1);
                    g.setColor(Color.black);
                    g.drawPolygon(surf1);
                    // wire above surface
                    drawLineThick(g,myX2-shiftup,(double)(20*getSize().height/100),myX2-shiftup+state.s50,(double)(20*getSize().height/100),3,Color.black);
                    // curent above surface
                    g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
                    if((myX2-shiftup)>(myX2-shiftup-((double)Field_zp[0]-myX2)-1)){
                        drawLineThick(g,myX2-shiftup,(double)newzpos1,(double)Field_zp[0]-shiftup,(double)newzpos1,4,mycolor2);
                        if(check > Len){
                            drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)Field_zp[0]-2*Ay-shiftup,(double)newzpos1-2*Ax,4,mycolor2);
                            drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)Field_zp[0]-2*Ay-shiftup,(double)newzpos1+2*Ax,4,mycolor2);
                        }
                        else{
                            drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)(Field_zp[0]-2*ty)-shiftup,(double)(newzpos1-2*tx),4,mycolor2);
                            drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)(Field_zp[0]-2*ty)-shiftup,(double)(newzpos1+2*tx),4,mycolor2);
                        }   
                    }
                    else{
                        drawLineThick(g,myX2-shiftup,(double)newzpos1,myX2-shiftup-((double)Field_zp[0]-myX2)-1,(double)newzpos1,4,mycolor2);
                    }
                }
                 
                //  CURRENT RETURN PATH ON LOWER WIRE
                newzpos1 = getSize().height - state.s60;
                
                double shiftup2;
                int tune = 11; // to position arrowhead properly
                if(Field_zp[0] < myX2){
                    if(check > Len){
                        shiftup2 = shiftup+tune;
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-1,(double)newzpos1-2*Ax,4,mycolor2);
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-1,(double)newzpos1+2*Ax,4,mycolor2);
                    }
                    else{
                        shiftup2 = shiftup+tune;
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-1+2*(Ay-ty),(double)(newzpos1-2*tx),4,mycolor2);
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-1+2*(Ay-ty),(double)(newzpos1+2*tx),4,mycolor2);
                    }
                }
                else{
                    if(check > Len){
                        shiftup2 = shiftup-tune;
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-1-2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-1,(double)newzpos1-2*Ax,4,mycolor2);
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-1-2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-1,(double)newzpos1+2*Ax,4,mycolor2);
                    }
                    else{
                        shiftup2 = shiftup-tune;
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-1-2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-1-2*(Ay-ty),(double)(newzpos1-2*tx),4,mycolor2);
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-1-2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-1-2*(Ay-ty),(double)(newzpos1+2*tx),4,mycolor2);
                    }
                }
                
                drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,myX2-shiftup-((double)Field_zp[0]-myX2)-1,(double)newzpos1,4,mycolor2);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                // surface cross-section
                if(SliceIt){
                    drawLineThickDashed(g,myX2-shiftup,(double)newzpos1+(double)state.s40,myX2-shiftup,(double)newzpos1-(double)state.s90,1,Color.green.darker());
                    
                    //LOWER WIRE CURRENT
                    g2d.setComposite(AlphaComposite.getInstance(rule, 0.75f));            
                    g.setColor(new Color(230,230,230));
                    Polygon surf2 = new Polygon();
                    surf2.addPoint((int)(myX2-shiftup-state.s20),(int)(newzpos1+state.s50));
                    surf2.addPoint((int)(myX2-shiftup+state.s20),(int)(newzpos1+state.s30));
                    surf2.addPoint((int)(myX2-shiftup+state.s20),(int)(newzpos1-state.s100));
                    surf2.addPoint((int)(myX2-shiftup-state.s20),(int)(newzpos1-state.s80));
                    g.drawPolygon(surf2);
                    g.fillPolygon(surf2);
                    g.setColor(Color.black);
                    g.drawPolygon(surf2);
                    // wire above surface
                    drawLineThick(g,myX2-shiftup,(double)(80*getSize().height/100),myX2-shiftup+(double)state.s50,(double)(80*getSize().height/100),3,Color.black);
                    // curent above surface
                    g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
                    if((myX2-shiftup)>(myX2-shiftup-((double)Field_zp[0]-myX2)-1)){
                        drawLineThick(g,myX2-shiftup,(double)newzpos1,(double)Field_zp[0]-shiftup,(double)newzpos1,4,mycolor2);
                    }
                    else{
                        drawLineThick(g,myX2-shiftup,(double)newzpos1,myX2-shiftup-((double)Field_zp[0]-myX2)-1,(double)newzpos1,4,mycolor2);
                        if(check > Len){
                        shiftup2 = shiftup+tune;
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-1,(double)newzpos1-2*Ax,4,mycolor2);
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-1,(double)newzpos1+2*Ax,4,mycolor2);
                    }
                    else{
                        shiftup2 = shiftup+tune;
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-1+2*(Ay-ty),(double)(newzpos1-2*tx),4,mycolor2);
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-1+2*(Ay-ty),(double)(newzpos1+2*tx),4,mycolor2);
                    }
                    }
                }
                
        }
    }
    

    
    /*
    private synchronized void drawRefPoints(Graphics g){
	Graphics2D g2d = (Graphics2D)g;
        int rule; 
        float alphaG = 0.7f;
        rule = AlphaComposite.SRC_OVER;
        g2d.setComposite(AlphaComposite.getInstance(rule, alphaG));
        
        int myX2;
	
        int Len = state.s20;
        int ytop = getSize().height/6;
        int ybottom = 5*getSize().height/6;
        
        int xleft = getSize().width/8;
        int xright = 6*getSize().width/8;
        int plate1_y = ytop+(ybottom-ytop)/3;
        int plate2_y = ytop+2*(ybottom-ytop)/3;
        int xsize = 2*getSize().width/8;
        int ysize = getSize().height/10;
        
        int xx1 = xright-xsize/4;
        int xx2 = xright+xsize/4;
        int yy1 = plate1_y-ysize/2;
        int yy2 = plate1_y+ysize/2;
        int yy3 = plate2_y-ysize/2;
        int yy4 = plate2_y+ysize/2;
        
        myX2 = plate2_y + 220*(plate2_y-plate1_y)/300;
        
        int Narrows = 7;
        int zpos1 = xright - xsize/2;
        
        for(int i = 0; i < Narrows; i++){    
            g.setColor(Color.magenta.darker()); // this is for current
            zpos1 = xright - 60*xsize/100 + i*xsize/Narrows;
            
            int myE = 90;
            double checkE = Math.abs(Field_zp[myE]- myX2);
            double check2E = checkE/(double)Len;
            double check = Math.abs(Field_zp[0]- myX2);
            double check2 = check/(double)Len;
            int mythick = state.s2;
            
            double Ax = 3.0;
            double Ay = 6.0;
            int tx = (int)(Ax*check2);
            int ty = (int)(Ay*check2);
            
            int txE = (int)(Ax*check2E);
            int tyE = (int)(Ay*check2E);
            
            double shiftup = 100*(plate2_y-plate1_y)/100-state.s1;
            
            Color mycolor = Color.red;
            Color mycolor2 = Color.blue;
            
            int tune1, tune2;
            tune1 = xsize/8 + 11;
            tune2 = (yy4-yy2)/4 - 2;
            
            // ELECTRIC FIELD VECTOR LINES IN CAPACITOR
            if(i != 3){
                g2d.setComposite(AlphaComposite.getInstance(rule, alphaG));
                if(Field_zp[myE] < myX2){
                    if(checkE > Len){
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)zpos1-Ax+tune1,(double)Field_zp[myE]+Ay-shiftup-tune2,mythick,mycolor);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)zpos1+Ax+tune1,(double)Field_zp[myE]+Ay-shiftup-tune2,mythick,mycolor);
                    }
                    else{
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)(zpos1-txE)+tune1,(double)(Field_zp[myE]+tyE)-shiftup-tune2,mythick,mycolor);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)(zpos1+txE)+tune1,(double)(Field_zp[myE]+tyE)-shiftup-tune2,mythick,mycolor);
                    }
                }
                else{
                    if(checkE > Len){
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)zpos1-Ax+tune1,(double)Field_zp[myE]-Ay-shiftup-tune2,mythick,mycolor);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)zpos1+Ax+tune1,(double)Field_zp[myE]-Ay-shiftup-tune2,mythick,mycolor);
                    }
                    else{
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)(zpos1-txE)+tune1,(double)(Field_zp[myE]-tyE)-shiftup-tune2,mythick,mycolor);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)(zpos1+txE)+tune1,(double)(Field_zp[myE]-tyE)-shiftup-tune2,mythick,mycolor);
                    }
                }
                drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[myE]-shiftup-tune2,(double)zpos1+tune1,myX2-shiftup-((double)Field_zp[myE]-myX2)-state.s1-tune2,mythick,mycolor);
            }
            else{ // DISPLACEMENT CURRENT VECTOR IN CAPACITOR
                g2d.setComposite(AlphaComposite.getInstance(rule, alphaG));
                if(Field_zp[0] < myX2){
                    if(check > Len){
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)zpos1-2*Ax+tune1,(double)Field_zp[0]+2*Ay-shiftup-tune2,4,mycolor2);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)zpos1+2*Ax+tune1,(double)Field_zp[0]+2*Ay-shiftup-tune2,4,mycolor2);
                    }
                    else{
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)(zpos1-2*tx)+tune1,(double)(Field_zp[0]+2*ty)-shiftup-tune2,4,mycolor2);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)(zpos1+2*tx)+tune1,(double)(Field_zp[0]+2*ty)-shiftup-tune2,4,mycolor2);
                    }
                }
                else{
                    if(check > Len){
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)zpos1-2*Ax+tune1,(double)Field_zp[0]-2*Ay-shiftup-tune2,4,mycolor2);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)zpos1+2*Ax+tune1,(double)Field_zp[0]-2*Ay-shiftup-tune2,4,mycolor2);
                    }
                    else{
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)(zpos1-2*tx)+tune1,(double)(Field_zp[0]-2*ty)-shiftup-tune2,4,mycolor2);
                        drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)(zpos1+2*tx)+tune1,(double)(Field_zp[0]-2*ty)-shiftup-tune2,4,mycolor2);
                    }
                }
                drawLineThick(g,(double)zpos1+tune1,(double)Field_zp[0]-shiftup-tune2,(double)zpos1+tune1,myX2-shiftup-((double)Field_zp[0]-myX2)-1-tune2,4,mycolor2);    
            }
            
            g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
            
            //  CURRENT DIRECT PATH ON TOP WIRE
            int newzpos1 = state.s60;
                
                if(Field_zp[0] < myX2){
                    if(check > Len){
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)Field_zp[0]+2*Ay-shiftup,(double)newzpos1-2*Ax,4,mycolor2);
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)Field_zp[0]+2*Ay-shiftup,(double)newzpos1+2*Ax,4,mycolor2);
                    }
                    else{
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)(Field_zp[0]+2*ty)-shiftup,(double)(newzpos1-2*tx),4,mycolor2);
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)(Field_zp[0]+2*ty)-shiftup,(double)(newzpos1+2*tx),4,mycolor2);
                    }
                }
                else{
                    if(check > Len){
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)Field_zp[0]-2*Ay-shiftup,(double)newzpos1-2*Ax,4,mycolor2);
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)Field_zp[0]-2*Ay-shiftup,(double)newzpos1+2*Ax,4,mycolor2);
                    }
                    else{
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)(Field_zp[0]-2*ty)-shiftup,(double)(newzpos1-2*tx),4,mycolor2);
                        drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)(Field_zp[0]-2*ty)-shiftup,(double)(newzpos1+2*tx),4,mycolor2);
                    }
                }
                
                drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,myX2-shiftup-((double)Field_zp[0]-myX2)-state.s1,(double)newzpos1,4,mycolor2);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                if(SliceIt){
                    drawLineThickDashed(g,myX2-shiftup,(double)newzpos1-(double)state.s40,myX2-shiftup,(double)newzpos1+(double)state.s90,1,Color.green.darker());
                    
                    g2d.setComposite(AlphaComposite.getInstance(rule, 0.75f));            
                    g.setColor(new Color(230,230,230));
                    Polygon surf1 = new Polygon();
                    surf1.addPoint((int)(myX2-shiftup-state.s20),(int)(newzpos1-(double)state.s30));
                    surf1.addPoint((int)(myX2-shiftup+state.s20),(int)(newzpos1-(double)state.s50));
                    surf1.addPoint((int)(myX2-shiftup+state.s20),(int)(newzpos1+(double)state.s80));
                    surf1.addPoint((int)(myX2-shiftup-state.s20),(int)(newzpos1+(double)state.s100));
                    g.drawPolygon(surf1);
                    g.fillPolygon(surf1);
                    g.setColor(Color.black);
                    g.drawPolygon(surf1);
                    // wire above surface
                    drawLineThick(g,myX2-shiftup,(double)(20*getSize().height/100),myX2-shiftup+state.s50,(double)(20*getSize().height/100),3,Color.black);
                    // curent above surface
                    g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
                    if((myX2-shiftup)>(myX2-shiftup-((double)Field_zp[0]-myX2)-state.s1)){
                        drawLineThick(g,myX2-shiftup,(double)newzpos1,(double)Field_zp[0]-shiftup,(double)newzpos1,4,mycolor2);
                        if(check > Len){
                            drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)Field_zp[0]-2*Ay-shiftup,(double)newzpos1-2*Ax,4,mycolor2);
                            drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)Field_zp[0]-2*Ay-shiftup,(double)newzpos1+2*Ax,4,mycolor2);
                        }
                        else{
                            drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)(Field_zp[0]-2*ty)-shiftup,(double)(newzpos1-2*tx),4,mycolor2);
                            drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,(double)(Field_zp[0]-2*ty)-shiftup,(double)(newzpos1+2*tx),4,mycolor2);
                        }   
                    }
                    else{
                        drawLineThick(g,myX2-shiftup,(double)newzpos1,myX2-shiftup-((double)Field_zp[0]-myX2)-state.s1,(double)newzpos1,4,mycolor2);
                    }
                }
                 
                //  CURRENT RETURN PATH ON LOWER WIRE
                newzpos1 = getSize().height - state.s60;
                
                double shiftup2;
                int tune = state.s11; // to position arrowhead properly
                if(Field_zp[0] < myX2){
                    if(check > Len){
                        shiftup2 = shiftup+tune;
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1,(double)newzpos1-2*Ax,4,mycolor2);
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1,(double)newzpos1+2*Ax,4,mycolor2);
                    }
                    else{
                        shiftup2 = shiftup+tune;
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1+2*(Ay-ty),(double)(newzpos1-2*tx),4,mycolor2);
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1+2*(Ay-ty),(double)(newzpos1+2*tx),4,mycolor2);
                    }
                }
                else{
                    if(check > Len){
                        shiftup2 = shiftup-tune;
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1-2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1,(double)newzpos1-2*Ax,4,mycolor2);
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1-2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1,(double)newzpos1+2*Ax,4,mycolor2);
                    }
                    else{
                        shiftup2 = shiftup-tune;
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1-2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1-2*(Ay-ty),(double)(newzpos1-2*tx),4,mycolor2);
                        drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1-2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1-2*(Ay-ty),(double)(newzpos1+2*tx),4,mycolor2);
                    }
                }
                
                drawLineThick(g,(double)Field_zp[0]-shiftup,(double)newzpos1,myX2-shiftup-((double)Field_zp[0]-myX2)-state.s1,(double)newzpos1,4,mycolor2);
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
                
                // surface cross-section
                if(SliceIt){
                    drawLineThickDashed(g,myX2-shiftup,(double)(newzpos1+state.s40),myX2-shiftup,(double)(newzpos1-state.s90),1,Color.green.darker());
                    
                    //LOWER WIRE CURRENT
                    g2d.setComposite(AlphaComposite.getInstance(rule, 0.75f));            
                    g.setColor(new Color(230,230,230));
                    Polygon surf2 = new Polygon();
                    surf2.addPoint((int)(myX2-shiftup-state.s20),(int)(newzpos1+(double)state.s50));
                    surf2.addPoint((int)(myX2-shiftup+state.s20),(int)(newzpos1+(double)state.s30));
                    surf2.addPoint((int)(myX2-shiftup+state.s20),(int)(newzpos1-(double)state.s100));
                    surf2.addPoint((int)(myX2-shiftup-state.s20),(int)(newzpos1-(double)state.s80));
                    g.drawPolygon(surf2);
                    g.fillPolygon(surf2);
                    g.setColor(Color.black);
                    g.drawPolygon(surf2);
                    // wire above surface
                    drawLineThick(g,myX2-shiftup,(double)(80*getSize().height/100),myX2-shiftup+state.s50,(double)(80*getSize().height/100),3,Color.black);
                    // curent above surface
                    g2d.setComposite(AlphaComposite.getInstance(rule, 1.0f));
                    if((myX2-shiftup)>(myX2-shiftup-((double)Field_zp[0]-myX2)-1)){
                        drawLineThick(g,myX2-shiftup,(double)newzpos1,(double)Field_zp[0]-shiftup,(double)newzpos1,4,mycolor2);
                    }
                    else{
                        drawLineThick(g,myX2-shiftup,(double)newzpos1,myX2-shiftup-((double)Field_zp[0]-myX2)-state.s1,(double)newzpos1,4,mycolor2);
                        if(check > Len){
                            shiftup2 = shiftup+tune;
                            drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1,(double)newzpos1-2*Ax,4,mycolor2);
                            drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1,(double)newzpos1+2*Ax,4,mycolor2);
                        }
                        else{
                            shiftup2 = shiftup+tune;
                            drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1+2*(Ay-ty),(double)(newzpos1-2*tx),4,mycolor2);
                            drawLineThick(g,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1+2*Ay,(double)newzpos1,myX2-shiftup2-((double)Field_zp[0]-myX2)-state.s1+2*(Ay-ty),(double)(newzpos1+2*tx),4,mycolor2);
                        }
                    }
                }
        }
    }
    */
    
    
    private void drawLineThick(Graphics g, double x1, double y1, double x2, double y2, int thick, Color color){
	
        Graphics2D g2d = (Graphics2D)g;
        g2d.setPaint(color);
        g2d.setStroke(new BasicStroke(thick,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
        
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        Line2D.Double line = new Line2D.Double(x1,y1,x2,y2);
        g2d.draw(line);
  
        g2d.setStroke(new BasicStroke(1));
        //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    }
    
    public static void drawLineThickDashed(Graphics g, double x1, double y1, double x2, double y2, int thick, Color color){
	
        Graphics2D g2d = (Graphics2D)g;
        g2d.setPaint(color);
        float[] dashPattern = {10,10};// Add these lines to make line dotted
        g2d.setStroke(new BasicStroke(thick,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,10.0F,dashPattern,0));
        
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        Line2D.Double line = new Line2D.Double(x1,y1,x2,y2);
        g2d.draw(line);
  
        g2d.setStroke(new BasicStroke(1));
        //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    }
    

    private void labelDetect(){
	X1 = "E (x, t)";
	X2 = "H (y, t)";
    }
    
    private void drawLabels(Graphics g){
	String tmp;
	int yref = TopMargin+(getSize().height-BottomMargin-TopMargin)/2;
        
        g.setFont(LabelFont);
	FontMetrics fm = g.getFontMetrics();
	
        g.setColor(Color.black);
        Graphics2D g2d = (Graphics2D)g;
	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        // z axis legends
        tmp = Z1;
	MaestroG.subscripter(tmp,"","",g,13,LeftMargin-3*fm.stringWidth(tmp)/2,VPos+fm.getHeight()-5+down);
        tmp = Z2+" = "+total_length+" \u03bb";
	MaestroG.subscripterSansItalic6("h",""," = "+MaestroA.rounder(total_length,6)," \u03bb",g,13,getSize().width-RightMargin-fm.stringWidth(tmp)/3+2,VPos+fm.getHeight()-5+down);
	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
        g.setFont(LabelFont);
        g.setColor(Color.red);
        
        tmp="Ex( t )";
	if(IsEon){MaestroG.subscripter("E","x","(t)",g,13,LeftMargin-fm.stringWidth(tmp)-20,TopMargin+fm.getHeight());
        	  //MaestroG.subscripterSansItalic3("E","x","( t )",g,15,LeftMargin-fm.stringWidth(tmp)-20,TopMargin+fm.getHeight());
        }

        g.setColor(Color.blue);
	if(IsHon){MaestroG.subscripter("H","y","(t)",g,13,12,yref+10);
                //MaestroG.subscripterSansItalic3("H","y","( t )",g,15,12,yref+10);
        }
        
        g.setColor(Color.magenta.darker());
	MaestroG.subscripter("J","x","(z,t)",g,13,LeftMargin-fm.stringWidth(tmp)-20,Decay3[0]);
        //MaestroG.subscripterSansItalic3("J","x","(z,t)",g,15,LeftMargin-fm.stringWidth(tmp)-25,Decay3[0]);
        g.setColor(Color.black);
	MaestroG.subscripterSansItalic3("z","","",g,16,getSize().width-55,yref-5);
        MaestroG.subscripterSansItalic3("z","","",g,16,getSize().width-55,VPos+down2-10);
        
        
    }
    
    private void drawZeroLine(Graphics g){
	int myX;
	myX = (int)MaestroA.mapper(0,(double)TopMargin,(double)VPos,xmax,xmin);
	g.setColor(Color.red);
	g.drawLine(LeftMargin+1,myX,getSize().width-RightMargin-1,myX);
    }
    
    
    public synchronized void reset(){
	IsYRangeMaxSet = false;
	IsYRangeMinSet = false;
	IsXRangeMaxSet = false;
	IsXRangeMinSet = false;
        IsPRangeMaxSet = false;
	IsPRangeMinSet = false;
	IsZRangeMinSet = false;
	IsZRangeMaxSet = false;
	IsTraceOn = false;
	IsCleanUpOn = false;
    }
    
    public synchronized void cleanUp(){
	IsCleanUpOn=true;
    }
    
    public synchronized void setYRangeMax(double ymax){
	this.ymax = ymax;
	IsYRangeMaxSet = true;
    }
    
    public synchronized void setYRangeMin(double ymin){
	this.ymin = ymin;
	IsYRangeMinSet = true;
    }
    
    public synchronized void setZRangeMax(double zmax){
	this.zmax = zmax;
	IsZRangeMaxSet = true;
    }
    
    public synchronized void setZRangeMin(double zmin){
	this.zmin = zmin;
	IsZRangeMinSet = true;
    }
    
    public synchronized void setXRangeMax(double xmax){
	this.xmax = xmax;
	IsXRangeMaxSet = true;
    }
    
    public synchronized void setXRangeMin(double xmin){
	this.xmin = xmin;
	IsXRangeMinSet = true;
    }
    
    public synchronized void setTrace(boolean IsTraceOn){
	this.IsTraceOn = IsTraceOn;
	this.clean_memory();
    }
    
    public synchronized void setDynamics(boolean IsDynamic){
	this.IsDynamic = IsDynamic;
    }
    

    public void update(Graphics g){
	if(!IsDynamic){
	    paint(g);
	    return;
	}
	if(IsDynamic && IsTraceOn){g.clipRect(LeftMargin+1,TopMargin+1,WIDTH-1,HEIGHT-1);}
	if(IsCleanUpOn){
	    g.clearRect(0,0,getSize().width,getSize().height);
	    IsCleanUpOn = false;
	    //drawAxis(g);
	    //drawLabels(g);
            drawCircuit(g);
	}
	drawZeroLine(g);
	if(!IsTraceOn){
	    //drawPoints(g,2);//This cleans the previous graph
            drawCircuit(g);//This cleans the previous graph
	}
	//drawPoints(g,1);//This draws the new graph
	//drawAxis(g);
	//drawRotor(g);
	drawCircuit(g);
        
        push();
    }
    
    
    public synchronized void plot(double xdata[], double tEField_z[], double tHField_z[], double tPField_z[], double tau){
   
        
	if(N != xdata.length){
	    N = xdata.length;
	    z = new double[N];
	    EField_z = new double[N];
	    HField_z = new double[N];
	    PField_z = new double[N];
	    zx = new int[N];
	    zy = new int[N];
	}
	for(int i = 0; i < N; i++){
	    z[i] = xdata[i];
	    EField_z[i] = tEField_z[i] * E_ratio;
	    HField_z[i] = tHField_z[i] * H_ratio ;
	    PField_z[i] = tPField_z[i] * P_ratio ;
	}
	//ignition();
	
	N = z.length;
	//Find the boundaries for data
	if(!IsXRangeMaxSet){ xmax = MaestroA.getMax(EField_z);}
	if(!IsXRangeMinSet){ xmin = MaestroA.getMin(EField_z);}
		
    	if(!IsYRangeMaxSet){ ymax = MaestroA.getMax(HField_z);}
	if(!IsYRangeMinSet){ ymin = MaestroA.getMin(HField_z);}
	if(!IsPRangeMaxSet){ pmax = MaestroA.getMax(PField_z);}
	if(!IsPRangeMinSet){ pmin = MaestroA.getMin(PField_z);}
	
	if(!IsZRangeMaxSet){ zmax = z[z.length-1];}
	if(!IsZRangeMinSet){ zmin = z[0]; }
	
	//Confine data
	MaestroA.confiner(z,zmax,zmin);
	MaestroA.confiner(HField_z,ymax,ymin);
	MaestroA.confiner(EField_z,xmax,xmin);
        MaestroA.confiner(PField_z,pmax,pmin);
        
	int myY;
	double Deltax = total_length*wavelength/zx.length;
	double angle = 140.0;
	double costhetanew,sinthetanew;
        
        costhetanew = Math.cos(angle*Math.PI/180.0);    
	sinthetanew = Math.sin(angle*Math.PI/180.0);
        int yref = TopMargin+(getSize().height-BottomMargin-TopMargin)/2;
        
        //int ytop = 15*getSize().height/100;
        //int ybottom = 85*getSize().height/100;
        //int plate1_y = ytop+(ybottom-ytop)/3;
        //int plate2_y = ytop+2*(ybottom-ytop)/3;
        
        int ytop = getSize().height/6;
        int ybottom = 5*getSize().height/6;
        int plate1_y = ytop+(ybottom-ytop)/3;
        int plate2_y = ytop+2*(ybottom-ytop)/3;
        
        int xleft = getSize().width/8;
        int xright = 6*getSize().width/8;
        
        int xsize = 2*getSize().width/3;
        int ysize = getSize().height/10;    
        
        double VRadiusy = 15.0; //for the voltage generator
        double VRadiusx = 25.0; //for the voltage generator
        double VCentery = ytop + (ybottom - ytop)/2; // vertical position of voltage generator 
	
        for(int i = 0; i < zx.length; i++){
		double Factor1 = Ex*Math.exp(-alpha*Deltax*i);
		double Factor2 = Hy*Math.exp(-alpha*Deltax*i);
		double Factor3 = Ex*Math.exp(-alpha*Deltax*i);
                zx[i] = (int)MaestroA.mapper(z[i],(double)xleft+VRadiusx,(double)xleft-VRadiusx,zmax,zmin);
		//Field_zx[i] = (int)MaestroA.mapper(EField_z[i],(double)TopMargin,(double)(getSize().height-BottomMargin),xmax,xmin);
                Field_zx[i] = (int)MaestroA.mapper(EField_z[i],(double)VCentery-VRadiusy,(double)VCentery+VRadiusy,xmax,xmin);
                
		Decay1[i] = (int)MaestroA.mapper(Factor1*E_ratio,(double)TopMargin,(double)(getSize().height-BottomMargin),xmax,xmin);
		Decay1B[i] = yref + (yref - Decay1[i]);
                Field_zp[i] = (int)MaestroA.mapper(PField_z[i],(double)TopMargin2,(double)(getSize().height-BottomMargin2-80),pmax,pmin);
		Decay3[i] = (int)MaestroA.mapper(Factor3*E_ratio,(double)TopMargin2,(double)(getSize().height-BottomMargin2-80),pmax,pmin);
		
		zy[i] = zx[i] + (int)MaestroA.mapper(costhetanew*HField_z[i],Orth_Axis_Front,-Orth_Axis_Back,ymax,ymin);	
		
    		Field_zy[i] = (int)MaestroA.mapper(sinthetanew*HField_z[i],(double)TopMargin,(double)getSize().height-BottomMargin,ymin,ymax);
		zy2[i] = zx[i] + (int)MaestroA.mapper(costhetanew*Factor2*H_ratio,Orth_Axis_Front,-Orth_Axis_Back,ymax,ymin);	
		Decay2[i] = (int)MaestroA.mapper(sinthetanew*Factor2*H_ratio,(double)TopMargin,(double)getSize().height-BottomMargin,ymin,ymax);
                zy2B[i] = zx[i] - (int)MaestroA.mapper(costhetanew*Factor2*H_ratio,Orth_Axis_Front,-Orth_Axis_Back,ymax,ymin);	
		Decay2B[i] = (int)MaestroA.mapper(-sinthetanew*Factor2*H_ratio,(double)TopMargin,(double)getSize().height-BottomMargin,ymin,ymax);
	} 
        
	repaint();
    }
    
    private void drawCircleThick(Graphics g, double xCenter, double yCenter, double Radius, int thick, Color color){
	
        Graphics2D g2d = (Graphics2D)g;
        g2d.setPaint(color);
        g2d.setStroke(new BasicStroke(thick, BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL));
        
        //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        Ellipse2D.Double circle = new Ellipse2D.Double(xCenter-Radius,yCenter-Radius,2*Radius,2*Radius);
        g2d.draw(circle);
        g2d.setStroke(new BasicStroke(1));
        //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
}
    
    private synchronized void push(){
	if(zx.length != zx_old.length){
	    zx_old = new int[zx.length];
	    zy_old = new int[zx.length];
	    Field_zx_old = new int[zx.length];
	    Field_zy_old = new int[zx.length];
            Field_zp_old = new int[zx.length];
	}
    
	for(int i = 0; i < zx.length; i++){
	    zx_old[i] = zx[i];
	    zy_old[i] = zy[i];
	    Field_zx_old[i] = Field_zx[i];
	    Field_zy_old[i] = Field_zy[i];
            Field_zp_old[i] = Field_zp[i];
	}
    }
    
     private void clean_memory(){
	if(zx.length != zx_old.length){
	    zx_old = new int[zx.length];
	    zy_old = new int[zx.length];
	    Field_zx_old = new int[zx.length];
            Field_zy_old = new int[zx.length];
            Field_zp_old = new int[zx.length];
	}
    
	for(int i = 0; i < zx.length; i++){
	    zx_old[i] = zx[i];
	    zy_old[i] = zy[i];
	    Field_zx_old[i] = 0;
	    Field_zy_old[i] = 0;
            Field_zp_old[i] = 0;
	}
    }
    
    
    public synchronized void setTitle(String TITLE){
	this.TITLE = TITLE;
    }
    
    public synchronized void setLabels(String Z1, String Z2, String X1, String X2){
	this.Z1 = Z1;
	this.Z2 = Z2;
	this.X1 = X1;
	this.X2 = X2;
    }
    
    public synchronized void setMargins(int LeftMargin, int RightMargin, int TopMargin, int BottomMargin){
	this.LeftMargin = LeftMargin;
	this.RightMargin = RightMargin;
	this.TopMargin = TopMargin;
	this.BottomMargin = BottomMargin;
    }
    
    public synchronized void setPlanes(double zpos1, double zpos2, boolean refz){
	this.zpos[0] = zpos1;
	this.zpos[1] = zpos2;
        this.refz = refz;
    }
    
    public synchronized void setRotor(int current_z){
	this.current_z = current_z;
	if(current_z >= zx.length){ current_z = zx.length-1; }
    }
    
    public synchronized void setAlpha(double alpha, double beta, double frequency, double wt){
	this.alpha = alpha;
        this.beta = beta;
        this.frequency = frequency;
        this.wt = wt;
        //System.out.println(wt);
    }
    
    public synchronized void setSkinDepth(double skin_depth){
	this.skin_depth = skin_depth;
    }
    
    public synchronized void setWavelength(double wavelength){
	this.wavelength = wavelength;
    }
    
    public synchronized void setLength(double total_length){
	this.total_length = total_length;
    }
    
    public synchronized void setLines(int nlines){
	this.nlines = nlines;
    }
    
    public synchronized void setEx(double Ex){
	this.Ex = Ex;
    }
    
    public synchronized void setHy(double Hy){
	this.Hy = Hy;
    }
    
    public synchronized void setPz(double Pz){
	this.Pz = Pz;
        this.pmax = Pz;
        this.pmin = -Pz;
    }
    
    public synchronized void setPhasor(boolean IsPhasorOn){
	this.IsPhasorOn = IsPhasorOn;
    }
    
    public synchronized void setSections(boolean SliceIt){
	this.SliceIt = SliceIt;
    }
    
    public synchronized void setEon(boolean IsEon){
	this.IsEon = IsEon;
    }
    
    public synchronized void setHon(boolean IsHon){
	this.IsHon = IsHon;
    }
    
    public synchronized void setSkinDepthOn(boolean IsSkinDepthOn){
	this.IsSkinDepthOn = IsSkinDepthOn;
    } 
    
    public synchronized void setEquivalent(boolean IsEquivalent){
	this.IsEquivalent = IsEquivalent;
    }
    
}
