Java Reference
In-Depth Information
public void paintComponent(Graphics g)
{
super.paintComponent(g); // paint the background
int nPoint = 101;
double w = getWidth();
double h = getHeight();
int[] x = new int[nPoint];
int[] y = new int[nPoint];
for (int i = 0; i < nPoint; i++)
{
// t goes 0 to 1
double t = ((double) i) / (nPoint - 1);
// x goes 0 to w
x[i] = (int) (t * w);
// y is h at t = 0 and t = 1, and y is 0 at t = .5
y[i] = (int) (4 * h * (t - .5) * (t - .5));
}
g.drawPolyline(x, y, nPoint);
}
This method hard-codes the function it plots. Suppose that you want to be able to create
functions for x and y at runtime. A first step is to apply polymorphism: Make the name of the
method fixed, and provide classes with varying implementations of this method. Figure 27.4
shows a FunPanel class that accepts functions to plot.
Search WWH ::




Custom Search