/**
 * TransGraphCanvas.java
 * Graphs specific for the transmission lines
 */
/* A Java class for
 * LossyWide.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.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.net.*;
import java.awt.event.*;
import java.lang.*;

public class TransGraphCanvas extends Canvas implements ComponentListener{
    
    private static final Color bgcolor = new Color(246,246,246); 
       
    private double ymax, ymin, xmax, xmin;
    private boolean IsYRangeMaxSet, IsYRangeMinSet, IsXRangeMaxSet, IsXRangeMinSet; 
    //private int LeftMargin=30, RightMargin=30, TopMargin=20, BottomMargin=20;
    private int LeftMargin=65, RightMargin=35, TopMargin=20, BottomMargin=20;
    private int WIDTH, HEIGHT;
    private String X1, X2, Y1, Y2, Y3, Y4, TITLE, UNITS, LABELY, VALUE;
    private Image im;
    private int Flag;
    private Graphics buf;
    private int VPos = 0;
    private static final Font TitleFont = new Font("SanSerif",Font.PLAIN,12);
    private static final Font TitleFontSe = new Font("Serif",Font.PLAIN,13);
    private static final Font LabelFont = new Font("SanSerif",Font.PLAIN,11);
    private static final Font LabelFontIT = new Font("Serif",Font.ITALIC,14);
    private static final Font LabelFontIT2 = new Font("Serif",Font.ITALIC,16);
    private static final Font LabelFontITsmall = new Font("Serif",Font.ITALIC,13);
    private double[] x, y;
    protected double xpos;
    private int[] xx, yy;
    private int[] xx_old, yy_old;
    private int N;
    private int lab_flag;
    private boolean IsTraceOn, IsCleanUpOn, IsDynamic;
    public TransGraphCanvas(){
	super();
	N = 301;
	Flag = 0;
	setBackground(bgcolor);
	xmax = 1.0;
	xmin = 0.0;
	ymax = 2.0;
	ymin = -2.0;
	X1 = "l";
	X2 = "0";
	Y1 = "  ";
	Y2 = "  ";
	TITLE = "  ";
	UNITS = "  ";
	LABELY = "   ";
	VALUE = "   ";
	
        lab_flag = 1;
        
	IsYRangeMaxSet = false;
	IsYRangeMinSet = false;
	IsXRangeMaxSet = false;
	IsXRangeMinSet = false;
	IsTraceOn = false;
	IsCleanUpOn = false;
	IsDynamic = false;
	try{
	    x = new double[N];
	    y = new double[N];
	    xx = new int[N];
	    yy = new int[N];
	    xx_old = new int[N];
	    yy_old = new int[N];
	}
	catch(Exception e){e.printStackTrace();}
	for(int i = 0; i < x.length; i++){
	    x[i] = (double)i;
	    y[i] = 0.0;
	}
	//Listeners
	this.addComponentListener(this);
    }
    
    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 update(Graphics g){
	paint(g);
    }
    
    public void drawGraph(Graphics g){
	if(!IsDynamic){
	    g.clearRect(0,0,getSize().width-1,getSize().height-1);
	}
	drawAxis(g);
	labelDetect();
	drawLabels(g);
	drawZeroLine(g);
	drawPoints(g,1);
        drawAxis(g);
	drawRef(g);

    }
    
    private void ignition(){
	N = x.length;
	//Find the boundaries for data
    	if(!IsYRangeMaxSet){ ymax = MaestroA.getMax(y);}
	if(!IsYRangeMinSet){ ymin = MaestroA.getMin(y);}
	
	if(!IsXRangeMaxSet){ xmax = x[x.length-1];}
	if(!IsXRangeMinSet){ xmin = x[0]; }
	
	//Confine data
	MaestroA.confiner(x,xmax,xmin);
	MaestroA.confiner(y,ymax,ymin);
	if(ymax==0.0 && ymin==0.0){ymax=0.001;}
	
	//Reverse Mapping
	for(int i = 0; i < xx.length; i++){
	    xx[N-1-i] = (int)MaestroA.mapper(x[i],(double)LeftMargin+1,(double)(getSize().width-RightMargin),xmax,xmin);
	    yy[N-1-i] = (int)MaestroA.mapper(y[i],(double)TopMargin+1,(double)(getSize().height-BottomMargin)-1,ymax,ymin);
	}
    }
    
    private void drawPoints(Graphics g, int tipo){
        Graphics2D g2d = (Graphics2D)g;
        g2d.setPaint(Color.black);
        
        /*  OLD GRAPHICS
	switch(tipo){
	    case 1:
		if(IsTraceOn){
		    //g.setColor(Color.yellow);
		    g.setColor(Color.green);
		    g.drawPolyline(xx,yy,xx.length);
		    g.setColor(Color.black);
		    g.drawPolyline(xx_old,yy_old,xx_old.length);
		}
		else{
         	    g.setColor(Color.blue);
		    g.drawPolyline(xx,yy,xx.length);
		    }
		}
		break;
	    case 2:
		g.setColor(bgcolor);
		g.drawPolyline(xx_old,yy_old,xx_old.length);
		break;
	}
        */
        
        // NEW GRAPHICS
        g2d.setStroke(new BasicStroke(1));
	
        switch(tipo){
	    case 1:
                
                if(IsTraceOn){
		    
                    g2d.setPaint(Color.green);
                    GeneralPath s = new GeneralPath();
                    s.moveTo((float)xx[0],(float)yy[0]); 
                    for (int i=1; i<N;i++){
                        s.lineTo((float)xx[i],(float)yy[i]);
                        
                        g2d.draw(s);
                    }
                    
                    g2d.setPaint(Color.black);
                    GeneralPath s2 = new GeneralPath();
                    s2.moveTo((float)xx_old[0],(float)yy_old[0]); 
                    for (int i=1; i<N;i++){
                        s2.lineTo((float)xx_old[i],(float)yy_old[i]);
                        
                        g2d.draw(s2);
                    }
		}
		else{
                    g2d.setPaint(Color.blue);
                    GeneralPath s = new GeneralPath();
                    s.moveTo((float)xx[0],(float)yy[0]); 
                    for (int i=1; i<N;i++){
                        s.lineTo((float)xx[i],(float)yy[i]);
                        
                        g2d.draw(s);
                    }
		}
		break;
	    case 2:
                    g2d.setPaint(bgcolor);
                    GeneralPath s = new GeneralPath();
                    s.moveTo((float)xx[0],(float)yy[0]); 
                    for (int i=1; i<N;i++){
                        s.lineTo((float)xx[i],(float)yy[i]);
                        
                        g2d.draw(s);
                    }
		break;
	}
    }
       
    private void drawAxis(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
	
	VPos = getSize().height - BottomMargin;
	WIDTH = getSize().width - LeftMargin -RightMargin;
	HEIGHT = VPos - TopMargin;
	g.setColor(Color.black);
	g.drawLine(LeftMargin-5,VPos,getSize().width-RightMargin+5,VPos);
	g.drawLine(LeftMargin,TopMargin,LeftMargin,VPos+5);
	g.drawLine(getSize().width-RightMargin,TopMargin,getSize().width-RightMargin,VPos+5);
	g.drawLine(LeftMargin,TopMargin,LeftMargin-5,TopMargin);
        g.drawLine(getSize().width-RightMargin,TopMargin,getSize().width-RightMargin+5,TopMargin);
    }
    
    private void labelDetect(){
	
	if(ymax==0.0){
	    Y1 = "0.0";
	}
	else if(Math.abs(ymax)>=1.0){
	    Y1 = String.valueOf(MaestroA.rounder(ymax,3))+" ";
	}
	else if(Math.abs(ymax)<1.0 && Math.abs(ymax)>0.0){
	    Y1 = String.valueOf(MaestroA.rounder(ymax,5))+" ";
	}
	
	if(ymin==0.0){
	    Y2 = "0.0";
	}
	else if(Math.abs(ymin)>=1.0){
	    Y2 = String.valueOf(MaestroA.rounder(ymin,3))+" ";
	}
	else if(Math.abs(ymin)<1.0 && Math.abs(ymin)>0.0){
	    Y2 = String.valueOf(MaestroA.rounder(ymin,5))+" ";
	}
	
	Y3 = "_";
	Y4 = "|";

    }
    
    public void drawRef(Graphics g){
	   int myX, myY, myZero;
	   Graphics2D g2d = (Graphics2D)g;
           
   	  if(xpos <=xmax && xpos >= xmin){
		int tx1, ty1, tx2, ty2;
		tx1 = (int)MaestroA.mapper(xpos,(double)LeftMargin+1,(double)(getSize().width-RightMargin),xmax,xmin);
		ty1 = TopMargin;
		tx2 = tx1;
		ty2 = getSize().height-BottomMargin;
		
		if(xpos == 0.0){
		    g.setColor(Color.green);
		}
		else{
		    g.setColor(Color.green);
		}
		
		myZero = (int)MaestroA.mapper(0,(double)TopMargin,(double)VPos,ymax,ymin);
		
		int i = (int)(N*(xpos-x[0])/(x[x.length-1]-x[0]));
		if(i<0) {i = 0;}
		if(i>=N){i=N-1;}
		
		// y-coordinate
		myY = (int)MaestroA.mapper(y[i],(double)TopMargin,(double)(getSize().height-BottomMargin),ymax,ymin);
		
		g.drawLine(tx1,myY,tx2,myZero);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		//-----------  draw circle  ------------------------------------------
		g.setColor(Color.green);
		MaestroG.fillCircle(tx1,myY,6,g);
		g.setColor(Color.black);
		MaestroG.drawCircle(tx1,myY,6,g);		
		//--------------------------------------------------------------------
               g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	    }
    }	

    
    private void drawLabels(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        String tmp;
	FontMetrics fm;
        
	g.clearRect(0,0,LeftMargin-1,getSize().height-1);
	g.clearRect(0,0,getSize().width-1,TopMargin-1);
	
        g.setFont(LabelFontIT2);
        fm = g.getFontMetrics();
	tmp = X1;
	//g.drawString(tmp,LeftMargin+3,getSize().height-3);
	
        g.setFont(LabelFont);
        fm = g.getFontMetrics();
	
	tmp = X2;
	g.drawString(tmp,getSize().width-RightMargin-fm.stringWidth(tmp)/2+1,VPos+fm.getHeight()+2);
	tmp = Y1;
	//g.drawString(tmp,LeftMargin-fm.stringWidth(tmp),TopMargin+fm.getHeight());
	g.drawString(tmp,LeftMargin-fm.stringWidth(tmp)-10,TopMargin);
	tmp = Y2;
	g.drawString(tmp,LeftMargin-fm.stringWidth(tmp)-10,VPos);
	
	tmp = Y3;
	//g.drawString(tmp,LeftMargin-5,VPos-1);
	//g.drawString(tmp,LeftMargin-5, TopMargin);
	//g.drawString(tmp,getSize().width - RightMargin,VPos-1);
	//g.drawString(tmp,getSize().width - RightMargin,TopMargin);
	//g.drawString(tmp,LeftMargin-5,VPos-1);
	//g.drawString(tmp,LeftMargin-5, TopMargin);
	//g.drawString(tmp,RightMargin+230,VPos-1);
	//g.drawString(tmp,RightMargin+230,TopMargin);
	
	tmp = Y4;
	//g.drawString(tmp,LeftMargin,VPos+4);
	//g.drawString(tmp,getSize().width-RightMargin-fm.stringWidth(tmp)/2+1,VPos+4);
	
        tmp = LABELY;
	g.setFont(LabelFontITsmall);
        g.drawString(tmp,getSize().width-RightMargin-fm.stringWidth(tmp),TopMargin-5);
	
	//tmp = TITLE;
	//g.setFont(TitleFontSe);
	//g.drawString(tmp,7,4*getSize().height/9-20);
	//-------------------------------------------------------------------
        // New code for plot label with book's conventions
        //-------------------------------------------------------------------
        
        String alpha, Ohm, lambda, infinity, Gamma;
	    
	    alpha="\u03b1";
	    lambda="\u03bb";
	    Ohm="\u03a9";
	    infinity="\u221e";
	    Gamma="\uu0393";
	    
        int newx, newy;
        newx = 6; newy = 4*getSize().height/9-20;
        if(lab_flag == 1){
            MaestroG.subscripterSS("Re{Z(d)}","","",g,11,newx,newy);
        }
        else if(lab_flag == 2){
            MaestroG.subscripterSS("Im{Z(d)}","","",g,11,newx,newy);
        }
        else if(lab_flag == 3){
            MaestroG.subscripterSS("Re{Y(d)}","","",g,11,newx,newy);
        }
        else if(lab_flag == 4){
            MaestroG.subscripterSS("Im{Y(d)}","","",g,11,newx,newy);
        }
        else if(lab_flag == 5){
            MaestroG.subscripterSS("Re{"+Gamma,"d","}",g,11,newx,newy);
        }
        else if(lab_flag == 6){
            MaestroG.subscripterSS("Im{"+Gamma,"d","}",g,11,newx,newy);
        }
        else if(lab_flag == 7){
            MaestroG.subscripterSS("|"+Gamma,"d","|",g,11,newx,newy);
        }
        //---------------------------------------------------------------------
	tmp = UNITS;
        g.setFont(TitleFontSe);
	g.drawString(tmp,7,4*getSize().height/9);
	
        if (VALUE.trim().length() >= 4) {
        //System.out.println("VALUE.substring(3,4) = "+VALUE.substring(3,4));
	tmp = VALUE;
        //System.out.println("1 VALUE = '"+VALUE+"'");
        if (VALUE.substring(3,4).equals(Gamma)) {
            MaestroG.subscripterSS(VALUE.substring(0,4),"d",VALUE.substring(4),
                                   g,13,4*getSize().width/10-200,TopMargin-7);
        } else if (VALUE.substring(3,4).equals("Z")) {
            MaestroG.subscripterSS(VALUE.substring(0,4),"","(d)"+VALUE.substring(4),
                                   g,13,4*getSize().width/10-200,TopMargin-7);
        } else if (VALUE.substring(3,4).equals("Y")) {
            MaestroG.subscripterSS(VALUE.substring(0,4),"","(d)"+VALUE.substring(4),
                                   g,13,4*getSize().width/10-200,TopMargin-7);
        } else if (VALUE.substring(3,4).equals("I")) {
            MaestroG.subscripterSS(VALUE.substring(0,4),"","(d)"+VALUE.substring(4),
                                   g,13,4*getSize().width/10-200,TopMargin-7);
        } else if (VALUE.substring(3,4).equals("V")) {
            MaestroG.subscripterSS(VALUE.substring(0,4),"","(d)"+VALUE.substring(4),
                                   g,13,4*getSize().width/10-200,TopMargin-7);
        } else {
            g.drawString(tmp,4*getSize().width/10-200,TopMargin-7);
        }

        }
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
	g.setColor(Color.green);
	MaestroG.fillCircle(4*getSize().width/10-210,TopMargin-12,6,g);
	g.setColor(Color.black);
	MaestroG.drawCircle(4*getSize().width/10-210,TopMargin-12,6,g);	
	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
	/*
	if(Flag == 1){
	    g.setFont(new Font("Symbol",Font.PLAIN,12));
	    g.drawString("\u03a9",LeftMargin+fm.stringWidth(tmp)-13,TopMargin-2);
	    g.setFont(new Font("SanSerif",Font.PLAIN,12));  
	}
	else if(Flag == 2){
	    g.setFont(new Font("Symbol",Font.PLAIN,12));
	    g.drawString("\u0393",LeftMargin+20,TopMargin-2);
	    g.setFont(new Font("SanSerif",Font.PLAIN,12));
	}
	*/
    }
    
    private void drawZeroLine(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
        
	int myY;
	myY = (int)MaestroA.mapper(0,(double)TopMargin,(double)VPos,ymax,ymin);
	g.setColor(Color.red);
	if(ymin==0.0 || ymax ==0.0 || (ymax>0.0 && ymin <0.0)){
	    g.drawLine(LeftMargin+1,myY,getSize().width-RightMargin-1,myY);
	}
    }
    
    
    public synchronized void reset(){
	IsYRangeMaxSet = false;
	IsYRangeMinSet = false;
	IsXRangeMaxSet = false;
	IsXRangeMinSet = 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 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 plot(double xdata[], double ydata[]){
	N = xdata.length;
	x = new double[N];
	y = new double[N];
	xx = new int[N];
	yy = new int[N];
	for(int i = 0; i < N; i++){
	    x[i] = xdata[i];
	    y[i] = ydata[i];
	}
	ignition();
	repaint();
    }
    
    private void push(){
	if(xx.length != xx_old.length){
	    xx_old = new int[xx.length];
	    yy_old = new int[xx.length];
	}
    
	for(int i = 0; i < xx.length; i++){
	    xx_old[i] = xx[i];
	    yy_old[i] = yy[i];
	}
    }
    
     private void clean_memory(){
	if(xx.length != xx_old.length){
	    xx_old = new int[xx.length];
	    yy_old = new int[xx.length];
	}
    
	for(int i = 0; i < xx.length; i++){
	    xx_old[i] = xx[i];
	    yy_old[i] = 0;
	}
    }
    
    
    
    
    public synchronized void setTitle(String TITLE){
	this.TITLE = TITLE;
    }
    
    public synchronized void setUnits(String UNITS){
	this.UNITS = UNITS;
    }
    
    public synchronized void setLabelY(String LABELY){
	this.LABELY = LABELY;
    }
    
    public synchronized void setValue(String VALUE){
	this.VALUE = VALUE;
    }
    
    public synchronized void setFlag(int number){
	this.Flag = number;
    }

    public synchronized void setLabFlag(int number){
	this.lab_flag = number;
    }
    
    public void setXpos(double xpos){
	this.xpos = xpos;   
    }

    
    public void componentHidden(ComponentEvent evt){/*cleanUp();*/}
    public void componentMoved(ComponentEvent evt){/*cleanUp();*/}
    public void componentResized(ComponentEvent evt){/*cleanUp();*/}
    public void componentShown(ComponentEvent evt){/*cleanUp();*/}
     
}
