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

public class Plot2D extends JPanel implements Printable
{  private static final long serialVersionUID = 52260969731900357L;
	public Graphics2D g;
	public Plot p1;
    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;
    double xx[],yy[];
    int xn;
    Dimension d;
    int dx;
    int dy;
    public Plot2D(Plot pi)
    {
	d=getSize();
	dx = d.width;
    dy = d.height;
	//Initialize drawing colors
    setBackground(Color.white);
    setForeground(Color.black);
    //read data from file Plot data previously defined 
    p1=pi;
    xx=new double[200];
    yy=new double[200];
    xn=0;
	}
	
	public Plot2D()
    {
	d=getSize();
	dx = d.width;
    dy = d.height;
	//Initialize drawing colors
    setBackground(Color.white);
    setForeground(Color.black);
    //read data from file Plot data previously defined 
    try{p1=new Plot();
       } catch(IOException ioe) {System.err.println("IOExceptionin opening plot");}
    xx=new double[200];
    yy=new double[200];
    xn=0;
	}
	
    public Plot2D(String pl,String xl,String yl,int xnt,int ynt
    ,int xgo,int ygo,String ifn[],int ipt[],int ir[],int ig[]
    ,int ib[])
    {
     //input variables 
     // pl : plot label
     // xl : x axis label
     // yl : y axis label
     //xnt : x axis tic (0=off, any other:on)
     //ynt : y axis tic (0=off, any other:on)
     //xgo : x axis grid on (0=off, any other:on)
     //ygo : y axis grid on (0=off, any other:on)
     //ifn :file name array variable
     //ipt : plot type array variable
     //ir  : color red component array variable
     //ig  : color green componenet array variable
        //Initialize drawing colors
        setBackground(Color.white);
        setForeground(Color.black);
        try{
             p1=new Plot(pl,xl,yl,xnt,ynt,xgo,ygo,ifn,ipt,ir,ig,ib);
           } catch(IOException ioe) {System.err.println("IOExceptionin opening plot");}
    xx=new double[200];
    yy=new double[200];
    xn=0;
    }       
          
    public void setXY(double ixx[],double iyy[],int ixn)
    {
    xn=ixn;  
	for(int i=0;i<xn;i++)
      {xx[i]=ixx[i];yy[i]=iyy[i];}    
	}
    
    public void yenidenPlotDatasiOku()
    {
        try{
             p1=new Plot();
           } catch(IOException ioe) {System.err.println("IOExceptionin opening plot");}
    }
    
    public void yenidenPlotDatasiOku(Plot pi)
    {    
             p1=pi;
     }
    
    public void plotVerisiEkle(double xi[],double yi[],int iplottype,int ir,int ig,int ib)
    {
       p1.addData(xi,yi,iplottype,ir,ig,ib);
    }
    
    public void plotVerisiEkle(double xi[][],double yi[][],int iplottype[],int ir[],int ig[],int ib[])
    {
       p1.addData(xi,yi,iplottype,ir,ig,ib);
    }
    public void plotVerisiEkle(double xi[][],double yi[][])
    {  int iplottype[]=new int[xi.length];
       int ir[]=new int[xi.length];
       int ig[]=new int[xi.length];
       int ib[]=new int[xi.length];
	   p1.addData(xi,yi,iplottype,ir,ig,ib);}
    
    public void plotVerisiEkle(f_x f,double xmin,double xmax,int N,int iplottype,int ir,int ig,int ib)
    {
       p1.addFunction(f,xmin,xmax,N,iplottype,ir,ig,ib);
    }
        
    public void yenidenPlotDatasiOku(String pl,String xl,String yl,int xnt,int ynt
    ,int xgo,int ygo,String ifn[],int ipt[],int ir[],int ig[]
    ,int ib[])
    {
     //input variables 
     // pl : plot label
     // xl : x axis label
     // yl : y axis label
     //xnt : x axis tic (0=off, any other:on)
     //ynt : y axis tic (0=off, any other:on)
     //xgo : x axis grid on (0=off, any other:on)
     //ygo : y axis grid on (0=off, any other:on)
     //ifn :file name array variable
     //ipt : plot type array variable
     //ir  : color red component array variable
     //ig  : color green componenet array variable
     //ib  : color blue componenet
        //Initialize drawing colors
        setBackground(Color.white);
        setForeground(Color.black);
        try{
             p1=new Plot(pl,xl,yl,xnt,ynt,xgo,ygo,ifn,ipt,ir,ig,ib);
           } catch(IOException ioe) {System.err.println("IOExceptionin opening plot");}
    }       
    
    public void yenidenciz()
    {
    repaint();
    }

    public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
        
	    if (pi >= 1) {
                     return Printable.NO_SUCH_PAGE;
                     }
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        AffineTransform origTransform = g2.getTransform();
        g2.scale(0.5,0.5);
        drawShapes(g2);
        g2.setTransform(origTransform);   
        return Printable.PAGE_EXISTS;
    }

    
    public void drawShapes(Graphics2D g2)
    {
        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;
        p1.xabsmin=(int)(0.25*rectWidth);
        p1.yabsmin=(int)(0.2*rectHeight);
        p1.abswidth=(int)(0.65*rectWidth);
        p1.absheight=(int)(0.7*rectHeight);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
        g2.setStroke(stroke);
        g2.setPaint(fg);
        //g2.drawRect(0,0,dx,dy);
        g2.drawRect(p1.xabsmin,p1.yabsmin,p1.abswidth,p1.absheight);
        ps=new PlotShapesSW(g2,p1.xabsmin,p1.yabsmin,
        p1.absheight,p1.abswidth,p1.xmin,p1.xmax,p1.ymin,p1.ymax,p1.xlog,p1.ylog);
        if(p1.xgridon!=0)
           ps.drawXGrid(p1.xntic);
        if(p1.ygridon!=0)
           ps.drawYGrid(p1.yntic);
        int i,j;
        ps.drawMouseTic(xx,yy,xn);
        for(i=0;i<p1.nline;i++)
           {
           //  Select plot colors
           g2.setPaint(new Color(p1.red[i],p1.green[i],p1.blue[i]));
           ps.drawPlotLines(i,p1.plottype,p1.x,p1.y,p1.n,p1.ch);
           }//end of for(i=0
        for(i=0;i<5;i++)
        {
	    if(p1.label_at_xy[i]!="") ps.drawAdditionalLabel(p1.label_at_xy[i],p1.label_x[i],p1.label_y[i]);
	    }
	    if(p1.skutu1[0]!="") {
		   p1.dx1=(p1.xmax-p1.xmin)/30.0;
		   p1.dy1=(p1.ymax-p1.ymin)/30.0;  
		   ps.drawString(p1.skutu1,p1.plottype,p1.x1,p1.y1,p1.dx1,p1.dy1);}
	    if(p1.skutu2[0]!="") 
	    {
		   p1.dy2=(p1.ymax-p1.ymin)/30.0;
		   ps.drawString(p1.skutu2,p1.x2,p1.y2,p1.dy2); 
	    }   
        g2.setPaint(fg);
        ps.drawXTic(p1.xntic,(p1.ymax-p1.ymin)/80.0,0);
        ps.drawYTic(p1.yntic,(p1.xmax-p1.xmin)/80.0,0);
        ps.drawXNumbers(p1.xntic);
        ps.drawYNumbers(p1.yntic);
        ps.drawLabel(p1.label);
        ps.drawXLabel(p1.xlabel);
        ps.drawYLabel(p1.ylabel);
    }
        
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        drawShapes(g2);        
    }
    
 public void addStringwithPlotType(String s[],double x1i,double y1i)
 { //adding a series of strings with plottype signs at the beginning
  p1.addStringwithPlotType(s,x1i,y1i);
 }
 
 public void addString(String s[],double x2i,double y2i)
 { // adding a series of strings 
 p1.addString(s,x2i,y2i);}
 
 public void addString(String s,double x3i,double y3i,int i)
 { // adding a series of strings 
 p1.addString(s,x3i,y3i,i);}
 
 public void addData(String fn)
 { //adds data from file  	      
 p1.addData(fn);}
 
 public void addData(double xi[],double yi[])
 {p1.addData(xi,yi,0,0,0,0);}

 public void addData(double ai[][])
 {p1.pp2(ai);}
 
 public void addData(double ai[][],int iplottype[],int ir[],int ig[],int ib[])
 {p1.pp2(ai,iplottype,ir,ig,ib);}
 
 public void addData(double ai[][],int iplottype[])
 {p1.pp2(ai,iplottype);}
 
 public void addData(double xi[],double yi[],int iplottype)
 {p1.addData(xi,yi,iplottype,0,0,0);}
  	  
public void addData(double xi[],double yi[],int ipt,int ir,int ig,int ib)
{p1.addData(xi,yi,ipt,ir,ig,ib);}

public void replaceData(double xi[],double yi[],int i_line,int ipt,int ir,int ig,int ib)
{p1.replaceData(xi,yi,i_line,ipt,ir,ig,ib);}

public void replaceData(double xi[],double yi[],int i_line,int ipt)
{ p1.replaceData(xi,yi,i_line,ipt,0,0,0); }

public void replaceData(double xi[],double yi[],int i_line)
{p1.replaceData(xi,yi,i_line,0,0,0,0); }

public void deleteData(int i_line)
{p1.deleteData(i_line);}
public void deleteallData()
{p1.deleteallData();}



public void addData(double xi[][],double yi[][])
{p1.addData(xi,yi);}
 
public void addData(double xi[][],double yi[][],int ip[])
{p1.addData(xi,yi,ip);}
 
public void addData(double xi[][],double yi[][],int ip[],int ir[],int ig[],int ib[])
{p1.addData(xi,yi,ip,ir,ig,ib); }


public void addFunction(f_x f,double xmi,double xma,int N,int ipt)
{p1.addFunction(f,xmi,xma,N,ipt);}
 
public void addFunction(f_x f,double xmi,double xma,int N)
{p1.addFunction(f,xmi,xma,N);}

public void addFunction(f_x f,double xmi,double xma,int N,int ipt,int ir,int ig,int ib)	
{p1.addFunction(f,xmi,xma,N,ipt,ir,ig,ib);}

public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function)
{p1.addFunction(f,xmin,xmax,Number_of_Data,Number_of_Function);}   

public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function,int ipt[])
{p1.addFunction(f,xmin,xmax,Number_of_Data,Number_of_Function,ipt);} 

public void addFunction(fi_x f,double xmin,double xmax,int Number_of_Data,int Number_of_Function,
   int ipt[],int ir[],int ig[],int ib[])	
{p1.addFunction(f,xmin,xmax,Number_of_Data,Number_of_Function,ipt,ir,ig,ib);   }

public void setMinMax()
{p1.setMinMax();}
public void setMinMax(double iminx,double imaxx,double iminy,double imaxy)
{p1.setMinMax(iminx,imaxx,iminy,imaxy);}

  public void setPlabel(String ip) {p1.setPlabel(ip);}
  public void setXlabel(String ix) {p1.setXlabel(ix);}
  public void setYlabel(String iy) {p1.setYlabel(iy);}
  public void setXYlabel(String ix,String iy) {p1.setXYlabel(ix,iy);}
  public void setPlotType(int plot_type[])	{p1.setPlotType(plot_type);}

  public void setPlotType(int dataset,int plot_no){p1.setPlotType(dataset,plot_no);}
  public void setPlotType(int dataset,char plot_char){p1.setPlotType(dataset,plot_char);}
//----------x y grid on-off ----------------
  public void setXgrid(int igx){p1.setXgrid(igx);}
  public void setYgrid(int igy){p1.setYgrid(igy);}
  public void setGrid(int igx,int igy){p1.setGrid(igx,igy);}
  public void setXtic(int ixt){p1.setXtic(ixt);}
  public void setYtic(int iyt){p1.setYtic(iyt);}
  public void setXYtic(int ixt,int iyt){p1.setXYtic(ixt,iyt);}
  public void setColor(int dataset,int ired,int igreen,int iblue){p1.setColor(dataset,ired,igreen,iblue);}
public void setXlogScaleOn()
{p1.setXlogScaleOn();}

public void setYlogScaleOn()
{p1.setYlogScaleOn();}

public void setXlogScaleOff()
{p1.setXlogScaleOff();}

public void setYlogScaleOff()
{p1.setYlogScaleOff();}
    //public void setColor(int ired,int igreen,int iblue){p1.setColor(ired,igreen,iblue);}
  //public void setColor(Color c){p1.setColor(c);} 
public void plot()
{p1.plot();}
public void dispose()
{p1.dispose();}

}
