Java Reference
In-Depth Information
After refactoring, the FlightPanel class is much simpler:
package com.oozinoz.applications;
import javax.swing.*;
import java.awt.*;
import com.oozinoz.simulation.FlightPath;
public class FlightPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g); // paint the background
int nPoint = 101;
FlightPath fp =
new FlightPath(
getWidth() - 1, getHeight() - 1);
g.drawPolyline(
fp.getX(nPoint),
fp.getY(nPoint),
nPoint);
}
}
The FlightPath class provides the parabolic flight path of a dud:
package com.oozinoz.simulation;
public class FlightPath
{
protected double distance;
protected double apogee;
public FlightPath(double distance, double apogee)
{
this.distance = distance;
this.apogee = apogee;
}
public int[] getX(int nPoint)
{
int[] x = 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 distance
x[i] = (int) (t * distance);
}
return x;
}
public int[] getY(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);
// y is apogee at t = 0 and t = 1,
// and y is 0 at t = .5
Search WWH ::




Custom Search