//package maestro.lib.graphics;
//GraphCanvas.java

import java.awt.*;
import java.util.*;
import java.lang.*;
//import maestro.lib.graphics.*;
//import maestro.lib.math.*;

public class GraphCanvasQ extends Canvas{
    protected static final Color bgcolor = new Color(216,216,191);
    protected static final Color colorp = new Color(255,255,255);
    protected Image im;
    protected Graphics buf;
    protected final int LeftMargin = 40;
    protected final int TopSep = 40;
    protected final int BottomSep = 40;
    protected final int RightMargin = 40;
    protected int VPos=0;
    protected String XLabel, YLabel, TITLE;
    protected String X1, X2, X3;
    protected String Y1, Y2, Y3;
    protected int NumPoints;
    protected double x[];
    protected double y[];
    protected double ymin, ymax, currentNormalization, currentMIN, currentin;
    protected double xmin, xmax;
    protected int xx[];
    protected int yy[];
    protected double xRef;
    protected boolean Should_Plot_Zero_Line;
    protected boolean Should_Plot_Ref_Point;
    protected boolean IsYRangeMinSet, IsYRangeMaxSet;
    protected boolean IsXRangeMinSet, IsXRangeMaxSet;
    protected static final Font font1 = new Font("SanSerif",Font.PLAIN,11);
    protected static final Font font2 = new Font("Serif",Font.PLAIN,11);
    public GraphCanvasQ(){
	super();
	setBackground(bgcolor);
	XLabel="x-axis";
	YLabel="y-axis";
	TITLE ="Unknown Title";
	Y1 = " ";
	Y2 = " ";
	Y3 = " ";
	X1 = " ";
	X2 = " ";
	X3 = " ";
	NumPoints = 1001;
	x = new double[NumPoints];
	y = new double[NumPoints];
	xx = new int[NumPoints];
	yy = new int[NumPoints];
	for(int i=0;i<NumPoints;i++){
	    x[i]=i;
	    y[i]=0.0;
	}
	IsYRangeMinSet=false;
	IsYRangeMaxSet=false;
	IsXRangeMinSet=false;
	IsXRangeMaxSet=false;
	
	xRef = 0.0;
	
	Should_Plot_Zero_Line = false;
	Should_Plot_Ref_Point = false;
	
	currentNormalization = 1.0;
	currentMIN = 1.0;
    }
     
public void paint(Graphics g){
    if(im == null){
	im = createImage(getSize().width,getSize().height);
	buf = im.getGraphics();
	drawGraph(buf);
    }
    drawGraph(buf);
    g.drawImage(im,0,0,null);
}

public void drawGraph(Graphics g){
	g.clearRect(0,0,getSize().width,getSize().height);
	g.setColor(Color.black);
	g.drawRect(0,0,getSize().width,getSize().height);
	g.setColor(Color.white);
	g.drawRect(1,1,getSize().width-3,getSize().height-3);
    	drawAxis(g);
	if(Should_Plot_Zero_Line){plotZeroLine(g);}
	//g.draw3DRect(0,0,getSize().width-1,getSize().height-1,false);
	drawTitle(g);
        plotPoints(g);
	//plotRefPoint(g);
	
}

protected void ignition(){
    int height=getSize().height;
    int width=getSize().width;
    if(!IsYRangeMinSet){ymin = MaestroA.getMin(y);}
    if(!IsYRangeMaxSet){ymax = MaestroA.getMax(y);}
    if(!IsXRangeMaxSet){xmax = x[x.length-1];}
    if(!IsXRangeMinSet){xmin = x[0];}
    if(IsYRangeMinSet || IsYRangeMaxSet){
	MaestroA.confiner(y,ymax,ymin);
    }
    if(IsXRangeMinSet || IsXRangeMaxSet){
	MaestroA.confiner(x,xmax,xmin);
    }
    
    if(ymin==ymax){ymax=2*ymin+1;}
    for(int i=0;i<xx.length;i++){
	xx[i]= (int)MaestroA.mapper(x[i],(double)(width-RightMargin),(double)(LeftMargin),xmax,xmin);
	yy[i]= (int)MaestroA.mapper(y[i],(double)TopSep,(double)(height-BottomSep),ymax,ymin);
    } 
    
    X1="- 0.5 Length";
    X2="0.5 Length";
    X3="";
    Y1=""+MaestroA.rounder(ymin,3);
    
    if(ymax < 1.0e230){
	//Y2="| Imax | = "+MaestroA.rounder(ymax*currentNormalization,6);
	//Y2= ""+currentin;
	Y2="| Imax | = "+MaestroA.rounder(currentin,6);
    }
    else{
	Y2="\u221e";
    }
    Y3="_";
    
}

protected void plotZeroLine(Graphics g){
    if(ymin*ymax>0){return;}
    FontMetrics fm = g.getFontMetrics();
    g.setFont(font1);
    int yy;
    yy = (int)MaestroA.mapper(0.0,(double)TopSep,(double)(getSize().height-BottomSep),ymax,ymin);
    g.setColor(Color.red);
    g.drawLine(LeftMargin-5,yy,getSize().width-RightMargin,yy);
    g.setColor(Color.white);
    g.drawString("0",LeftMargin-fm.stringWidth("0")-13,yy+fm.getHeight()/3);
    g.setColor(colorp);
}

public synchronized void plotZeroLine(boolean Should_Plot_Zero_Line){
    this.Should_Plot_Zero_Line = Should_Plot_Zero_Line;
}

protected void plotRefPoint(Graphics g){
    int myX=0, myY=0, i;
    i = (int)((NumPoints-1)*(xRef-x[0])/(x[x.length-1]-x[0]));
    if(i<0) {i = 0;}
    if(i>=NumPoints){i=NumPoints-1;}
    myX = (int)MaestroA.mapper(xRef,(double)(getSize().width-RightMargin),(double)(LeftMargin),xmax,xmin);
    if(y[i]>0.0){
	myY = (int)MaestroA.mapper(y[i],(double)TopSep,(double)(getSize().height-BottomSep),ymax,ymin);
    }
    else{
	myY =getSize().height-BottomSep ;
    }
    
    Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
    g.setColor(Color.yellow);
    g.drawLine(myX,myY,myX,getSize().height-BottomSep+5);
    
    g.setColor(Color.red);
    MaestroG.fillCircle(myX,myY,6,g);
    
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    
}


public synchronized void plotRefPoint(boolean Should_Plot_Ref_Point){
    this.Should_Plot_Ref_Point = Should_Plot_Ref_Point;
}

protected void plotPoints(Graphics g){
    ignition();
    
    Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
    //g.drawPolyline(xx,yy,xx.length);
	g.setColor(Color.white);
	for(int i=0;i<NumPoints-1;i++){
		    g.drawLine(xx[i],yy[i],xx[i+1],yy[i+1]);
	}
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    
}

protected void drawTitle(Graphics g){
    FontMetrics fm = g.getFontMetrics();
    g.setFont(font1);
    
    g.setColor(Color.white);
    g.drawString(XLabel,getSize().width-RightMargin+3*fm.stringWidth(XLabel)-5,getSize().height-BottomSep-8);
    //g.drawString(YLabel,LeftMargin-33,getSize().height/3);
    
    g.drawString(TITLE,(getSize().width-fm.stringWidth(TITLE))/2,TopSep/2-3);
    g.setColor(Color.yellow);
    MaestroG.subscripterS("I","max"," = "+currentin+" [ A ]",g,14,10,20);
    g.drawString("_",20,getSize().height-BottomSep-2);
    g.drawString("+",20,TopSep+5);
    
    
    
    g.setColor(Color.yellow);
    
    //g.drawString(X1,LeftMargin-12,getSize().height-BottomSep+3*fm.getHeight()/2);
    //g.drawString(X2,getSize().width-RightMargin-fm.stringWidth(X2)+12,getSize().height-BottomSep+3*fm.getHeight()/2);
    
    MaestroG.subscripterL("-0.5","","Length",g,12,LeftMargin-12,getSize().height-BottomSep+3*fm.getHeight()/2);
    MaestroG.subscripterL("0.5","","Length",g,12,getSize().width-RightMargin-fm.stringWidth(X2)+12,getSize().height-BottomSep+3*fm.getHeight()/2);

    //g.drawString(X3,LeftMargin,getSize().height-BottomSep+5);
    //g.drawString(X3,LeftMargin+(getSize().width-RightMargin-LeftMargin)/2,getSize().height-BottomSep+5);
    
    g.setFont(font1);
    g.drawString("0",LeftMargin+(getSize().width-RightMargin-LeftMargin)/2-2,getSize().height-BottomSep+3*fm.getHeight()/2);
    
    //g.drawString(X3,getSize().width-RightMargin,getSize().height-BottomSep+5);
    //g.drawString(Y1,Math.max(4,LeftMargin-fm.stringWidth(Y1))-7,getSize().height-BottomSep+5);
            
    //g.drawString(Y3,LeftMargin-6,getSize().height-BottomSep-1);
    //g.drawString(Y3,LeftMargin-6,TopSep-1);
    
    g.drawLine(LeftMargin,getSize().height-BottomSep,LeftMargin,getSize().height-BottomSep+6);
    g.drawLine(LeftMargin+(getSize().width-RightMargin-LeftMargin)/2,getSize().height-BottomSep,
               LeftMargin+(getSize().width-RightMargin-LeftMargin)/2,getSize().height-BottomSep+6);
    g.drawLine(getSize().width-RightMargin,getSize().height-BottomSep,
               getSize().width-RightMargin,getSize().height-BottomSep+6);
    g.drawLine(LeftMargin-6,getSize().height-BottomSep,LeftMargin,getSize().height-BottomSep);
    g.drawString(Y3,LeftMargin-6,TopSep-1);
    
    g.setColor(colorp);
}

protected void drawAxis(Graphics g){
    VPos = getSize().height-BottomSep;
    g.setColor(Color.lightGray);
    g.drawLine(LeftMargin+(getSize().width-RightMargin-LeftMargin)/2,getSize().height-BottomSep,LeftMargin+(getSize().width-RightMargin-LeftMargin)/2,TopSep-5);
    
    g.setColor(Color.yellow);
    //Vertical Axis
    g.drawLine(LeftMargin,TopSep-1,LeftMargin,getSize().height-BottomSep);
    //g.drawLine(LeftMargin,TopSep/2,LeftMargin,getSize().height-BottomSep);
    //MaestroG.drawArrow(LeftMargin,TopSep/2,1,g);
    //Horizontal Axis
    g.drawLine(LeftMargin,VPos,getSize().width-RightMargin+10,VPos);
    MaestroG.drawArrow(getSize().width-RightMargin+10,VPos,7,g);
    g.drawLine(getSize().width-RightMargin,getSize().height-BottomSep,getSize().width-RightMargin,TopSep-1);
    //g.drawString(X3,getSize().width-RightMargin,getSize().height-BottomSep+5);
}

public final synchronized void setXLabel(String XLabel){
    this.XLabel=XLabel;
}

public final synchronized void setYLabel(String YLabel){
    this.YLabel=YLabel;
} 

public final synchronized void setTitle(String TITLE){
    this.TITLE = TITLE;
}

public final synchronized void setLabels(String TITLE, String YLabel, String XLabel){
    this.TITLE=TITLE;
    this.YLabel = YLabel;
    this.XLabel = XLabel;
}
 
public void plot(double[] xdata, double[] ydata, double currentNormalization, double currentMIN, double currentin){
    if(NumPoints != xdata.length){
	NumPoints = xdata.length;
	x = new double[NumPoints];
	y = new double[NumPoints];
	xx = new int[NumPoints];
	yy = new int[NumPoints];
    }
    for(int i = 0; i < NumPoints; i++){
	x[i] = xdata[i];
	y[i] = ydata[i];
    }
    this.currentMIN = currentMIN;
    this.currentNormalization = currentNormalization;
    this.currentin = currentin;
    repaint();
}

public void setYRange(double ymax, double ymin){
    IsYRangeMaxSet=true;
    IsYRangeMinSet=true;
    this.ymax=ymax;
    this.ymin=ymin;
}

public void setYRangeMin(double ymin){
    IsYRangeMinSet=true;
    this.ymin=ymin;
}

public void setYRangeMax(double ymax){
    IsYRangeMaxSet=true;
    this.ymax=ymax;
}

public void setAuto(){
    IsYRangeMaxSet=false;
    IsYRangeMinSet=false;
    IsXRangeMaxSet=false;
    IsXRangeMinSet=false;
}

public synchronized void setRefPoint(double xRef){
    this.xRef = xRef;
}

public void update(Graphics g){
    paint(g);
}


}