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 Plot2DP extends JPanel implements Printable
{
    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);

    public Plot2DP()
    {
        //Initialize drawing colors
        setBackground(Color.white);
        setForeground(Color.black);

        try{
             p1=new Plot();
           } catch(IOException ioe) {System.err.println("IOExceptionin opening plot");}

    }

    public void yenidenciz()
    {
    repaint();
    }

    public void yazdir()
    {
        PrinterJob printJob = PrinterJob.getPrinterJob();
        printJob.setPrintable(this);
        if (printJob.printDialog()) {
            try {
                printJob.print();
                } catch (Exception ex) {ex.printStackTrace();}
                                     }
    }

    public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
        if (pi >= 1) {
                     return Printable.NO_SUCH_PAGE;
                     }
        drawShapes((Graphics2D) g,10);
        return Printable.PAGE_EXISTS;
    }



    public void drawShapes(Graphics2D g2, int printfactor)
    {
        Dimension d=getSize();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        int gridWidth = d.width*printfactor;
        int gridHeight = d.height*printfactor;
        int rowspacing=5;
        int columnspacing=7;
        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);

        AffineTransform aft=new AffineTransform();
        aft.setToIdentity();
        g2.setTransform(aft);
        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);
        PlotShapesSW ps=new PlotShapesSW(g2,p1.xabsmin,p1.yabsmin,
        p1.absheight,p1.abswidth,p1.xmin,p1.xmax,p1.ymin,p1.ymax);
        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.xntic);
        ps.drawLabel(p1.label);
        ps.drawXLabel(p1.xlabel);
        ps.drawYLabel(p1.ylabel);
        if(p1.xgridon!=0)
           ps.drawXGrid(p1.xntic);
        if(p1.ygridon!=0)
           ps.drawYGrid(p1.yntic);
        int i,j;
        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
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        drawShapes(g2,1);
    }


}
