March 2002
Intermediate to advanced
496 pages
8h 51m
English
You can combine DECORATOR with the idea of treating functions as objects to allow for runtime composition of new functions. In Chapter 4, Facade, you refactored an application that shows the flight path of a nonexploding aerial shell. The original code calculated the path in the paintComponent() method of a JPanel subclass:
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 ...Read now
Unlock full access