import java.awt.*;
      
public class BasicLinePlot extends Canvas {
  double[] x,y,xgrid,ygrid;
  int i,nper;
  int ixleft,ixright,iytop,iybottom,ih,iw,ihalftic,iRise,fmHeight;
  double xleft,xright,ytop,ybottom;
  double lmarg = 0.10,rmarg = 0.05,tmarg = 0.10,bmarg = 0.10,halftic = 0.0035;
  double ixintercept,ixslope,iyintercept,iyslope;
  String thingy;
  public BasicLinePlot() {}
  String mainTitle;
  public void newXY(double[] x,double[] y,double[] xgrid,double[] ygrid,String mainTitle) {
    if(x.length != y.length) {
      System.err.println("Error: x and y must have the same length");
      System.exit(-1);}
    this.x = new double[x.length];
    this.y = new double[x.length];
    for(i=0;i<x.length;i++) {
      this.x[i] = x[i];
      this.y[i] = y[i];}
    this.xgrid = new double[xgrid.length];
    this.ygrid = new double[ygrid.length];
    for(i=0;i<xgrid.length;i++)
      this.xgrid[i] = xgrid[i];
    for(i=0;i<ygrid.length;i++)
      this.ygrid[i] = ygrid[i];
    this.mainTitle = mainTitle;
    repaint();}
  public void paint(Graphics g) {
      if(xgrid != null) {
      xleft = xgrid[0];
      xright = xgrid[xgrid.length - 1];
      ybottom = ygrid[0];
      ytop = ygrid[ygrid.length - 1];
  
      Rectangle r = bounds();
      ih = r.height;
      iw = r.width;
      iytop = (int) (tmarg * ((double) ih));
      iybottom = (int) ((1.0-bmarg) * ((double) ih));
      ixleft = (int) (lmarg * ((double) iw));
      ixright = (int) ((1.0-rmarg) * ((double) iw));
      ihalftic = (int) (halftic * ((double) iw));
  
      ixslope = ((double) (ixright - ixleft))/(xright - xleft);
      ixintercept = 0.5 + (((double) ixright) * xleft - ((double) ixleft) * xright)
        /(xleft - xright);
      iyslope = ((double) (iybottom - iytop))/(ybottom - ytop);
      iyintercept = 0.5 + (((double) iybottom) * ytop - ((double) iytop) * ybottom)
        /(ytop - ybottom);
  
//
//  white background
//
      g.setColor(Color.white);
      g.fillRect(0,0,iw,ih);
  
//
//  draw the axes
//
      g.setColor(Color.black);
      g.drawLine(ixleft,iytop,ixleft,iybottom);
      g.drawLine(ixleft,iybottom,ixright,iybottom);
  
//
//  add tick marks
//
      g.setColor(Color.black);
      for(i=0;i<ygrid.length;i++)
        g.drawLine(ixleft-ihalftic,y2iy(ygrid[i]),
          ixleft+ihalftic,y2iy(ygrid[i]));
      for(i=0;i<xgrid.length;i++)
        g.drawLine(x2ix(xgrid[i]),iybottom-ihalftic,
          x2ix(xgrid[i]),iybottom+ihalftic);
  
//
//  add axis numbers
//
      g.setColor(Color.black);
      FontMetrics fm = g.getFontMetrics();
      iRise = fm.getAscent()/2;
      fmHeight = fm.getHeight();
      for(i=0;i<ygrid.length;i++) {
        thingy = Double.toString(ygrid[i]);
        g.drawString(thingy,ixleft - ihalftic -fm.stringWidth(thingy),
          y2iy(ygrid[i]) + iRise);}
      for(i=0;i<xgrid.length;i++) {
        thingy = Double.toString(xgrid[i]);
        g.drawString(thingy,x2ix(xgrid[i]) - fm.stringWidth(thingy)/2,
          iybottom + ihalftic + fmHeight);}
  
//
//  plot lines
//
      g.setColor(Color.blue);
      for(i=1;i<x.length;i++)
        g.drawLine(x2ix(x[i-1]),y2iy(y[i-1]),
          x2ix(x[i]),y2iy(y[i]));
  
//
//  add the mainTitle
//
      g.drawString(mainTitle,(ixright + ixleft)/2 - fm.stringWidth(mainTitle)/2,
        fmHeight);}}
  int x2ix(double x) {return (int) Math.floor(ixintercept + ixslope * x);}
  int y2iy(double y) {return (int) Math.floor(iyintercept + iyslope * y);}}

