/**
 * TransGraphCanvas.java
 * Graphs specific for the transmission lines
 */

/* 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.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.*;


public class TransGraphCanvas extends Canvas implements ComponentListener{
    private static final Color bgcolor = Color.gray;
       
    private double ymax, ymin, xmax, xmin;
    private boolean IsYRangeMaxSet, IsYRangeMinSet, IsXRangeMaxSet, IsXRangeMinSet; 
    private int LeftMargin=65, RightMargin=35, TopMargin=20, BottomMargin=20;
    private int WIDTH, HEIGHT;
    private String X1, X2, Y1, Y2, Y3, Y4, TITLE;
    private Image im;
    private Graphics buf;
    private int VPos = 0;
    private static final Font TitleFont = TheFonts.serif12;
    private static final Font LabelFont = TheFonts.sanSerif11;
    private static final Font LabelFontIT = TheFonts.italic12;
    private static final Font LabelFontIT2 = new Font("Serif",Font.ITALIC,14);
    private double[] x, y;
    private int[] xx, yy;
    private int[] xx_old, yy_old;
    private int N, maxY, minY;
    private double xpos;
    private boolean IsTraceOn, IsCleanUpOn, IsDynamic, IsVoltage, IsTooLarge;
    public TransGraphCanvas(){
	super();
	N = 1000;
	setBackground(bgcolor);
	xmax = 1.0;
	xmin = 0.0;
	ymax = 2.0;
	ymin = -2.0;
	X1 = " l"; // lower case L, not a "one"
	X2 = "0";
	Y1 = "  ";
	Y2 = "  ";
	TITLE = "  ";
	IsVoltage = true;
	IsYRangeMaxSet = false;
	IsYRangeMinSet = true;
	IsXRangeMaxSet = false;
	IsXRangeMinSet = false;
	IsTraceOn = false;
	IsCleanUpOn = false;
	IsDynamic = false;
        IsTooLarge = 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;
	}
	xpos = xmin;
	//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 drawGraph(Graphics g){
	if(!IsDynamic){
	    g.clearRect(0,0,getSize().width-1,getSize().height-1);
	}
	drawAxis(g);
	labelDetect();
	drawLabels(g);
	
	drawPoints(g,1);
	drawZeroLine(g);
        
        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]; }
	
        if(ymax <0.0 && ymin < 0.0){ymax = 0.0;}
        
	//Confine data
	MaestroA.confiner(x,xmax,xmin);
	MaestroA.confiner(y,ymax,ymin);
	
        maxY = 0; minY = 150;
                
	//Reverse Mapping
	for(int i = 0; i < xx.length; i++){
	    if(ymax==0.0 && ymin ==0.0){
		yy[N-1-i] = VPos;
	    }
	    else{
		yy[N-1-i] = (int)MaestroA.mapper(y[i],(double)TopMargin+1,(double)(getSize().height-BottomMargin)-1,ymax,ymin);
	    }
	    xx[N-1-i] = (int)MaestroA.mapper(x[i],(double)LeftMargin+1,(double)(getSize().width-RightMargin),xmax,xmin);
	    
	}
        for(int i = 0; i < xx.length; i++){
            if(yy[i]<minY){minY = yy[i];}
            if(yy[i]>maxY){maxY = yy[i];}
            
        }
    }
    
    private void drawPoints(Graphics g, int tipo){
        Graphics2D g2d = (Graphics2D)g;

	switch(tipo){
	    case 1:
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		if(IsTraceOn){
		    g.setColor(Color.green);
		    g.drawPolyline(xx,yy,xx.length);
		    g.setColor(Color.black);
		    g.drawPolyline(xx_old,yy_old,xx_old.length);
		}
		else{
		    if(IsVoltage){
			g.setColor(Color.yellow);
			if(!IsTooLarge){
                            //g.drawPolyline(xx,yy,xx.length);
                            if (xx.length >= N && yy.length >=N) {
                            for(int i=0;i<N-1;i++){
                                g.drawLine(xx[i],yy[i],xx[i+1],yy[i+1]);
                            }    
                            }
                        }
                        else{
                            g.setColor(Color.lightGray);
                            g.fillRect(xx[0],minY,xx[N-1]-xx[0],maxY-minY);
                            g.setColor(Color.yellow);
                            g.drawRect(xx[0],minY,xx[N-1]-xx[0],maxY-minY);
                        }
		    }
		    else{
			g.setColor(Color.white);
			//g.drawPolyline(xx,yy,xx.length);
		    }
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
		}
		break;
	    case 2:
		g.setColor(bgcolor);
		//g.drawPolyline(xx_old,yy_old,xx_old.length);
		break;
	}
    }
       
    private void drawAxis(Graphics g){
	VPos = getSize().height - BottomMargin;
	WIDTH = getSize().width - LeftMargin -RightMargin;
	HEIGHT = VPos - TopMargin;
	g.setColor(Color.white);
	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,VPos,LeftMargin-5,VPos);
        
    }
    
    private void labelDetect(){
	double tempmax=0.0;
	double tempmin=0.0;
	//tempmax = ymax;
	if(Math.max(ymax,Math.abs(ymin))==0.0){
	    tempmax = 0.0;
	    tempmin = 0.0;
	}
	else if(Math.max(ymax,Math.abs(ymin))>=1.0 && Math.max(ymax,Math.abs(ymin))<1.0E3|| Math.max(ymax,Math.abs(ymin))==0.0){
	    tempmax = ymax;
	    tempmin = ymin;
	}
	else if(Math.max(ymax,Math.abs(ymin))<1.0E6 && Math.max(ymax,Math.abs(ymin))>=1.0E3){
	    tempmax = ymax*1.0E-3;
	    tempmin = ymin*1.0E-3;
	}
	else if(Math.max(ymax,Math.abs(ymin))>=1.0e6){
	    tempmax = ymax*1.0E-6;
	    tempmin = ymin*1.0E-6;
	}
	else if(Math.max(ymax,Math.abs(ymin))<1.0 && Math.max(ymax,Math.abs(ymin))>=1.0E-3){
	    tempmax = ymax*1.0E3;
	    tempmin = ymin*1.0E3;
	}
	else if(Math.max(ymax,Math.abs(ymin))<1.0E-3 && Math.max(ymax,Math.abs(ymin))>=1.0e-6){
	    tempmax = ymax*1.0E6;
	    tempmin = ymin*1.0E6;
	}
	else if(Math.max(ymax,Math.abs(ymin))<1.0E-6 && Math.max(ymax,Math.abs(ymin))>=1.0e-9){
	    tempmax = ymax*1.0E9;
	    tempmin = ymin*1.0E9;
	}
	else if(Math.max(ymax,Math.abs(ymin))<1.0E-9 && Math.max(ymax,Math.abs(ymin))>=1.0E-12){
	    tempmax = ymax*1.0E12;
	    tempmin = ymin*1.0E12;
	}
	else if(Math.max(ymax,Math.abs(ymin))<1.0E-12 && Math.max(ymax,Math.abs(ymin))>=1.0e-15){
	    tempmax = ymax*1.0E15;
	    tempmin = ymin*1.0E15;
	}
	else if(Math.max(ymax,Math.abs(ymin))<1.0E-15){
	    tempmax = ymax*1.0E18;
	    tempmin = ymin*1.0E18;
	}
		
	Y1 = String.valueOf(MaestroA.rounder(tempmax,3))+" ";
	//Y1 = String.valueOf(MaestroA.rounder(ymax,5))+" ";
	Y2 = String.valueOf(MaestroA.rounder(tempmin,3))+" ";
	Y3 = "_";
	Y4 = "|";

    }
    
    private void drawLabels(Graphics g){
	String tmp;
	g.setFont(LabelFont);
	g.setColor(Color.white);
	g.clearRect(0,0,LeftMargin-1,getSize().height-1);
	g.clearRect(0,0,getSize().width-1,TopMargin-1);
	FontMetrics fm = g.getFontMetrics();
	tmp = X1;
        g.setFont(LabelFontIT2);
        fm = g.getFontMetrics();
        // CHANGE ==============================================================
	//g.drawString(tmp,LeftMargin-1,VPos+fm.getHeight()+2);
        MaestroG.subscripterIT2(tmp,"","", g, 14, LeftMargin-1,VPos+fm.getHeight()+2);
        //======================================================================
        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-5);
	
	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);
	tmp = Y4;
	//g.drawString(tmp,LeftMargin,VPos+4);
	//g.drawString(tmp,getSize().width-RightMargin-fm.stringWidth(tmp)/2+1,VPos+4);
	
	tmp = TITLE;
	g.setFont(TitleFont);
	//g.drawString(tmp,LeftMargin+(WIDTH-fm.stringWidth(tmp))/2,TopMargin-fm.getHeight()/2);
	g.drawString(tmp,LeftMargin+40,TopMargin-8);
    }
    
    private void drawZeroLine(Graphics g){
	int myY;
	myY = (int)MaestroA.mapper(0,(double)TopMargin,(double)VPos,ymax,ymin);
	if(ymax == 0.0 && ymin == 0.0){myY=VPos;}
	g.setColor(Color.red);
	if(ymin == 0.0){
            
        }
        else{
	    g.drawLine(LeftMargin+1,myY,getSize().width-RightMargin-1,myY);
        }
    }
    /*
    public static double mapper(double x, double ymax, double ymin, double xmax, double xmin){
	double a, b;
	//if(xmax == xmin){return xmin;}
	a = (ymax-ymin)/(xmax-xmin);
	b = ymax - a * xmax;
	return a*x+b;
    }
    */
    
    public synchronized void reset(){
	IsYRangeMaxSet = false;
	IsYRangeMinSet = true;
	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 synchronized void IsTooLarge(boolean IsTooLarge){
	this.IsTooLarge = IsTooLarge;
    }

   /* public void update(Graphics g){
	if(!IsDynamic){
	    super.update(g);
	    return;
	}
	if(IsDynamic && IsTraceOn){g.clipRect(LeftMargin+1,TopMargin+1,WIDTH-1,HEIGHT-1);}
	if(IsCleanUpOn){
	    //g.clearRect(0,0,getSize().width-1,getSize().height-1);
	    g.clearRect(LeftMargin+1,TopMargin+1,WIDTH-1,HEIGHT-1);
	    IsCleanUpOn = false;
	}
	drawZeroLine(g);
	if(!IsTraceOn){
	    drawPoints(g,2);//This cleans the previous graph
	}
	drawPoints(g,1);//This draws the new graph
	push();
    }
    */
    
    
    public void update(Graphics g){		// added to avoid clearing
	paint(g);
}

    
    
    
    
    public void drawRef(Graphics 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;
		
		g.setColor(Color.green);
		g.drawLine(tx1,ty1,tx2,ty2);
          }
    }	

    
    
    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 void componentHidden(ComponentEvent evt){/*cleanUp();*/}
    public void componentMoved(ComponentEvent evt){/*cleanUp();*/}
    public void componentResized(ComponentEvent evt){/*cleanUp();*/}
    public void componentShown(ComponentEvent evt){/*cleanUp();*/}
     
    public double getXpos(){
	return xpos;
    }
    
    public synchronized void setXpos(double xpos){
	this.xpos = xpos;
    }
    
    public synchronized void setIsVoltage(boolean IsVoltage){
	this.IsVoltage = IsVoltage;
    }
    
}
