Java Reference
In-Depth Information
Figure 4.2. The FlightPanel_1 application shows where a dud may land.
There is a problem with the FlightPanel_1 class: It intermingles three purposes. Its
primary purpose is to act as a panel that displays a flight path. A second purpose of this class
is to calculate a parabolic flight path. The FlightPanel_1 class currently performs this
calculation in its paintComponent() code:
public void paintComponent(Graphics g)
{
super.paintComponent(g); // paint the background
int nPoint = 101;
double w = getWidth() - 1;
double h = getHeight() - 1;
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);
}
Search WWH ::




Custom Search