Java Reference
In-Depth Information
protected void calculateExtrema()
{
for (int i = 0; i < nPoint; i++)
{
double t = ((double) i) / (nPoint - 1);
double dx = fx.f(t);
double dy = fy.f(t);
if (i == 0 || dx > xMax)
{
xMax = dx;
}
if (i == 0 || dx < xMin)
{
xMin = dx;
}
if (i == 0 || dy > yMax)
{
yMax = dy;
}
if (i == 0 || dy < yMin)
{
yMin = dy;
}
}
}
The
paintComponent()
method also lets time vary from 0 to 1 and scales the curves to fill
the panel:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
double h = (double) (getHeight() - 1);
double w = (double) (getWidth() - 1);
for (int i = 0; i < nPoint; i++)
{
double t = ((double) i) / (nPoint - 1);
xArray[i] = (int)
(w * (fx.f(t) - xMin) / (xMax - xMin));
yArray[i] = (int)
(h - h * (fy.f(t) - yMin) / (yMax - yMin));
}
g.setColor(Color.black);
g.drawPolyline(xArray, yArray, nPoint);
}
The
FunPanel
class lets you supply various implementations of the
f()
method in separate
classes. If you were to implement the
f()
method directly, you would wind up with classes
like
FlightPathX
and
FlightPathY
, which provide
x
and
y
functions. Rather than
creating new classes for each new function, D
ECORATOR
lets you assemble a function from
an existing hierarchy. Figure 27.5 shows a hierarchy of function classes that implement a
common operation
f()
. These classes appear in the
com.ooz-inoz.function
package.