import javax.swing.*;
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

abstract class f_x
{ abstract double func(double x);}

public class plot2P extends JPanel
{  private static final long serialVersionUID = 7685675L;
 int x[];
 int y[];
 double xmin;
 double xmax;
 int n;
 fb f;
 int width;
 int height;
 public plot2P(f_x f,double xmini,double xmaxi,int widthi,int heighti)
 { 
   // height : height of the plot window;
   // width  : width of the plot window
   // xmin minimum x value
   // x maximum x value  
   xmin=xmini;
   xmax=xmaxi;
   Dimension d=new Dimension();;
   width=widthi;
   height=heighti;
   n=200;
   x=new int[n+1];
   y=new int[n+1];
   double xd[]=new double[n+1];
   double yd[]=new double[n+1];
   double ymin=1.0e60;
   double ymax=-1.0e60;
   //double calculated values
   for(int i=0;i<n+1;i++) 
   {xd[i] = xmin+(xmax-xmin)*(double)i/(double)n;
    yd[i] =  f.func(xd[i]);
    if(yd[i]<ymin) ymin=yd[i];
    if(yd[i]>ymax) ymax=yd[i];
   }
   //convert to integer (pixel)
  for(int i=0;i<n+1;i++)
  {x[i]=(int)(width*(xd[i]-xmin)/(xmax-xmin));
   y[i]=height-(int)(height*(yd[i]-ymin)/(ymax-ymin));	
  }
 }
 
 public void paintComponent(Graphics g)
 {
 Graphics2D g2=(Graphics2D)g;
 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
 for(int i=1;i<x.length;i++)   
  { Line2D l=new Line2D.Double(x[i-1],y[i-1],x[i],y[i]);
    g2.draw(l);	
  }
 Rectangle2D r=new Rectangle2D.Double(0,0,height,width);
 g2.draw(r);	  
 }
}

