Design Patterns Java™ Workbook

(Michael S) #1
Chapter 4. Facade

See the sidebar Parametric Equations on page 40 for an explanation of how this code
establishes the x and y values of the dud's path.


The third purpose that FlightPanel_1 fulfills is to act as a complete application, wrapping
the flight path panel in a titled border and displaying it. This code appears in the main()
routine:


public static void main(String[] args)
{
FlightPanel_1 fp = new FlightPanel_1();
fp.setPreferredSize(new Dimension(300, 200));
JPanel fp_titled = createTitledPanel("Flight Path", fp);
//
JFrame frame = new JFrame("Flight Path for Shell Duds");
frame.getContentPane().add(fp_titled);
frame.addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
frame.pack();
frame.setVisible(true);
}


Parametric Equations


When you need to plot a curve, it can be difficult to describe the y values as
functions of x values. Parametric equations let you define both x and y in terms of
a third parameter that you introduce. Specifically, you can define that time t goes
from 0 to 1 as the curve is drawn, and you can define x and yas functions of the
parameter t.

For example, suppose that you want the parabolic flight of a nonexploding shell to
stretch across the width, w, of a Graphics object. Then a parametric equation for x
is easy:

The y values for a parabola must vary with the square of the value of t. Y values
increase going down the screen. For a parabolic flight, y should be 0 at time t = 0.5:

Here, k represents a constant that we still need to determine. The equation provides
for y to be 0 at t = 0.5 and provides for yto have the same value at t = 0 and t = 1. At
those two times, y should be h,so with a little algebraic manipulation, you can find
Free download pdf