//=======================================================//
//  veritaban?ndan veriyi okuyup basit bir grafik ç?kt?s?                                       //
//  olarak gösteren bir program                                                                             //
//  tplot2D                                                                                                              //
//  M. Turhan Coban                                                                                              //
//=======================================================//

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.print.PrinterJob;
import java.util.*;
import java.awt.print.*;

public class tplot2D extends JPanel 
{   private static final long serialVersionUID = 832595695263L; 
	public Graphics2D g;
    final static Color bg = Color.white;
    final static Color fg = Color.black;
    final static Color red = Color.red;
    final static Color white = Color.white;
    final static BasicStroke stroke = new BasicStroke(1.0f);
    final static BasicStroke boldStroke = new BasicStroke(2.0f);
    PlotShapesSW ps;
    public double xx[],yy[][];
    public double xmin,xmax,ymin,ymax;
    int ny;  //number of data in the plot window
    int nque;
    ArrayDeque<Double> timeque;
    ArrayDeque<Double>[] xque;
    String label;
    String xlabel;
    String ylabel;
    int xntic;
    int yntic;
    int xgridon;
    int ygridon;
    int nplot[];
    Color fgcolor[];
    Color bgColor;
    // ny : number of data set
    // label : plot label
    // xlabel : xaxis label
    // ylabel : y axis label
    // xntic  : number of x axis tic
    // yntic  : number of y axis tic
    // nplot  : plot line type
   
    public tplot2D(int nyi,int nquei,
    String pl,String xl,String yl,int xnt,int ynt,int xgr,int ygr,int ipt[],Color bg,Color fg[])
    { ny=nyi;
      nque=nquei;
      label=pl;
      xlabel=xl;
      ylabel=yl;
      xntic=xnt;
      yntic=ynt;
      xgridon=xgr;
      ygridon=ygr;
      bgColor=bg;
      fgcolor=new Color[ny];
      nplot=new int[ny];
      xx=new double[nque];
      yy=new double[ny][nque];
      xmin=9.99e50;
      xmax=-9.99e50;
      ymin=9.99e50;
      ymax=-9.99e50;
      for(int i=0;i<ny;i++)
      {fgcolor[i]=fg[i];
       nplot[i]=ipt[i];
      }
    setBackground(bgColor);
    setForeground(Color.black);
    timeque=new ArrayDeque<Double>(nque);
    xque=new ArrayDeque[ny]; 

    for(int i=0;i<ny;i++)
    {xque[i]=new ArrayDeque<Double>(nque);}
	}
	                
    public void setXY(double itime,double iyy[])
    {
	  xmin=9.99e50;
      xmax=-9.99e50;
      ymin=9.99e50;
      ymax=-9.99e50;
    timeque.addLast(new Double(itime)); 
    if(timeque.size()>nque) timeque.removeFirst(); 
	for(int i=0;i<ny;i++)
      { xque[i].addLast(new Double(iyy[i]));
        if(xque[i].size()>nque) xque[i].removeFirst(); 
      } 
    Object[] a=timeque.toArray();
    Double da;

    for(int j=0;j<a.length;j++)
    {da=(Double)a[j];xx[j]=da.doubleValue();
    if(xx[j]<xmin) xmin=xx[j];
    if(xx[j]>xmax) xmax=xx[j];  
    }
    
    Object[] b;
    for(int i=0;i<ny;i++)
    {
	b=xque[i].toArray();
    Double db;
    for(int j=0;j<b.length;j++)
    {db=(Double)b[j];yy[i][j]=db.doubleValue();
     if(yy[i][j]<ymin) ymin=yy[i][j];
     if(yy[i][j]>ymax) ymax=yy[i][j];
     if(ymin==ymax) {ymin=yy[i][j]-0.1;ymax=yy[i][j]+0.1;}
    }	
    }}
    public void setXY(double itime,double iyy[],double ymini,double ymaxi)
    {
	  xmin=9.99e50;
      xmax=-9.99e50;
      ymin=ymini;
      ymax=ymaxi;
    timeque.addLast(new Double(itime)); 
    if(timeque.size()>nque) timeque.removeFirst(); 
	for(int i=0;i<ny;i++)
      { xque[i].addLast(new Double(iyy[i]));
        if(xque[i].size()>nque) xque[i].removeFirst(); 
      } 
    Object[] a=timeque.toArray();
    Double da;

    for(int j=0;j<a.length;j++)
    {da=(Double)a[j];xx[j]=da.doubleValue();
    if(xx[j]<xmin) xmin=xx[j];
    if(xx[j]>xmax) xmax=xx[j];  
    }
    
    Object[] b;
    for(int i=0;i<ny;i++)
    {
	b=xque[i].toArray();
    Double db;
    for(int j=0;j<b.length;j++)
    {db=(Double)b[j];
    yy[i][j]=db.doubleValue();
    if(yy[i][j]<ymin) yy[i][j]=ymin;
    if(yy[i][j]>ymax) yy[i][j]=ymax;
     //if(yy[i][j]<ymin) ymin=yy[i][j];
     //if(yy[i][j]>ymax) ymax=yy[i][j];
     //if(ymin==ymax) {ymin=yy[i][j]-0.1;ymax=yy[i][j]+0.1;}
    }	
    }}       
    public void drawShapes(Graphics2D g2)
    {
        g=g2;
	    //draw Plot
        Dimension d=getSize();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
        int gridWidth = d.width;
        int gridHeight = d.height;
        int rowspacing=(int)(d.width/100.0);
        int columnspacing=(int)(d.height/50.0);
        int rectWidth=gridWidth-columnspacing;
        int rectHeight=gridHeight-rowspacing;
        int xabsmin=(int)(0.25*rectWidth);
        int yabsmin=(int)(0.2*rectHeight);
        int abswidth=(int)(0.65*rectWidth);
        int absheight=(int)(0.7*rectHeight);
        int xlog=0;
        int ylog=0;
        char ch='+';
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setStroke(stroke);
        //g2.setPaint(fgcolor);
        g2.drawRect(xabsmin,yabsmin,abswidth,absheight);
        ps=new PlotShapesSW(g2,xabsmin,yabsmin,
        absheight,abswidth,xmin,xmax,ymin,ymax,xlog,ylog);
        if(xgridon!=0) ps.drawXGrid(xntic);
        if(ygridon!=0) ps.drawYGrid(yntic);
        int i,j;
        for(i=0;i<ny;i++)
           {
           //  Select plot colors
           g2.setPaint(fgcolor[i]);
           
           ps.drawPlotLine(nplot[i],xx,yy[i]);
           }//end of for(i=0	   
        g2.setPaint(fg);
        ps.drawXTic(xntic,(ymax-ymin)/80.0,0);
        ps.drawYTic(yntic,(xmax-xmin)/80.0,0);
        ps.drawXString(xntic);
        //ps.drawXNumbers(xntic);
        ps.drawYNumbers(yntic);
        ps.drawLabel(label);
        ps.drawXLabel(xlabel);
        ps.drawYLabel(ylabel);
    }       
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        drawShapes(g2); 
        repaint();      
    }
}
