/**
 * DrawableHilbert.java
 * @author Frank Ruskey
 * @version 1.0
 * @revised Johannes Martin
 * @version 1.1
 * @revised Bette Bultena (October 2001)
 */
import java.awt.*;
 
/** 
 * Class DrawableHilbert extends class Hilbert adding methods
 * to draw the Hilbert curve.
 */
public class DrawableHilbert extends Hilbert implements DrawableCurve {

   private static final int GRIDFACTOR = 2; //helpful to divide the canvas into grids
   private static final int XSTART = 1;
   private static final int YSTART = 1;
   private int gridSize;  // The size of the grids
   private int numGrids;  // Needed to find the right upper x start value
   private int stepSize;  // the length of line segments (twice the gridSize)
   private boolean fast;  // whether to go fast or slow
   private Turtle joe;    // our dear little turtle
  
   private Graphics page;
  
   /**
    * Draw the Hilbert curve.
    *
    * @param depth the order of the highest order curve to draw
    * @param showAll specifies whether to draw all curves or highest order curve only
    * @param goFast specifies whether to draw fast or slow
    * @param size of the drawing area (minimum of width and height)
    * @param graphics handle
    */
   public void drawTheCurves( int depth, boolean showAll, 
			     boolean goFast, int size, Graphics graphics ) {
      numGrids = GRIDFACTOR;
      fast = goFast;
      for (int i = 1; i <= depth; ++i) {
         CurveUtil.setLineColor(i, graphics);
         numGrids *= 2;
         gridSize = size / numGrids;
         stepSize = 2 * gridSize;
         if (stepSize == 0) {
	    graphics.drawString("Parameter n too big", 100, 100);
	    return;
         }
         if (showAll || i == depth) {
	    joe = new Turtle(graphics, (numGrids-XSTART)*gridSize, YSTART*gridSize);
	    makeCurve( i );
         }
      }                               
   }
  
   /**
    * Draw a line starting at the current position.
    * 
    * @param xh number of units to go right (may be negative to go left)
    * @param yh number of units to go down (may be negative to go up)
    */
   protected void plot ( int xh, int yh ) {
      if (!fast) 
         CurveUtil.slowDown(stepSize);
      joe.move(xh * stepSize, yh * stepSize);
   }
} // end class DrawableHilbert
 

