/*
  The Koch Snowflake project
  by: Burt Rosenberg
  date: 20 September 1996
  for: Math 220, 97/1, Univ. of Miami

  CODE HAS BEEN DELETED.
    The assignment is to write the code needed to
  make the program work again.

  Note: a better approach would be to separate generating
  the snowflake from drawing the snowflake, using an offscreen
  canvas. However, this would only obscure our pedegogic objectives.

*/

import java.applet.Applet ;
import java.awt.* ;

public class Koch extends Applet {
   
   final boolean DEBUG=true ;
   // final boolean DEBUG=false ;

   Panel myButtonPanel ;
   MyDrawingPanel myDrawingPanel ;
   Choice selectDepth ;
   int [] depthValues = { 0, 1, 2, 3, 4, 5, 6, 7, 8 } ;
   String [] depthNames = {
             "Depth 0", 
             "Depth 1", 
             "Depth 2",
             "Depth 3", 
             "Depth 4",
             "Depth 5",
             "Depth 6",
             "Depth 7", 
             "Depth 8" } ; 
 
   int recursionDepth = 3 ;

    public void init() {

       setLayout( new BorderLayout() ) ;

       // make the button panel
       myButtonPanel = new Panel() ;
       selectDepth = new Choice() ;
       for ( int i = 0 ; i < depthNames.length; i++ ) {
           selectDepth.addItem(depthNames[i]) ;
       }
       myButtonPanel.add(selectDepth) ; 
       add( "North", myButtonPanel ) ;

       myDrawingPanel = new MyDrawingPanel(this) ;      
       add( "Center", myDrawingPanel ) ;
       validate() ;
    }

    public boolean action(Event e, Object o) {
       if ( e.target==selectDepth ) {
         myDrawingPanel.setRecursionDepth( 
            depthValues[selectDepth.getSelectedIndex()] ) ;
         myDrawingPanel.repaint() ;
         return true ;
      }
      return false ;
   }
 
} 

class MyDrawingPanel extends Panel {

     final boolean DEBUG = true ;
     // final boolean DEBUG = false ;

     Koch parent ;
     int recursionDepth = 0 ;
     final Color drawingColor = Color.red ;

     MyDrawingPanel(Koch parent) {
        this.parent = parent ;
        M = java.lang.Math.sqrt(3.0)/2.0 ;
     }

     public void setRecursionDepth(int recursionDepth) {
        this.recursionDepth=recursionDepth ;
     }

     public void paint(Graphics gc) {
      if (DEBUG) System.out.println("MyDrawingPanel:entered") ;

      // clear the screen
      Dimension d = size() ;
      gc.setColor( getBackground() ) ;
      gc.fillRect(0,0,d.width,d.height) ;

      gc.setColor( drawingColor ) ;
      kochDraw( gc, recursionDepth, 0.0, 
         ((double)(3*d.height))/4.0, 
         (double)d.width, ((double)(3*d.height))/4.0) ;
   }
     
   double M ;  // sqrt(3)/2

   void kochDraw( Graphics gc, int d, double x1, double y1, 
         double x2, double y2 ) {
      if ( d==0 ) 
	     gc.drawLine((int)x1,(int)y1,(int)x2,(int)y2) ; 
      else {

          //
          // code deleted here
          //

      }  
   }

}
