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

public class StepCanvas extends Canvas{
    private static final Color bgcolor = new Color(236,236,236);
    //private final Color bgcolor = new Color(170,200,200);
    //private static final Color bgcolor = Color.lightGray;
    private final Font titlefont = TheFonts.sanSerif18;
    private final Font titlefont2 = TheFonts.sanSerif14;
    private final Font symbolfont = TheFonts.symbol15;
    private final Font textfont = TheFonts.sanSerif15;
    //private final Font textfont = TheFonts.serif18;
    private final Font textfont3 = TheFonts.serif10;
    private final Font textfont4 = TheFonts.sanSerif14;
    private final Font textfont2 = TheFonts.sanSerif14;
    private final Font symbolfont3 = TheFonts.symbol12;
    private final Font symbolfont4 = TheFonts.symbol13;
    private final Font symbolfont2 = TheFonts.symbol14;
    public int ystart = 55;
    public String Bigomega, omega;
    private int fonto = 14;
    private int fonto2 = 14;
    private int fonto3 = 12;
    
    
    StateVars state;
    //Stub stub;
    StepCanvas(StateVars state){
	super();
	this.state = state;
	setBackground(bgcolor);
    }
    
    public void paint(Graphics g){
	g.clearRect(0,0,getSize().width,getSize().height);
	g.setColor(bgcolor.darker());
	g.fillRect(0,getSize().height-2,getSize().width,2);
	g.fillRect(getSize().width-2,0,2,getSize().height);
	g.setColor(bgcolor.brighter());
	g.fillRect(0,0,2,getSize().height-1);
	g.fillRect(0,0,getSize().width-2,2);
	
	FontMetrics fm;
	g.setFont(TheFonts.symbol15);
	fm = g.getFontMetrics();
	Bigomega = "\u03a9";
	omega = "\u03c9";
	
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

	switch(state.getStepNumber()){
	    case 0: step0(g); break;
	    case 1: step1(g); break;
	    case 2: step2(g); break;
	    case 3: step3(g); break;
	    case 4: step4(g); break;
	    case 5: step5(g); break;
	    case 6: step6(g); break;
	    case 7: step7(g); break;
	    case 8: step8(g);break;
	    case 9: step9(g);break;
	    case 10: step10(g);break;
	    case 11: step11(g);break;
	    case 12: step12(g);break;
	    case 13: step13(g);break;
	    
	}
	    
    }
    
    private void step0(Graphics g){
	g.setColor(Color.red.darker());
	String titulo = "Step 1";
	String stmp;
        int xstart1 = 35;
	int xstart2 = 50;
        
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,14,fmetrics.getHeight());
	g.setColor(Color.black);
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	stmp = "- Identify normalized load impedance";
	g.drawString(stmp,xstart1,ystart);
	
	g.setColor(Color.blue.darker());
	
        if(state.getZL().Imaginary() >= 0.0){
	    MaestroG.subsubsubSymbol("Z","L"," = R","L"," + j X","L","= "+MaestroA.rounder(state.getZL().Real(),4)+" + j "+MaestroA.rounder(state.getZL().Imaginary(),4)," "+Bigomega,g,fonto2,xstart2,80);
	}
	else{
	    MaestroG.subsubsubSymbol("Z","L"," = R","L"," + j X","L","= "+MaestroA.rounder(state.getZL().Real(),4)+" - j "+MaestroA.rounder(Math.abs(state.getZL().Imaginary()),4)," "+Bigomega,g,fonto2,xstart2,80);
	}
	
	
	if(state.getZL().Imaginary() >= 0.0){
	    MaestroG.subsubsub("z","L"," = r","L"," + j x","L","= "+MaestroA.rounder((state.getZL().Real()/state.getZ0()),4)+" + j "+MaestroA.rounder((state.getZL().Imaginary()/state.getZ0()),4),g,fonto2,xstart2,100);
	}
	else{
	    MaestroG.subsubsub("z","L"," = r","L"," + j x","L","= "+MaestroA.rounder((state.getZL().Real()/state.getZ0()),4)+" - j "+MaestroA.rounder((Math.abs(state.getZL().Imaginary())/state.getZ0()),4),g,fonto2,xstart2,100);
	}
	
        // CHANGE ==============================================================
        //g.setColor(Color.black);
	////stmp = "For frequency analysis we assume";
	//stmp = "- For frequency analysis, we assume R";
	////g.drawString(stmp,xstart1,125);
        //MaestroG.subscripter(stmp,"L","",g,fonto2,xstart1,125);
        //int xExtra = fmetrics.stringWidth("- ");
        //g.drawString("remains unchanged and we model",xstart1+xExtra,145);
        //g.drawString("the imaginary part as an inductor",xstart1+xExtra,165);
        
        if(state.getZL().Imaginary() >= 0.0){
            g.setColor(Color.black);
            //stmp = "For frequency analysis we assume";
            stmp = "- For frequency analysis, we assume R";
            //g.drawString(stmp,15,ystart+85);
            MaestroG.subscripter(stmp,"L","",g,fonto2,xstart1,135);
            int xExtra = fmetrics.stringWidth("- ");
            g.drawString("remains unchanged and we model",xstart1+xExtra,155);
            g.drawString("the imaginary part as an inductor",xstart1+xExtra,175);
        }
        else{
            g.setColor(Color.black);
            //stmp = "For frequency analysis we assume";
            stmp = "- For frequency analysis, we assume R";
            //g.drawString(stmp,15,ystart+85);
            MaestroG.subscripter(stmp,"L","",g,fonto2,xstart1,135);
            int xExtra = fmetrics.stringWidth("- ");
            g.drawString("remains unchanged and we model",xstart1+xExtra,155);
            g.drawString("the imaginary part as a capacitor",xstart1+xExtra,175);
        }
        //======================================================================
        
        
        //g.setColor(Color.blue.darker());
	//if(state.getZL().Imaginary() >= 0.0){
	//    MaestroG.subsubsubSymbol("j X","L"," = j "+omega+" L","","","","= j "+MaestroA.rounder(state.getZL().Imaginary(),4)," "+Bigomega,g,fonto2,xstart2,145);
	//    
        //
	//		double tempore = 0.0;
	//		double temporal = state.getZL().Imaginary()/(2.0*Math.PI*state.frequency);
	//		int xtempore = xstart2;
	//		int ytempore = 170;
	//		
	//		g.setColor(Color.blue);
	//		if(temporal < 1.0E-12 && temporal != 0.0){
	//		    tempore = temporal * 1.0E12;
	//		    MaestroG.subsup("L",""," = "+MaestroA.rounder(tempore,4)+" x 10","-12"," [ H ]",g,fonto2,xtempore, ytempore);
	//		}
	//	
	//		else if(temporal >= 1.0E-12 && temporal < 1.0E-9){
	//		    tempore = temporal * 1.0E12;
	//		    MaestroG.subsup("L",""," = "+MaestroA.rounder(tempore,4)+" x 10","-12"," [ H ]",g,fonto2,xtempore, ytempore);
	//		}
	//		else if(temporal >= 1.0E-9 && temporal < 1.0E-6){
	//		    tempore = temporal * 1.0E9;
	//		    MaestroG.subsup("L",""," = "+MaestroA.rounder(tempore,4)+" x 10","-9"," [ H ]",g,fonto2,xtempore, ytempore);
	//		}
	//		else if(temporal >= 1.0E-6 && temporal < 1.0E-3){
	//		    tempore = temporal * 1.0E6;
	//		    MaestroG.subsup("L",""," = "+MaestroA.rounder(tempore,4)+" x 10","-6"," [ H ]",g,fonto2,xtempore, ytempore);
	//		}
	//		else if(temporal >= 1.0E-3 && temporal < 1.0){
	//		    tempore = temporal * 1.0E3;
	//		    MaestroG.subsup("L",""," = "+MaestroA.rounder(tempore,4)+" x 10","-3"," [ H ]",g,fonto2,xtempore, ytempore);
	//		}
	//		else if(temporal >= 1.0 || temporal == 0.0){
	//		    tempore = temporal;
	//		    MaestroG.subsup("L",""," = "+MaestroA.rounder(tempore,4)+"",""," [ H ]",g,fonto2,xtempore, ytempore);
	//		}
	//}
	//else{
	//    MaestroG.subsubsupSymbol("j X","L"," = ( j "+omega+" C","",")","-1","= - j "+MaestroA.rounder(Math.abs(+state.getZL().Imaginary()),4),"  [ "+Bigomega+" ]",g,fonto2,xstart2,145);
	//			
	//		double tempore = 0.0;
	//		double temporal = 1.0/(2.0*Math.PI*state.frequency*Math.abs(state.getZL().Imaginary()));
	//		int xtempore = xstart2;
	//		int ytempore = 170;
	//		
	//		
	//		g.setColor(Color.blue);
	//		if(temporal < 1.0E-12 && temporal != 0.0){
	//		    tempore = temporal * 1.0E12;
	//		    MaestroG.subsup("C",""," = "+MaestroA.rounder(tempore,4)+" x 10","-12"," [ F ]",g,fonto2,xtempore, ytempore);
	//		}
	//	
	//		else if(temporal >= 1.0E-12 && temporal < 1.0E-9){
	//		    tempore = temporal * 1.0E12;
	//		    MaestroG.subsup("C",""," = "+MaestroA.rounder(tempore,4)+" x 10","-12"," [ F ]",g,fonto2,xtempore, ytempore);
	//		}
	//		else if(temporal >= 1.0E-9 && temporal < 1.0E-6){
	//		    tempore = temporal * 1.0E9;
	//		    MaestroG.subsup("C",""," = "+MaestroA.rounder(tempore,4)+" x 10","-9"," [ F ]",g,fonto2,xtempore, ytempore);
	//		}
	//		else if(temporal >= 1.0E-6 && temporal < 1.0E-3){
	//		    tempore = temporal * 1.0E6;
	//		    MaestroG.subsup("C",""," = "+MaestroA.rounder(tempore,4)+" x 10","-6"," [ F ]",g,fonto2,xtempore, ytempore);
	//		}
	//		else if(temporal >= 1.0E-3 && temporal < 1.0){
	//		    tempore = temporal * 1.0E3;
	//		    MaestroG.subsup("C",""," = "+MaestroA.rounder(tempore,4)+" x 10","-3"," [ F ]",g,fonto2,xtempore, ytempore);
	//		}
	//		else if(temporal >= 1.0 || temporal == 0.0){
	//		    tempore = temporal;
	//		    MaestroG.subsup("C",""," = "+MaestroA.rounder(tempore,4)+"",""," [ F ]",g,fonto2,xtempore, ytempore);
	//		}
	//}
	
	g.setColor(Color.black);
	stmp = "- Plot SWR circle with radius r = ";
        
        MaestroG.subscripterSS2(""+stmp,"","| \u0393 |",g,14,xstart1,195);
    }
    
    private void step1(Graphics g){
	String stmp;
	String titulo = "First Solution";
	g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,14,fmetrics.getHeight());
	g.setColor(Color.black);
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
        
        int xstart1 = 35;
        int xstart2 = 50;
	
	if(state.getZL().Real() == 0.0){
                g.setColor(Color.black);
                g.setFont(textfont4);
		if(state.getZL().Imaginary() == 0.0){
		   stmp = "The load is a short circuit and ";
		}
		else{
		   stmp = "The load is purely imaginary and ";
		}
		g.drawString(stmp,xstart1,ystart);
		stmp = "it cannot be matched.";
		g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
		stmp = "Please, go Back to Start. ";
		g.drawString(stmp,xstart1,60+4*fmetrics.getHeight());
	}
	if(state.getZL().Imaginary() == 0.0 && state.getZL().Real() != 0.0){
	  if(state.getZL().Real() > state.getZ0()){
            
	    MaestroG.subsubsub("Z","L","  is  Real  and  Z","L"," > Z","01","",g,fonto2,xstart1,ystart);
	    g.setFont(textfont4);
            g.setColor(Color.red.darker());
	    stmp = "Click Next Step to proceed";
	    g.drawString(stmp,xstart1,75+4*fmetrics.getHeight());
	    
	  }
	  else if(state.getZL().Real() < state.getZ0()){
	    MaestroG.subsubsub("Z","L","  is  Real  and  Z","L"," < Z","01","",g,fonto2,xstart1,ystart);
	    g.setFont(textfont4);
	    g.setColor(Color.red.darker());
            stmp = "Click Next Step to proceed";
	    g.drawString(stmp,xstart1,75+4*fmetrics.getHeight());
	  }
	  else if(state.getZL().Real() == state.getZ0()){
		g.setColor(Color.red);
                g.setFont(textfont4);
		stmp = "The load impedance is already matched. ";
		
		g.drawString(stmp,xstart1,ystart);
		stmp = "Please, go Back to Start. ";
		g.drawString(stmp,xstart1,60+3*fmetrics.getHeight());
	  }
	}
	
	else if(state.getZL().Imaginary() > 0.0 && state.getZL().Real() != 0.0){
	    MaestroG.subscripter("  Im { Z","L"," } > 0",g,fonto2,xstart1,ystart);
            g.setFont(textfont4);
	    stmp = "The transformer can be inserted";
	    g.drawString(stmp,xstart1,100);
	    
	    stmp = "at the first voltage maximum location";
	    g.drawString(stmp,xstart1,100+fmetrics.getHeight());
	    
	    stmp = "";
	    g.drawString(stmp,xstart1,75+3*fmetrics.getHeight());
            
            g.setColor(Color.red.darker());
	    stmp = "Click Next Step to proceed";
	    g.drawString(stmp,xstart1,75+4*fmetrics.getHeight());
	}
	else if(state.getZL().Imaginary() < 0.0 && state.getZL().Real() != 0.0){
	    MaestroG.subscripter("  Im { Z","L"," } < 0",g,fonto2,xstart1,ystart);
            g.setFont(textfont4);
	    stmp = "The transformer can be inserted";
	    g.drawString(stmp,xstart1,100);
	    stmp = "at the first voltage minimum location";
	    g.drawString(stmp,xstart1,100+fmetrics.getHeight());
	    
	    stmp = "";
	    g.drawString(stmp,xstart1,75+3*fmetrics.getHeight());
            g.setColor(Color.red.darker());
	    stmp = "Click Next Step to proceed";
	    g.drawString(stmp,xstart1,75+4*fmetrics.getHeight());
	}
	
    }
    
    private void step2(Graphics g){
	String stmp;
        
        int xstart1 = 35;
	int xstart2 = 50;
        
	String titulo = "Step 2  -  First Solution";
	g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,14,fmetrics.getHeight());
	g.setColor(Color.black);
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
    if(state.getZL().Real() == 0.0){
		g.setColor(Color.red);
		if(state.getZL().Imaginary() == 0.0){
		   stmp = "The load is a short circuit and ";
		}
		else{
		   stmp = "The load is purely imaginary and ";
		}
		g.drawString(stmp,xstart1,ystart);
		stmp = "it cannot be matched.";
		g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
		stmp = "Please, go Back to Start. ";
		g.drawString(stmp,xstart1,60+4*fmetrics.getHeight());
    }
    else{
      
      if(state.getZL().Imaginary() == 0.0){
	    if(state.getZL().Real() > state.getZ0()){
		stmp = "The load impedance is Real and a ";
		g.drawString(stmp,xstart1,ystart);
		stmp = "voltage maximum is located at the";
		g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
		stmp = "load. ";
		g.drawString(stmp,xstart1,ystart+2*fmetrics.getHeight());
		g.setColor(Color.red);
	    stmp = "You can insert the transformer ";
	    g.drawString(stmp,xstart1,60+4*fmetrics.getHeight());
	    stmp = "at the load location. ";
	    g.drawString(stmp,xstart1,60+5*fmetrics.getHeight());
	    }
	    else if(state.getZL().Real() == state.getZ0()){
		g.setColor(Color.red);
		stmp = "The load impedance is already matched. ";
		
		g.drawString(stmp,xstart1,ystart);
		stmp = "Please, go Back to Start. ";
		g.drawString(stmp,xstart1,60+3*fmetrics.getHeight());
	    }
	    else{
		stmp = "The load impedance is Real and a ";
		g.drawString(stmp,xstart1,ystart);
		stmp = "voltage minimum is located at the";
		g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
		stmp = "load. ";
		g.drawString(stmp,xstart1,ystart+2*fmetrics.getHeight());
		g.setColor(Color.red);
	    stmp = "You can insert the transformer ";
	    g.drawString(stmp,xstart1,ystart+4*fmetrics.getHeight());
	    
	    stmp = "at the load location. ";
	    g.drawString(stmp,xstart1,ystart+5*fmetrics.getHeight());
	    }
	    
      }
      else{
	stmp = "Move along the transmission line from";
	g.drawString(stmp,xstart1,ystart);
	stmp = "load to generator until the first voltage";
	g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	
	if(state.getZL().Imaginary() > 0.0){
	    stmp = "maximum position is reached at";
	    g.drawString(stmp,xstart1,ystart+2*fmetrics.getHeight());
	    
	    g.setColor(Color.blue.darker());
	    stmp = " = "+MaestroA.rounder(state.d1,4);
	    MaestroG.subscripterSSSnew("  d","max",stmp," \u03bb","   from the load",g,fonto2,xstart1,ystart+3*fmetrics.getHeight()+fmetrics.getHeight()/4);
	}
	else{
	    stmp = "minimum position is reached at";
	    g.drawString(stmp,xstart1,ystart+2*fmetrics.getHeight());
	    
	    g.setColor(Color.blue.darker());
	    
	    stmp = " = "+MaestroA.rounder(state.d1,4);
	    MaestroG.subscripterSSSnew("  d","min",stmp,"\u03bb","   from the load",g,fonto2,xstart1,ystart+3*fmetrics.getHeight()+fmetrics.getHeight()/4);
	}
	g.setFont(textfont4);
	
	g.setColor(Color.black);
	stmp = "Here, you can insert the transformer ";
	g.drawString(stmp,xstart1,ystart+5*fmetrics.getHeight());
	stmp = "since the line impedance is Real: ";
	g.drawString(stmp,xstart1,ystart+6*fmetrics.getHeight());
	g.setColor(Color.blue.darker());
	stmp = " = "+MaestroA.rounder(state.Zin1.Real(),4);
        MaestroG.subsubsupSymbol("  Z ( d","1"," )"+stmp,"","","",""," \u03a9",g,fonto2,xstart1,ystart+7*fmetrics.getHeight()+fmetrics.getHeight()/4);
      }
     }
    }
    
    private void step3(Graphics g){
	
	String titulo = "Step 3  -  First Solution";
	String stmp;
	double ytest;
        
	int xstart1 = 35;
	int xstart2 = 50;
	
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,14,fmetrics.getHeight());
	g.setColor(Color.black);
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	stmp = "The transformer is inserted to match";
        g.drawString(stmp,xstart1,ystart);
	stmp = "the line impedance.";
	g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	stmp = "The needed characteristic impedance ";
	g.drawString(stmp,xstart1,ystart+2*fmetrics.getHeight()+fmetrics.getHeight()/2);
	stmp = "of the transformer line is:";
	g.drawString(stmp,xstart1,ystart+3*fmetrics.getHeight()+fmetrics.getHeight()/2);
	g.setColor(Color.blue.darker());
        if(state.getZL().Imaginary() == 0.0){
            MaestroG.subsubsubsupSymbol("Z","02"," = [ Z","in"," Z","L"," ]","1/2","","",g,fonto2,xstart2,ystart+5*fmetrics.getHeight()+fmetrics.getHeight()/2);
        }
        else{
            MaestroG.subsubsubsupSymbol("Z","02"," = [ Z","in"," Z (d","1",") ]","1/2","","",g,fonto2,xstart2,ystart+5*fmetrics.getHeight()+fmetrics.getHeight()/2);
        }
        MaestroG.subscripterSanSym3("Z","","02"," = "+MaestroA.rounder(state.Z_transformer1,4)," \u03a9",g,fonto2,xstart2,ystart+7*fmetrics.getHeight()+fmetrics.getHeight()/2);
    }
    
    private void step4(Graphics g){
	String stmp;
	
        int xstart1 = 35;
	int xstart2 = 50;
        
        String titulo = "Step 4  -  First Solution";
	g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,14,fmetrics.getHeight());
	
	g.setColor(Color.black);
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	stmp = "To move further on the Smith chart";
	g.drawString(stmp,xstart1,ystart);
	stmp = "we renormalize the line impedance";
	g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	stmp = "using the characteristic impedance";
	g.drawString(stmp,xstart1,ystart+2*fmetrics.getHeight());
	stmp = "of the transformer line:";
	g.drawString(stmp,xstart1,ystart+3*fmetrics.getHeight());
	
	g.setColor(Color.blue.darker());
	
	stmp = " = "+MaestroA.rounder(state.Zin1.Real()/state.getZ0old(),4)+" * "
			 +MaestroA.rounder(state.getZ0old(),4)+" / "
			 +MaestroA.rounder(state.Z_transformer1,4)+" = "
			 +MaestroA.rounder(state.Zin1.Real()/state.Z_transformer1,4);
	if(state.getZL().Imaginary() == 0.0){
            MaestroG.subsubsubSymbol2("z","L"," = "+MaestroA.rounder(state.Zin1.Real()/state.getZ0old(),4)+" * Z","01","/ Z","02","","",
                                      g,fonto2,xstart1,ystart+5*fmetrics.getHeight());
            MaestroG.subscripter3("z ","L",stmp,g,fonto2,xstart1,ystart+7*fmetrics.getHeight());
        }
        else{    
            MaestroG.subsubsubSymbol2("z ( d","1"," ) = "+MaestroA.rounder(state.Zin1.Real()/state.getZ0old(),4)+" * Z","01","/ Z","02","","",
                                      g,fonto2,xstart1,ystart+5*fmetrics.getHeight());
            MaestroG.subscripter3("z ( d","1"," )"+stmp,g,fonto2,xstart1,ystart+7*fmetrics.getHeight());
        }
    }
            
    
    private void step5(Graphics g){
	String stmp, stmp2, stmp3;
	String titulo = "Step 5  -  First Solution";
	
        int xstart1 = 35;
	int xstart2 = 50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,14,fmetrics.getHeight());
	g.setColor(Color.black);
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	stmp = "Move to the input of the transformer. ";
	g.drawString(stmp,xstart1,ystart);
	stmp = "Here the line impedance is matched: ";
	g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	
	g.setColor(Color.blue.darker());
	
        //MaestroG.subscripterSymSubSym("Z [( "+MaestroA.rounder(state.d1,4)+" + 0.25 )","\u03bb"," ] = Z","01",
          //                            " = "+MaestroA.rounder(state.getZ0old(),4)+" ","\u03a9",
            //                          g,fonto2,xstart1,85+fmetrics.getHeight()+fmetrics.getHeight()/2);
        
        MaestroG.subsubsubSymbol2("","","Z","in"," = Z","01"," = "+MaestroA.rounder(state.getZ0old(),4),"\u03a9",
                                      g,fonto2,xstart1,85+fmetrics.getHeight()+fmetrics.getHeight()/2);
        
        g.setFont(textfont4);
	g.setColor(Color.black);
	stmp = "Note that the normalized impedance ";
	g.drawString(stmp,xstart1,145);
	stmp = "on the Smith chart is not 1.0 because";
	g.drawString(stmp,xstart1,165);
	MaestroG.subscripterSanSym3("it is still normalized with Z","", "02",". ","",g,fonto2,xstart1,185);
    }
           
   
    private void step6(Graphics g){
	String stmp;
	String titulo = "Step 6  -  First Solution";
	
        int xstart1 = 35;
	int xstart2 = 50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,14,fmetrics.getHeight());
	g.setColor(Color.black);
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	stmp = "Since we are going back to the original";
	g.drawString(stmp,xstart1,ystart);
	stmp = "transmission line, the line impedance";
	g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	stmp = " :";
	MaestroG.subscripter3("on the chart is re-normalized with Z","01",stmp,g,fonto2,xstart1,ystart+2*fmetrics.getHeight());
	
        g.setColor(Color.blue.darker());
	
        stmp = " = "+MaestroA.rounder(state.getZ0old()/state.Z_transformer1,4)+" * "
			 +MaestroA.rounder(state.Z_transformer1,4)+" / "
			 +MaestroA.rounder(state.getZ0(),4)+" = 1.0";
	MaestroG.subsubsubSymbol2("z","in"," = "+MaestroA.rounder(state.getZ0old()/state.Z_transformer1,4)+" * Z","02","/ Z","01","","",
                                      g,fonto2,xstart1,ystart+4*fmetrics.getHeight());
        MaestroG.subscripter3("z","in",stmp,g,fonto2,xstart1,ystart+6*fmetrics.getHeight());
    }
    
    
     private void step7(Graphics g){
	String stmp;
	String titulo = "Second Solution";
	
        int xstart1 = 35;
	int xstart2 = 50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,14,fmetrics.getHeight());
	g.setColor(Color.black);
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	
	stmp = "The transformer can be inserted at the";
	g.drawString(stmp,xstart1,ystart);
	
	if(state.getZL().Imaginary() == 0.0){
	  if(state.getZL().Real() > state.getZ0()){
	    stmp = "next voltage minimum location";
	    g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	  }
	  else{
	    stmp = "next voltage maximum location";
	    g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	  }
	}
	
	else if(state.getZL().Imaginary() > 0.0){
	    stmp = "next voltage minimum location";
	    g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	}
	else{
	    stmp = "next voltage maximum location";
	    g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	}
	
	stmp = "";
        
        g.setColor(Color.red.darker());
	g.drawString(stmp,xstart1,ystart+3*fmetrics.getHeight());
	stmp = "Click Next Step to proceed";
	g.drawString(stmp,xstart1,ystart+4*fmetrics.getHeight());
	
    }
       
   private void step8(Graphics g){
	String stmp;
	String titulo = "Step 2  -  Second Solution";
	
        int xstart1 = 35;
	int xstart2 = 50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,14,fmetrics.getHeight());
	g.setColor(Color.black);
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	
	stmp = "Move on the line to reach the location of";
	g.drawString(stmp,xstart1,ystart);
	
	if(state.getZL().Imaginary() == 0.0){
	  if(state.getZL().Real() > state.getZ0old()){
	    stmp = "the next voltage minimum";
	    g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	    
	    g.setColor(Color.blue.darker());
	    
            stmp = " = "+MaestroA.rounder(state.d2,4);
	    MaestroG.subscripterSSSnew("  d","min",stmp," \u03bb","   from the load",g,fonto2,xstart1,ystart+3*fmetrics.getHeight());
	  }
	  else{
	    stmp = "the next voltage maximum";
	    g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	    
	    g.setColor(Color.blue.darker());
	    stmp = " = "+MaestroA.rounder(state.d2,4);
	    MaestroG.subscripterSSSnew("  d","max",stmp," \u03bb","   from the load",g,fonto2,xstart1,ystart+3*fmetrics.getHeight());
	  }
	}
	
	else if(state.getZL().Imaginary() > 0.0){
	    stmp = "the next voltage minimum";
	    g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	    
	    g.setColor(Color.blue.darker());
	    
            stmp = " = "+MaestroA.rounder(state.d2,4);
	    MaestroG.subscripterSSSnew("  d","min",stmp," \u03bb","   from the load",g,fonto2,xstart1,ystart+3*fmetrics.getHeight());
	}
	else{
	    stmp = "the next voltage maximum";
	    g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	    
	    g.setColor(Color.blue.darker());
	    
            stmp = " = "+MaestroA.rounder(state.d2,4);
	    MaestroG.subscripterSSSnew("  d","max",stmp," \u03bb","   from the load",g,fonto2,xstart1,ystart+3*fmetrics.getHeight());
	}
        
	g.setColor(Color.black);
	stmp = "which has Real line impedance: ";
	g.drawString(stmp,xstart1,ystart+5*fmetrics.getHeight());
	
        g.setColor(Color.blue.darker());
	
        stmp = " = "+MaestroA.rounder(state.Zin2.Real(),4);
        MaestroG.subsubsupSymbol("  Z ( d","2"," ) "+stmp,"","","",""," \u03a9",g,fonto2,xstart1,ystart+7*fmetrics.getHeight());
    }
       
    
    private void step9(Graphics g){
	String titulo = "Step 3  -  Second Solution";
	String stmp;
	double ytest;
	int xstart1 = 35;
	int xstart2 = 50;
        
	g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,14,fmetrics.getHeight());
	g.setColor(Color.black);
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	stmp = "The transformer is inserted to match";
	g.drawString(stmp,xstart1,ystart);
	
	stmp = "the line impedance.";
	g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	stmp = "The needed characteristic impedance ";
	g.drawString(stmp,xstart1,ystart+2*fmetrics.getHeight()+fmetrics.getHeight()/2);
	stmp = "of the transformer line is:";
	g.drawString(stmp,xstart1,ystart+3*fmetrics.getHeight()+fmetrics.getHeight()/2);
	
        g.setColor(Color.blue.darker());
	MaestroG.subsubsubsupSymbol("Z","02"," = [ Z","in"," Z (d","2",") ]","1/2","","",g,fonto2,xstart2,ystart+5*fmetrics.getHeight()+fmetrics.getHeight()/2);
        MaestroG.subscripterSanSym3("Z","","02"," = "+MaestroA.rounder(state.Z_transformer2,4)," \u03a9",g,fonto2,xstart2,ystart+7*fmetrics.getHeight()+fmetrics.getHeight()/2);
    }
    private void step10(Graphics g){
    
	String stmp;
	String titulo = "Step 4  -  Second Solution";
	
        int xstart1 = 35;
	int xstart2 = 50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,14,fmetrics.getHeight());
	
	g.setColor(Color.black);
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	stmp = "To move further on the Smith chart";
	g.drawString(stmp,xstart1,ystart);
	stmp = "we renormalize the line impedance";
	g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	stmp = "using the characteristic impedance";
	g.drawString(stmp,xstart1,ystart+2*fmetrics.getHeight());
	stmp = "of the transformer line:";
	g.drawString(stmp,xstart1,ystart+3*fmetrics.getHeight());
	
	
	g.setColor(Color.blue.darker());
	
	stmp = " ) = "+MaestroA.rounder(state.Zin2.Real()/state.getZ0old(),4)+" * "
			 +MaestroA.rounder(state.getZ0old(),4)+" / "
			 +MaestroA.rounder(state.Z_transformer2,4)+" = "
			 +MaestroA.rounder(state.Zin2.Real()/state.Z_transformer2,4);
        
        MaestroG.subsubsubSymbol2("z ( d","2"," ) = "+MaestroA.rounder(state.Zin2.Real()/state.getZ0old(),4)+" * Z","01","/ Z","02","","",
                                      g,fonto2,xstart1,ystart+5*fmetrics.getHeight());
            
	MaestroG.subscripter3("z ( d","2",stmp,g,fonto2,xstart1,ystart+7*fmetrics.getHeight()); 
    }
    
    private void step11(Graphics g){
	
	String stmp, stmp2, stmp3;
	String titulo = "Step 5  -  Second Solution";
	
        int xstart1 = 35;
	int xstart2 = 50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,14,fmetrics.getHeight());
	g.setColor(Color.black);
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	stmp = "Move to the input of the transformer. ";
	g.drawString(stmp,xstart1,ystart);
	stmp = "Here the line impedance is matched: ";
	g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	
	g.setColor(Color.blue.darker());      
        
        //MaestroG.subscripterSymSubSym("Z [( "+MaestroA.rounder(state.d2,4)+" + 0.25 ) ","\u03bb"," ] = Z","01",
          //                            " = "+MaestroA.rounder(state.getZ0old(),4)+" ","\u03a9",
            //                          g,fonto2,xstart1,ystart+3*fmetrics.getHeight());
        
        
        MaestroG.subsubsubSymbol2("","","Z","in"," = Z","01"," = "+MaestroA.rounder(state.getZ0old(),4),"\u03a9",
                                      g,fonto2,xstart1,85+fmetrics.getHeight()+fmetrics.getHeight()/2);
        
        g.setFont(textfont4);
	g.setColor(Color.black);
	stmp = "Note that the normalized impedance ";
	g.drawString(stmp,xstart1,ystart+5*fmetrics.getHeight());
	stmp = "on the Smith chart is not 1.0 because";
	g.drawString(stmp,xstart1,ystart+6*fmetrics.getHeight());
	MaestroG.subscripterSanSym3("it is still normalized with Z","", "02",". ","",g,fonto2,xstart1,ystart+7*fmetrics.getHeight());
    }
    
    private void step12(Graphics g){
    
    
	String stmp;
	String titulo = "Step 6  -  Second Solution";
	
        int xstart1 = 35;
	int xstart2 = 50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,14,fmetrics.getHeight());
	g.setColor(Color.black);
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	stmp = "Since we are going back to the original";
	g.drawString(stmp,xstart1,ystart);
	stmp = "transmission line, the line impedance";
	g.drawString(stmp,xstart1,ystart+fmetrics.getHeight());
	stmp = " :";
	MaestroG.subscripter3("on the chart is re-normalized with Z","01",stmp,g,fonto2,xstart1,ystart+2*fmetrics.getHeight());
	g.setColor(Color.blue.darker());
	
        stmp = " = "+MaestroA.rounder(state.getZ0old()/state.Z_transformer2,4)+" * "
			 +MaestroA.rounder(state.Z_transformer2,4)+" / "
			 +MaestroA.rounder(state.getZ0(),4)+" = 1.0";
	
        MaestroG.subsubsubSymbol2("z","in"," = "+MaestroA.rounder(state.getZ0old()/state.Z_transformer2,4)+" * Z","02","/ Z","01","","",
                                      g,fonto2,xstart1,ystart+4*fmetrics.getHeight());
        MaestroG.subscripter3("z","in",stmp,g,fonto2,xstart1,ystart+6*fmetrics.getHeight());
    }
    
    
    
    private void step13(Graphics g){
	String titulo = "SUMMARY";
	String stmp;
	double ytest;
	
        int xstart1 = 35;
	int xstart2 = 50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont2);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,16,fmetrics.getHeight());
	
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	int ystart = 50;
	int xmid = 15+fmetrics.stringWidth("Transformer Location: d ");
	
	g.setColor(Color.black);
	stmp = "First Solution";
	g.drawString(stmp,xstart1,ystart);
	
        double testlength = state.d1_meters;
	    int range = 5;
            int dy = 18;
	    String lambda="\u03bb";
	    int xpos1 = xstart2; 
            int ypos1 = ystart+20; 
            int ypos2 = ypos1 + dy; 
            int sizefont = 12;
	    
            g.setColor(Color.blue.darker());
            
            if(testlength < 1.0E-9 && testlength > 0.0){
		MaestroG.subscripterSymMiddle2("d","1"," = "+MaestroA.rounder(state.d1,range),lambda,"  = "+MaestroA.rounder(state.d1_meters*1.0e9,8)+" nm",g,sizefont,xpos1,ypos1);	
            }
	    else if(testlength < 1.0E-6 && testlength >= 1.0E-9){
		MaestroG.subscripterSymMiddle2("d","1"," = "+MaestroA.rounder(state.d1,range),lambda,"  = "+MaestroA.rounder(state.d1_meters*1.0e9,4)+" nm",g,sizefont,xpos1,ypos1);	
            }
	    else if(testlength < 1.0E-3 && testlength >= 1.0E-6){
		MaestroG.subscripterSymMiddle2("d","1"," = "+MaestroA.rounder(state.d1,range),lambda,"  = "+MaestroA.rounder(state.d1_meters*1.0e6,4)+" \u00b5m",g,sizefont,xpos1,ypos1);	
	    }
	    else if(testlength < 1.0 && testlength >= 1.0E-3){
		MaestroG.subscripterSymMiddle2("d","1"," = "+MaestroA.rounder(state.d1,range),lambda,"  = "+MaestroA.rounder(state.d1_meters*1000.0,4)+" mm",g,sizefont,xpos1,ypos1);
            }
	    else if((testlength < 1000.0 && testlength >= 1.0 ) || testlength == 0.0){
		MaestroG.subscripterSymMiddle2("d","1"," = "+MaestroA.rounder(state.d1,range),lambda,"  = "+MaestroA.rounder(state.d1_meters,6)+" m",g,sizefont,xpos1,ypos1);	
            }
	    else if(testlength >= 1000.0 && testlength < 1.0E6){
		MaestroG.subscripterSymMiddle2("d","1"," = "+MaestroA.rounder(state.d1,range),lambda,"  = "+MaestroA.rounder(state.d1_meters/1.0e3,4)+" km",g,sizefont,xpos1,ypos1);	
            }
	    else if(testlength >= 1.0E6 && testlength < 1.0E9){
		MaestroG.subSymsup("d","1"," = "+MaestroA.rounder(state.d1,range),lambda,"  = "+MaestroA.rounder(state.d1_meters/1.0e6,4)+" x 10","3"," km",g,sizefont,xpos1,ypos1);
            }
	    else if(testlength >= 1.0E9){
		MaestroG.subSymsup("d","1"," = "+MaestroA.rounder(state.d1,range),lambda,"  = "+MaestroA.rounder(state.d1_meters/1.0e9,4)+" x 10","6"," km",g,sizefont,xpos1,ypos1);
            }

            MaestroG.subscripterSanSym3("Z (d","","1",") = "+MaestroA.rounder(state.Zin1.Real(),4)," \u03a9",g,sizefont,xpos1,ypos1+dy);
            MaestroG.subscripterSanSym3("Z","","02"," = "+MaestroA.rounder(state.Z_transformer1,4)," \u03a9",g,sizefont,xpos1,ypos1+dy+dy);
        
	
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	
	ystart = ystart+83;
	
	g.setColor(Color.black);
	stmp = "Second Solution";
	g.drawString(stmp,xstart1,ystart);
	
	ypos1 = ystart + dy;
        
        g.setColor(Color.blue.darker());
        
            testlength = state.d2_meters;
            
            if(testlength < 1.0E-9 && testlength > 0.0){
		MaestroG.subscripterSymMiddle2("d","2"," = "+MaestroA.rounder(state.d2,range),lambda,"  = "+MaestroA.rounder(state.d2_meters*1.0e9,8)+" nm",g,sizefont,xpos1,ypos1);	
            }
	    else if(testlength < 1.0E-6 && testlength >= 1.0E-9){
		MaestroG.subscripterSymMiddle2("d","2"," = "+MaestroA.rounder(state.d2,range),lambda,"  = "+MaestroA.rounder(state.d2_meters*1.0e9,4)+" nm",g,sizefont,xpos1,ypos1);	
            }
	    else if(testlength < 1.0E-3 && testlength >= 1.0E-6){
		MaestroG.subscripterSymMiddle2("d","2"," = "+MaestroA.rounder(state.d2,range),lambda,"  = "+MaestroA.rounder(state.d2_meters*1.0e6,4)+" \u00b5m",g,sizefont,xpos1,ypos1);	
	    }
	    else if(testlength < 1.0 && testlength >= 1.0E-3){
		MaestroG.subscripterSymMiddle2("d","2"," = "+MaestroA.rounder(state.d2,range),lambda,"  = "+MaestroA.rounder(state.d2_meters*1000.0,4)+" mm",g,sizefont,xpos1,ypos1);
            }
	    else if((testlength < 1000.0 && testlength >= 1.0 ) || testlength == 0.0){
		MaestroG.subscripterSymMiddle2("d","2"," = "+MaestroA.rounder(state.d2,range),lambda,"  = "+MaestroA.rounder(state.d2_meters,6)+" m",g,sizefont,xpos1,ypos1);	
            }
	    else if(testlength >= 1000.0 && testlength < 1.0E6){
		MaestroG.subscripterSymMiddle2("d","2"," = "+MaestroA.rounder(state.d2,range),lambda,"  = "+MaestroA.rounder(state.d2_meters/1.0e3,4)+" km",g,sizefont,xpos1,ypos1);	
            }
	    else if(testlength >= 1.0E6 && testlength < 1.0E9){
		MaestroG.subSymsup("d","2"," = "+MaestroA.rounder(state.d2,range),lambda,"  = "+MaestroA.rounder(state.d2_meters/1.0e6,4)+" x 10","3"," km",g,sizefont,xpos1,ypos1);
            }
	    else if(testlength >= 1.0E9){
		MaestroG.subSymsup("d","2"," = "+MaestroA.rounder(state.d2,range),lambda,"  = "+MaestroA.rounder(state.d2_meters/1.0e9,4)+" x 10","6"," km",g,sizefont,xpos1,ypos1);
            }

            MaestroG.subscripterSanSym3("Z (d","","2",") = "+MaestroA.rounder(state.Zin2.Real(),4)," \u03a9",g,sizefont,xpos1,ypos1+dy);
            MaestroG.subscripterSanSym3("Z","","02"," = "+MaestroA.rounder(state.Z_transformer2,4)," \u03a9",g,sizefont,xpos1,ypos1+dy+dy);
        
        g.setColor(Color.black);
	g.drawLine(10,ystart+75,getSize().width-15,ystart+75);
	stmp = "Transformer Length";
	MaestroG.subscripter("","",stmp,g,sizefont,xstart1,ystart+92);
	
        g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	
        g.setColor(Color.blue.darker());
        xpos1 = xstart1+130;
        ypos1 = ystart +92;
        testlength = state.L2_meters;
            
            if(testlength < 1.0E-9 && testlength > 0.0){
		MaestroG.subscripterSymFirst2(""+lambda,"2","/4 = "+MaestroA.rounder(state.L2_meters*1.0e9,8)+" nm",g,sizefont,xpos1,ypos1);	
            }
	    else if(testlength < 1.0E-6 && testlength >= 1.0E-9){
		MaestroG.subscripterSymFirst2(""+lambda,"2","/4 = "+MaestroA.rounder(state.L2_meters*1.0e9,4)+" nm",g,sizefont,xpos1,ypos1);	
            }
	    else if(testlength < 1.0E-3 && testlength >= 1.0E-6){
		MaestroG.subscripterSymFirst2(""+lambda,"2","/4 = "+MaestroA.rounder(state.L2_meters*1.0e6,4)+" \u00b5m",g,sizefont,xpos1,ypos1);	
	    }
	    else if(testlength < 1.0 && testlength >= 1.0E-3){
		MaestroG.subscripterSymFirst2(""+lambda,"2","/4 = "+MaestroA.rounder(state.L2_meters*1000.0,4)+" mm",g,sizefont,xpos1,ypos1);
            }
	    else if((testlength < 1000.0 && testlength >= 1.0 ) || testlength == 0.0){
		MaestroG.subscripterSymFirst2(""+lambda,"2","/4 = "+MaestroA.rounder(state.L2_meters,6)+" m",g,sizefont,xpos1,ypos1);	
            }
	    else if(testlength >= 1000.0 && testlength < 1.0E6){
		MaestroG.subscripterSymFirst2(""+lambda,"2","/4 = "+MaestroA.rounder(state.L2_meters/1.0e3,4)+" km",g,sizefont,xpos1,ypos1);	
            }
	    else if(testlength >= 1.0E6 && testlength < 1.0E9){
		MaestroG.subSymsup("","","",lambda,"/4  = "+MaestroA.rounder(state.L2_meters/1.0e6,4)+" x 10","3"," km",g,sizefont,xpos1,ypos1);
            }
	    else if(testlength >= 1.0E9){
		MaestroG.subSymsup("","","",lambda,"/4  = "+MaestroA.rounder(state.L2_meters/1.0e9,4)+" x 10","6"," km",g,sizefont,xpos1,ypos1);
            }
	//------------------------------------
    }
    
}
