// Create and display a dragon curve.
// CS 310, Spring 2008

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFrame;
import java.awt.geom.Rectangle2D;


class DragonFrame
extends JFrame {

  DragonFrame(DragonCurve dragonCurve, int height, int width) {

    // Create a dragon frame based on the given dragon curve with the given
    // dimensions.

    setTitle("Order " + dragonCurve.order() + " Dragon Curve");
    setSize(height, width);
    add(new DragonPanel(dragonCurve));
    }
  }


class 
DCShow {

  private static Rectangle2D
  boundingBox(Point2D points[]) {

    // Return the smallest rectangle enclosing the given set of points.

    double minX, minY, maxX, maxY;
    minX = maxX = points[0].getX();
    minY = maxY = points[0].getY();

    for (int i = points.length - 1; i > 0; i--) {
      final Point2D pt = points[i];
      minX = Math.min(minX, pt.getX());
      maxX = Math.max(maxX, pt.getX());
      minY = Math.min(minY, pt.getY());
      maxY = Math.max(maxY, pt.getY());
      }
  
    return new Rectangle2D.Double(minX, minY, maxX, maxY);
    }


  private static void
  die(String emsg) {

    // Print an error message and die.

    System.err.println(emsg + ".");
    System.exit(1);
    }


  private static void
  displayDragonCurve(DragonCurve dragonCurve) {
    
    // Draw the given dragon curve.

    DragonFrame frame = new DragonFrame(dragonCurve, 500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }


  private static int
  doArgs(String args[]) {

    // Handle the given command-line arguments, which should be the dragon
    // curve's order.  Return the order or die with an error message.

    if (args.length < 1)
      emsg("No arguments given");
    if (args.length > 1)
      emsg("Too many arguments given");

    int order = 0;
    try { order = (new Integer(args[0])).intValue(); }
    catch (Exception e) { die("Command-line argument should be an integer"); }
    if (order < 0)
      die("Command-line argument should be a positive integer");

    return order;
    }


  private static void
  emsg(String emsg) {

    // Print a command-line error message and die.

    die(emsg + ".\nCommand-line format is \"order\"");
    }


  public static void 
  main(String args[]) {

    // Read in a dragon curve and show it.

    int order = doArgs(args);

    DragonCurve dragonCurve = new DragonCurve(readPoints());
    while (--order > -1)
      dragonCurve = new DragonCurve(dragonCurve);

    displayDragonCurve(dragonCurve);
    }


  private static Point2D.Float
  readPoint(Scanner scanner) {

    // Read and return a point from the given scanner, or die with an error
    // message.  Return null if there's no more points to return.

    if (!scanner.hasNextFloat())
      return null;

    final float x = scanner.nextFloat();
    if (!scanner.hasNextFloat())
      die("An even number of coordinates are required");

    return new Point2D.Float(x, scanner.nextFloat());
    }


  private static Point2D []
  readPoints() {

    // Read and return points from std-in, dying with an error message if
    // needed.

    ArrayList<Point2D> points = new ArrayList<Point2D>();
    final Scanner scanner = new Scanner(System.in);

    while (true) {
      final Point2D point = readPoint(scanner);
      if (point == null)
	break;
      points.add(point);
      }

    if (points.size() < 2)
      die("Two or more points required");

    return points.toArray(new Point2D [] {});
    }
  }

syntax highlighted by Code2HTML, v. 0.9.1