01: package org.cougaar.demo.mandelbrot.v0;
02:
03: import java.io.FileOutputStream;
04: import java.io.OutputStream;
05: import org.cougaar.demo.mandelbrot.util.Arguments;
06: import org.cougaar.demo.mandelbrot.util.FractalMath;
07: import org.cougaar.demo.mandelbrot.util.ImageOutput;
08:
09: /**
10: * A minimal non-Cougaar application that computes the Mandelbrot Set image
11: * data and either writes it to a file or pops up a UI.
12: * <p>
13: * To write to a file, specify a "file=<i>String</i>" argument. If no file
14: * is specified then the image will be displayed in a popup Swing UI.
15: * <p>
16: * Usage is:<pre>
17: * java \
18: * -classpath lib/mandelbrot.jar \
19: * org.cougaar.demo.mandelbrot.v0.MandelbrotPopup \
20: * [ARGS]
21: * </pre>
22: * Supported arguments are:<ul>
23: * <li>width=<i>integer</i></li>
24: * <li>height=<i>integer</i></li>
25: * <li>x_min=<i>double</i></li>
26: * <li>x_max=<i>double</i></li>
27: * <li>y_min=<i>double</i></li>
28: * <li>y_max=<i>double</i></li>
29: * <li>file=<i>String</i></li>
30: * </ul>
31: */
32: public class MandelbrotPopup {
33:
34: // default mandelbrot parameters
35: private static final Arguments DEFAULT_SELECTION = new Arguments(
36: new String[] { "width=750", "height=500", "x_min=-2.0",
37: "x_max=1.0", "y_min=-1.0", "y_max=1.0" });
38:
39: public static void main(String[] sa) {
40: // parse our arguments
41: Arguments args = new Arguments(sa, DEFAULT_SELECTION);
42:
43: // compute the mandelbrot data
44: byte[] data = FractalMath.mandelbrot(args.getInt("width"), args
45: .getInt("height"), args.getDouble("x_min"), args
46: .getDouble("x_max"), args.getDouble("y_min"), args
47: .getDouble("y_max"));
48:
49: // optionally write to file
50: String filename = args.getString("file");
51: if (filename != null) {
52: try {
53: OutputStream out = new FileOutputStream(filename);
54: ImageOutput.writeJPG(args.getInt("width"), args
55: .getInt("height"), data, out);
56: out.close();
57: } catch (Exception e) {
58: throw new RuntimeException("Unable to write to "
59: + filename, e);
60: }
61: System.out.println("Wrote JPEG to " + filename);
62: return;
63: }
64:
65: // display the data as a Swing popup UI
66: //
67: // Ideally our popup would support interactive navigation, but for now
68: // it's a trivial image display.
69: //
70: // In contrast, our servlet version of this UI uses HTML forms and
71: // JavaScript to support interactive navigation. See ../FrontPageServlet.
72: ImageOutput.displayImage(args.getInt("width"), args
73: .getInt("height"), data);
74:
75: // the popup "close" button will call "System.exit"
76: }
77: }
|