//StepCanvas.java
import java.awt.*;

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 Font titlefont, labfont, titlefont2, symbolfont, textfont, textfont3, 
            textfont4, textfont2, symbolfont3, symbolfont4, symbolfont2, normalfont,
            normalfontB, normalfont2, smallfont;
    
    public int ystart;
    public String Bigomega, omega;
    private int fonto;
    private int fonto2;
    private int fonto3;
    
    StateVars state;
    
    StepCanvas(StateVars state){
	super();
	this.state = state;
	setBackground(bgcolor);
        
        ystart = state.s55;
        fonto = state.font14;
        fonto2 = state.font14;
        fonto3 = state.font12;
    }
    
    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;
	fm = g.getFontMetrics();
	Bigomega = "\u03a9";
	omega = "\u03c9";
	
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        labfont = new Font("Sanserif",Font.PLAIN,state.font12);
        titlefont = new Font("Sanserif",Font.BOLD,state.font18);
        symbolfont = new Font("Serif",Font.PLAIN,state.font12);;
        normalfont = new Font("Sanserif",Font.PLAIN,state.font12);;
        normalfontB = new Font("Sanserif",Font.BOLD,state.font11);;
        normalfont2 = new Font("Sanserif",Font.PLAIN,state.font12);
        smallfont = new Font("Sanserif",Font.PLAIN,state.font10);
        
        titlefont2 = new Font("Sanserif",Font.BOLD,state.font14);
        symbolfont = new Font("Serif",Font.PLAIN,state.font15);
        textfont = new Font("Sanserif",Font.PLAIN,state.font15);
        textfont3 = new Font("Sanserif",Font.PLAIN,state.font10);
        textfont4 = new Font("Sanserif",Font.PLAIN,state.font14);
        textfont2 = new Font("Sanserif",Font.PLAIN,state.font14);
        symbolfont3 = new Font("Serif",Font.PLAIN,state.font12);
        symbolfont4 = new Font("Serif",Font.PLAIN,state.font13);
        symbolfont2 = new Font("Serif",Font.PLAIN,state.font14);
        
	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){
	FontMetrics fmetrics;
        int xstart1 = state.s35;
	int xstart2 = state.s50;
        ystart = state.s55;
        
        g.setFont(titlefont);
	fmetrics = g.getFontMetrics();
	g.setColor(Color.red.darker());
	String titulo = "Step 1";
	String stmp;
        
        g.drawString(titulo,state.s14,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,state.s80);
	}
	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,state.s80);
	}
	
	
	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,state.s100);
	}
	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,state.s100);
	}
	
        // CHANGE ==============================================================
        
        if(state.getZL().Imaginary() >= 0.0){
            g.setColor(Color.black);
            stmp = "- For frequency analysis, we assume R";
            MaestroG.subscripter(stmp,"L","",g,fonto2,xstart1,state.s135);
            int xExtra = fmetrics.stringWidth("- ");
            g.drawString("remains unchanged and we model",xstart1+xExtra,state.s155);
            g.drawString("the imaginary part as an inductor",xstart1+xExtra,state.s175);
        }
        else{
            g.setColor(Color.black);
            stmp = "- For frequency analysis, we assume R";
            MaestroG.subscripter(stmp,"L","",g,fonto2,xstart1,state.s135);
            int xExtra = fmetrics.stringWidth("- ");
            g.drawString("remains unchanged and we model",xstart1+xExtra,state.s155);
            g.drawString("the imaginary part as a capacitor",xstart1+xExtra,state.s175);
        }
        //======================================================================
        
	g.setColor(Color.black);
	stmp = "- Plot SWR circle with radius r = ";
        
        MaestroG.subscripterSS2(""+stmp,"","| \u0393 |",g,state.font14,xstart1,state.s200);
    }
    
    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,state.s14,fmetrics.getHeight());
	
        g.setColor(Color.black);
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
        
        int xstart1 = state.s35;
	int xstart2 = state.s50;
        ystart = state.s55;
        
	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,state.s60+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,state.s75+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,state.s75+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,state.s60+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,state.s100);
	    
	    stmp = "at the first voltage maximum location";
	    g.drawString(stmp,xstart1,state.s100+fmetrics.getHeight());
	    
	    stmp = "";
	    g.drawString(stmp,xstart1,state.s75+3*fmetrics.getHeight());
            
            g.setColor(Color.red.darker());
	    stmp = "Click Next Step to proceed";
	    g.drawString(stmp,xstart1,state.s75+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,state.s100);
	    stmp = "at the first voltage minimum location";
	    g.drawString(stmp,xstart1,state.s100+fmetrics.getHeight());
	    
	    stmp = "";
	    g.drawString(stmp,xstart1,state.s75+3*fmetrics.getHeight());
            g.setColor(Color.red.darker());
	    stmp = "Click Next Step to proceed";
	    g.drawString(stmp,xstart1,state.s75+4*fmetrics.getHeight());
	}
    }
    
    private void step2(Graphics g){
	String stmp;
        
        int xstart1 = state.s35;
	int xstart2 = state.s50;
        
	String titulo = "Step 2  -  First Solution";
	g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,state.s14,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,state.s60+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,state.s60+4*fmetrics.getHeight());
                    stmp = "at the load location. ";
                    g.drawString(stmp,xstart1,state.s60+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,state.s60+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+8*fmetrics.getHeight()+fmetrics.getHeight()/4);
            }
        }
    }
    
    private void step3(Graphics g){
	
	String titulo = "Step 3  -  First Solution";
	String stmp;
	
	int xstart1 = state.s35;
	int xstart2 = state.s50;
	
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
        
	g.drawString(titulo,state.s14,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 titulo = "Step 4  -  First Solution";
	String stmp;
        String stmp2;
	
	int xstart1 = state.s35;
	int xstart2 = state.s50;
	
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
        
	g.drawString(titulo,state.s14,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);
        
        stmp2 = " = "+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());
            MaestroG.subscripter3("z ( d","1"," )"+stmp2,g,fonto2,xstart1,ystart+9*fmetrics.getHeight());
        }
    }
            
    private void step5(Graphics g){
	String stmp, stmp2, stmp3;
	String titulo = "Step 5  -  First Solution";
	
        int xstart1 = state.s35;
	int xstart2 = state.s50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
        
	g.drawString(titulo,state.s14,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.subsubsubSymbol2("","","Z","in"," = Z","01"," = "+MaestroA.rounder(state.getZ0old(),4),"\u03a9",
                                      g,fonto2,xstart1,state.s85+fmetrics.getHeight()+fmetrics.getHeight()/2);
        
        g.setFont(textfont4);
	g.setColor(Color.black);
	stmp = "Note that the normalized impedance ";
	g.drawString(stmp,xstart1,state.s145);
	stmp = "on the Smith chart is not 1.0 because";
	g.drawString(stmp,xstart1,state.s165);
	MaestroG.subscripterSanSym3("it is still normalized with Z","", "02",". ","",g,fonto2,xstart1,state.s185);
    }
           
    private void step6(Graphics g){
	String stmp;
	String titulo = "Step 6  -  First Solution";
	
        int xstart1 = state.s35;
	int xstart2 = state.s50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
        
	g.drawString(titulo,state.s14,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 = state.s35;
	int xstart2 = state.s50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
        
	g.drawString(titulo,state.s14,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 = state.s35;
	int xstart2 = state.s50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
	g.drawString(titulo,state.s14,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;
	
	int xstart1 = state.s35;
	int xstart2 = state.s50;
        
	g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
        
	g.drawString(titulo,state.s14,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 titulo = "Step 4  -  Second Solution";
	String stmp;
        String stmp2;
	
        int xstart1 = state.s35;
	int xstart2 = state.s50;
        
	g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
        
	g.drawString(titulo,state.s14,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);
        stmp2 = " ) = "+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());
        MaestroG.subscripter3("z ( d","2",stmp2,g,fonto2,xstart1,ystart+9*fmetrics.getHeight());
    }
    
    private void step11(Graphics g){
	
	String stmp, stmp2, stmp3;
	String titulo = "Step 5  -  Second Solution";
	
        int xstart1 = state.s35;
	int xstart2 = state.s50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
        
	g.drawString(titulo,state.s14,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.subsubsubSymbol2("","","Z","in"," = Z","01"," = "+MaestroA.rounder(state.getZ0old(),4),"\u03a9",
                                      g,fonto2,xstart1,state.s85+fmetrics.getHeight()+fmetrics.getHeight()/2);
        
        g.setFont(textfont4);
	g.setColor(Color.black);
	stmp = "Note that the normalized impedance ";
	g.drawString(stmp,xstart1,ystart+6*fmetrics.getHeight());
	stmp = "on the Smith chart is not 1.0 because";
	g.drawString(stmp,xstart1,ystart+7*fmetrics.getHeight());
	MaestroG.subscripterSanSym3("it is still normalized with Z","", "02",". ","",g,fonto2,xstart1,ystart+8*fmetrics.getHeight());
    }
    
    private void step12(Graphics g){
	String stmp;
	String titulo = "Step 6  -  Second Solution";
	
        int xstart1 = state.s35;
	int xstart2 = state.s50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont);
	FontMetrics fmetrics=g.getFontMetrics();
        
	g.drawString(titulo,state.s14,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 = state.s35;
	int xstart2 = state.s50;
        
        g.setColor(Color.red.darker());
	g.setFont(titlefont2);
	FontMetrics fmetrics=g.getFontMetrics();
	
        g.drawString(titulo,state.s14,fmetrics.getHeight());
	
	g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	int ystart = state.s50;
	int xmid = state.s15 + 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 = state.s18;
	    String lambda="\u03bb";
	    int xpos1 = xstart2; 
            int ypos1 = ystart+state.s20; 
            int ypos2 = ypos1 + dy; 
            int sizefont = state.font12;
	    
            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 + state.s83;
	
	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(state.s10,ystart+state.s75,getSize().width-state.s15,ystart+state.s75);
	stmp = "Transformer Length";
	MaestroG.subscripter("","",stmp,g,sizefont,xstart1,ystart+state.s92);
	
        g.setFont(textfont4);
	fmetrics=g.getFontMetrics();
	
        g.setColor(Color.blue.darker());
        xpos1 = xstart1 + state.s130;
        ypos1 = ystart + state.s92;
        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);
            }
	//------------------------------------
    }
}
