import java.awt.*;
//import java.util.*;
//import java.lang.*;

public class GraphCanvasA extends Canvas{
    protected static final Color bgcolor = new Color(216,216,191);
    protected Image im;
    protected Graphics buf;
    protected int LeftMargin = 60;
    protected int TopSep = 40;
    protected int BottomSep = 40;
    protected 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;
    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 fontYLabel = new Font("Serif",Font.PLAIN,11);
    protected Color ColorHeader = Color.magenta.darker();
    private double sfactor = 1.0;
    protected int s11;
    
    public GraphCanvasA(){
	super();
        
        LeftMargin = (int)Math.ceil(60*sfactor);
        TopSep = (int)Math.ceil(40*sfactor);
        BottomSep = (int)Math.ceil(40*sfactor);
        RightMargin = (int)Math.ceil(40*sfactor);
        s11 = (int)Math.ceil(11*sfactor);
        
	setBackground(bgcolor);
	XLabel="x-axis";
	YLabel="y-axis";
	TITLE ="Unknown Title";
	Y1 = " ";
	Y2 = " ";
	Y3 = " ";
	X1 = " ";
	X2 = " ";
	X3 = " ";
	NumPoints = 901;
	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;
    }
     
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);
    	drawAxis(g);
	g.draw3DRect(0,0,getSize().width-1,getSize().height-1,false);
	
        plotPoints(g);
	plotRefPoint(g);
	drawTitle(g);
        if(ymin >= -0.000001){// || ymax <= 0.00001){
            
        }
        else{
            if(Should_Plot_Zero_Line){plotZeroLine(g);}
        }
}

protected void ignition(){
    int height=getSize().height;
    int width=getSize().width;
    //if(!IsYRangeMinSet){ymin = MaestroA.getMin(y);}
    //if(!IsYRangeMaxSet){ymax = MaestroA.getMax(y);}
    
    //ymin = MaestroA.getMin(y);
    //ymin = MaestroA.getMax(y);
    
        double min;
	min = 0.0;
	for(int i=1;i<x.length;i++){
	    if(min>y[i]) {min = y[i];}
	}	
        ymin = min;

        double max;
	max = 0.0;
	for(int i=1;i<x.length;i++){
	    if(max<y[i]) {max = y[i];}
	}	
        ymax = max;
        
        if(Math.abs(ymax)< 1.0e-8 && Math.abs(ymin)<1.0e-8 ){
            ymax = 1.0;
            ymin = 0.0;
            
        }
        
    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);
    }
    
    
    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=""+MaestroA.rounder(x[0],3)+"\u00ba";
    X2=""+MaestroA.rounder(x[x.length-1]/100,3)+"\u00ba";
    X3="|";
    Y1=""+MaestroA.rounder(ymin,3);
    Y2=""+MaestroA.rounder(ymax,3);
    Y3="_";
}

protected void plotZeroLine(Graphics g){
    if(ymin*ymax>0){return;}
    FontMetrics fm = g.getFontMetrics();
    g.setFont(new Font("SanSerif",Font.PLAIN,s11));
    int yy;
    yy = (int)MaestroA.mapper(0.0,(double)TopSep,(double)(getSize().height-BottomSep),ymax,ymin);
    g.setColor(Color.red);
    g.drawLine(LeftMargin,yy,getSize().width-RightMargin,yy);
    if( ymax <= 0.00001){}
    else{g.drawString("0",LeftMargin-fm.stringWidth("0"),yy+fm.getHeight()/3);
    }
    g.setColor(Color.black);
}

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, myY0=0, i;
    i = (int)(NumPoints*xRef*10/(x[x.length-1]-x[0]));
    if(i<0) {i = 0;}
    if(i>=NumPoints){i=NumPoints-1;}
    myX = (int)MaestroA.mapper(xRef*10,(double)(getSize().width-RightMargin),(double)(LeftMargin),xmax,xmin);
    myY = (int)MaestroA.mapper(y[i],(double)TopSep,(double)(getSize().height-BottomSep),ymax,ymin);
    myY0 = (int)MaestroA.mapper(0.0,(double)TopSep,(double)(getSize().height-BottomSep),ymax,ymin);
    g.setColor(Color.red);
    
    Graphics2D g2d = (Graphics2D)g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    
    g.setColor(Color.white);
    g.drawLine(myX,myY,myX,myY0);
    g.setColor(Color.red);
    MaestroG.fillCircle(myX,myY,(int)Math.ceil(6*sfactor),g);
    g.setColor(Color.black);
    MaestroG.drawCircle(myX,myY,(int)Math.ceil(6*sfactor),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.gray);
    //int step = 1;
    //for(int i = 0; i < NumPoints-step; i+=step){
	//g2d.drawLine(xx[i],yy[i],xx[i],yy[i]);
        //MaestroG.drawLineThick(g2d, xx[i], yy[i], xx[i+step], yy[i+step], 2, Color.black);
    //}    
    //g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    
    ignition();
    Graphics2D g2d = (Graphics2D)g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    //g.drawPolyline(xx,yy,xx.length);
    
    //for(int i=0;i<NumPoints-1;i++){ 
	//	g.drawLine(xx[i],yy[i],xx[i+1],yy[i+1]);
    //}
    
    int thick_plot = (int)Math.ceil(1*sfactor);
    
    for(int i=0;i<NumPoints-1;i++){
            MaestroG.drawLineThick(g,xx[i],yy[i],xx[i+1],yy[i+1],thick_plot,Color.gray);
    }
    
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
}

protected void drawTitle(Graphics g){
    FontMetrics fm = g.getFontMetrics();
    
    Graphics2D g2d = (Graphics2D)g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    g.setColor(ColorHeader);
    g.setFont(new Font("Serif",Font.BOLD,s11));
    g.drawString(YLabel,(int)Math.ceil(10*sfactor),(int)Math.ceil(20*sfactor));
    g.setFont(new Font("Serif",Font.BOLD,s11));
    g.setColor(Color.red);
    g.drawString(XLabel,(getSize().width-fm.stringWidth(XLabel))/2,getSize().height-BottomSep+3*fm.getHeight()/2);
    g.setColor(ColorHeader);
    g.drawString(TITLE,(getSize().width-fm.stringWidth(TITLE))/2,TopSep/2);
    g.setFont(new Font("SanSerif",Font.PLAIN,s11));
    g.setColor(Color.black);
    g.drawString(X1,LeftMargin-(int)Math.ceil(6*sfactor),getSize().height-BottomSep+3*fm.getHeight()/2);
    g.drawString(X2,getSize().width-RightMargin-fm.stringWidth(X2)+(int)Math.ceil(15*sfactor),getSize().height-BottomSep+3*fm.getHeight()/2);
    g.drawLine(getSize().width-RightMargin,getSize().height-BottomSep,getSize().width-RightMargin,getSize().height-BottomSep+(int)Math.ceil(6*sfactor));
    g.drawLine(LeftMargin-(int)Math.ceil(6*sfactor),TopSep-(int)Math.ceil(1*sfactor),LeftMargin,TopSep-(int)Math.ceil(1*sfactor));
            
    g.drawString(Y1,Math.max((int)Math.ceil(4*sfactor),LeftMargin-fm.stringWidth(Y1))-(int)Math.ceil(12*sfactor),getSize().height-BottomSep+(int)Math.ceil(5*sfactor));
    g.drawString(Y2,Math.max((int)Math.ceil(4*sfactor),LeftMargin-fm.stringWidth(Y2))-(int)Math.ceil(12*sfactor),TopSep+(int)Math.ceil(4*sfactor));
    
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
    g.setColor(Color.black);
}

protected void drawAxis(Graphics g){
    VPos = getSize().height-BottomSep;
    g.setColor(Color.black);
    //Vertical Axis
    g.drawLine(LeftMargin,TopSep/2,LeftMargin,getSize().height-BottomSep+6);
    MaestroG.drawArrow(LeftMargin,TopSep/2,5,g);
    //Horizontal Axis
    g.drawLine(LeftMargin-(int)Math.ceil(6*sfactor),VPos,getSize().width-RightMargin+(int)Math.ceil(10*sfactor),VPos);
    MaestroG.drawArrow(getSize().width-RightMargin+10,VPos,7,g);
}

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, Color colore){
    this.TITLE = TITLE;
    this.ColorHeader = colore;
}

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){
    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];
    }
    repaint();
}

public void setSfactor(double sfactor){
    this.sfactor = sfactor;
    this.LeftMargin = (int)Math.ceil(60*sfactor);
    this.TopSep = (int)Math.ceil(40*sfactor);
    this.BottomSep = (int)Math.ceil(40*sfactor);
    this.RightMargin = (int)Math.ceil(40*sfactor);
    this.s11 = (int)Math.ceil(11*sfactor);
        
}

public void setNoRange(boolean decide){
    this.IsYRangeMaxSet=decide;
    this.IsYRangeMinSet=decide;
}

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);
}


}
