//Trans_MultiGraphPanelB.java
/* A Java class for
 * LineImpedance.java
 * Electromagnetic Transmission Line Applet
 * Applet without Smith Chart - Prepared by Umberto Ravaioli 
 * for 6th edition of Fundamentals of Applied Electromagnetics Book
 * May 2009 - All Rights Reserved
 */ 

import java.awt.*;

public class Trans_MultiGraphPanelB extends Panel{
    //private  static final Color bgcolor = new Color(180,147,112);
    private  static final Color bgcolor = new Color(161,220,220);
    private static final Font titlefont = TheFonts.bold12;
    private Label titlelabel;   
    private String titulo;
    public Choice c1;
    
    private Image im;
    private Graphics buf;
    private boolean IsLengthTooLarge, IsLengthTooSmall;
        
    TransGraphCanvas tgc1, tgc2;
    Trans_State state;

    public Trans_MultiGraphPanelB(String titulo, Trans_State state){
	super();
        this.state = state;
	this.titulo=titulo;
	setLayout(null);

        setFont(TheFonts.sanSerif14);

	//setBackground(bgcolor);
	setBackground(Color.white);
	
        IsLengthTooLarge = false;
        IsLengthTooSmall = false;
        
	c1 = new Choice();
	c1.addItem("Voltage Phasor");
	c1.addItem("Current Phasor");
	c1.addItem("Standing Wave Pattern - | V | & | I |");
	c1.addItem("Impedance");
	c1.addItem("Admittance");
	c1.addItem("Reflection Coefficient");
	c1.setBackground(bgcolor.brighter());
	c1.setFont(TheFonts.sanSerif11);
	titlelabel = new Label(titulo,Label.CENTER);
	titlelabel.setFont(new Font("SanSerif",Font.BOLD,state.font12));
	
	tgc1 = new TransGraphCanvas(state);
	tgc2 = new TransGraphCanvas(state);
	add(titlelabel);
	//add(c1);
	add(tgc1);
	add(tgc2);
	titlelabel.setBounds(state.s5,state.s3,state.s100,state.s15);
	c1.setBounds(state.s5,state.s30,state.s300,state.s25);
	tgc1.setBounds(state.s6,state.s21,state.s613,state.s123);
	tgc2.setBounds(state.s6,state.s151,state.s613,state.s123);
	
	Panel pgraph2 = new Panel();
	    pgraph2.setBackground(Color.black);
	    //add(pgraph2);
	    pgraph2.setBounds(state.s6-1,state.s151-1,state.s613+2,state.s123+2);
	    
	Panel pgraph4 = new Panel();
	    pgraph4.setBackground(Color.black);
	    //add(pgraph4);
	    pgraph4.setBounds(state.s6-1,state.s151-1,state.s613+2,state.s123+2);
    }
    
        public void update(Graphics g){
		paint(g);
	}

	public void paint(Graphics g){
		if(im == null){
			im = createImage(getSize().width,getSize().height);
			buf = im.getGraphics();
			drawWarning(buf);
		}
		else{
			drawWarning(buf);
		}
		g.drawImage(im,0,0,null);
	}
    public void drawWarning(Graphics g){
                Font symbolfont=TheFonts.symbol12;
                Font normalfont=TheFonts.sanSerif11;
                String alpha, Ohm, lambda, infinity, Gamma, epsilon;
	
		g.setFont(symbolfont);
		alpha="\u03b1";
		lambda="\u03bb";
		Ohm="\u03a9";
		infinity="\u221e";
		Gamma="\uu0393"; 
		epsilon="\u03b5";
		g.setFont(normalfont);
	
		FontMetrics fm;
		                
                if(IsLengthTooLarge){
                    g.setColor(Color.white);
                    g.fillRect(115,1,180,20);
                    g.setColor(Color.red);
                    
                    MaestroG.subscripterSanSym2IT("","","","","","",g,state.font11,130,14); 
                    fm = g.getFontMetrics();
                    int lungo = fm.stringWidth("Plots resolution limit:  l  < 15 lambda         ");
                    MaestroG.subscripterSanSym2IT("Plots resolution limit:  ","l", " < 15","","",""+lambda,g,state.font11,getSize().width - lungo,state.s14); 
                    
                    //MaestroG.subscripterSanSym2IT("Plots resolution limit:  ","l", " < 15","","",""+lambda,g,11,130,14); 
                    tgc1.IsTooLarge(true);
                    tgc2.IsTooLarge(true);
                    tgc1.repaint();
                    tgc2.repaint();
                }
                else{
                    g.setColor(Color.white);
                    g.fillRect(state.s115,state.s1,getSize().width - state.s125,state.s20);
                    tgc1.IsTooLarge(false);
                    tgc2.IsTooLarge(false);
                    tgc1.repaint();
                    tgc2.repaint();
                }
    }
    
    public synchronized void setXpos(double xpos){
	tgc1.setXpos(xpos);
	tgc2.setXpos(xpos);
    }
    
    public synchronized void setIsVoltage(boolean IsVoltage){
	tgc1.setIsVoltage(IsVoltage);
	tgc2.setIsVoltage(IsVoltage);
    }

    public synchronized void setTooLarge(boolean IsLengthTooLarge){
	this.IsLengthTooLarge = IsLengthTooLarge;
    }
    
    public synchronized void setTooSmall(boolean IsLengthTooSmall){
	this.IsLengthTooSmall = IsLengthTooSmall;
    }
    
     public synchronized void plotImpedance(Trans_State state){
	state.ScanImpedance();
	//System.out.println(state.maxR+"   "+state.maxX+"   "+state.minX);
	//tgc1.setTitle("Re{ Z(z) }   [ \u03a9 ]");
	
	if (state.maxR>=1.0 && state.maxR<1.0E3 || state.maxR==0.0){
	    tgc1.setTitle("Re{ Z ( d ) }   [ \u03a9 ]");  
	}
	else if(state.maxR<1.0E6 && state.maxR>=1.0E3  ){
	      
	    tgc1.setTitle("Re{ Z ( d ) }   [ k\u03a9 ]");
	}
	else if(state.maxR>=1.0E6  ){
	    tgc1.setTitle("Re{ Z ( d ) }   [ M\u03a9 ]");
	      
	}
	else if(state.maxR<1.0 && state.maxR>=1.0E-3  ){
	    tgc1.setTitle("Re{ Z ( d ) }   [ m\u03a9 ]");
	    
	}
	else if(state.maxR<1.0E-3 && state.maxR>=1.0E-6  ){
	    tgc1.setTitle("Re{ Z ( d ) }   [ \u00b5\u03a9 ]");
	     
	}
	else if(state.maxR<1.0E-6 && state.maxR>=1.0E-9  ){
	    tgc1.setTitle("Re{ Z ( d ) }   [ n\u03a9 ]");
	      
	}
	else if(state.maxR<1.0E-9 && state.maxR>=1.0E-12  ){
	    tgc1.setTitle("Re{ Z ( d ) }   [ p\u03a9 ]");
	      
	}
	else if(state.maxR<1.0E-12 && state.maxR>=1.0E-15){
	    tgc1.setTitle("Re{ Z ( d ) }   [ f\u03a9 ]");
	      
	}
	else if(state.maxR<1.0E-15){
	    tgc1.setTitle("Re{ Z ( d ) }   [ a\u03a9 ]");
	}
	
	
	tgc1.reset();
	tgc1.setDynamics(false);
	tgc1.setYRangeMin(0.0);
	//tgc1.reset();
	tgc1.plot(state.x,state.y1);
	
	tgc2.setTitle("Im{ Z ( d ) }   [ \u03a9 ]");
	
	if (state.maxX>=1.0 && state.maxX<1.0E3 || state.maxX==0.0){
	    tgc2.setTitle("Im{ Z ( d ) }   [ \u03a9 ]");  
	}
	else if(state.maxX<1.0E6 && state.maxX>=1.0E3  ){
	      
	    tgc2.setTitle("Im{ Z ( d ) }   [ k\u03a9 ]");
	}
	else if(state.maxX>=1.0E6  ){
	    tgc2.setTitle("Im{ Z ( d ) }   [ M\u03a9 ]");
	      
	}
	else if(state.maxX<1.0 && state.maxX>=1.0E-3  ){
	    tgc2.setTitle("Im{ Z ( d ) }   [ m\u03a9 ]");
	    
	}
	else if(state.maxX<1.0E-3 && state.maxX>=1.0E-6  ){
	    tgc2.setTitle("Im{ Z ( d ) }   [ \u00b5\u03a9 ]");
	     
	}
	else if(state.maxX<1.0E-6 && state.maxX>=1.0E-9  ){
	    tgc2.setTitle("Im{ Z(d ) }   [ n\u03a9 ]");
	      
	}
	else if(state.maxX<1.0E-9 && state.maxX>=1.0E-12  ){
	    tgc2.setTitle("Im{ Z ( d ) }   [ p\u03a9 ]");
	      
	}
	else if(state.maxX<1.0E-12 && state.maxX>=1.0E-15){
	    tgc2.setTitle("Im{ Z ( d ) }   [ f\u03a9 ]");
	      
	}
	else if(state.maxX<1.0E-15){
	    tgc2.setTitle("Im{ Z ( d ) }   [ a\u03a9 ]");
	}
	
	tgc2.setDynamics(false);
        tgc2.setYRangeMin(state.minX);
	tgc2.reset();
	tgc2.plot(state.x,state.y2);
    }
    
}
