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 Plot2D1 extends JPanel implements Printable
{ 
	public Graphics2D g;
	public Plot1 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);
    PlotShapesSW1 ps;
    double xx[],yy[];
    int xn;
    
    public Plot2D1(Plot1 pi)
    {
	//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 Plot2D1()
    {
	//Initialize drawing colors
    setBackground(Color.white);
    setForeground(Color.black);
    //read data from file Plot data previously defined 
    try{p1=new Plot1();
       } catch(IOException ioe) {System.err.println("IOExceptionin opening plot");}
    xx=new double[200];
    yy=new double[200];
    xn=0;
	}
    public Plot2D1(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 Plot1(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 Plot1();
           } catch(IOException ioe) {System.err.println("IOExceptionin opening plot");}
    }
    
    public void yenidenPlotDatasiOku(Plot1 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(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 Plot1(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;
        drawShapes(g2);
        return Printable.PAGE_EXISTS;
    }

    
    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;
        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(p1.xabsmin,p1.yabsmin,p1.abswidth,p1.absheight);
        ps=new PlotShapesSW1(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);        
    }
}
