import java.awt.*; import java.awt.event.*; public abstract class FunctionPlotter extends Frame { int yvalues[]; static final int xdisp = 20; static final int ydisp = 20; static final int maxy = 400; static final int samples = 100; static final int extraspace = 20; public FunctionPlotter() { yvalues = new int[samples]; setSize(samples+xdisp+extraspace, maxy+ydisp+extraspace); for(int x = 0; x<100; x++) yvalues[x] = 0; // exits when window gets closed addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void paint(Graphics g) { g.drawLine(xdisp, extraspace+extraspace, xdisp, maxy+ydisp); g.drawLine(0, maxy-ydisp, samples+xdisp, maxy-ydisp); g.setColor(Color.blue); for(int x = 0; x < 99; x++) g.drawLine(xdisp+x,maxy-ydisp-yvalues[x], xdisp+(x+1),maxy-ydisp-yvalues[x+1]); } public void plot() { for(int x = 0; x < 100; x++) yvalues[x] = (int) Math.round(f(1.0*x)); show(); } public abstract double f(double x); }