/*

*/
import java.awt.*;

public class RecursivePolygons3 extends Frame
{
   public static final int SLEEP_TIME = 200;

   public RecursivePolygons3()
   {
      super( "Drawing Polygons" );
      setSize( 500, 500 );
      show();
   }

   public void recursiveDrawing(int n, Graphics g, int x1, int y1, int x2, int y2)
   {
      if (n == 0) return;
      else if ( n%2 == 0 )
      {
         g.setColor(Color.red);
         int[] xValues1 = {x1, (x1+x2)/2, (x1+x2)/2, x1};
         int[] yValues1 = {y1, y1, (y1+y2)/2, (y1+y2)/2};
         g.fillPolygon( xValues1, yValues1, 4 );
         try{ Thread.sleep(SLEEP_TIME); } catch (InterruptedException e){}

         g.setColor(Color.green);
         int[] xValues2 = {(x1+x2)/2, x2, x2, (x1+x2)/2};
         int[] yValues2 = {(y1+y2)/2, (y1+y2)/2, y2, y2};
         g.fillPolygon( xValues2, yValues2, 4 );
         try{ Thread.sleep(SLEEP_TIME); } catch (InterruptedException e){}

         recursiveDrawing(n-1, g, (x1+x2)/2, y1, x2, (y1+y2)/2);

         recursiveDrawing(n-1, g, x1, (y1+y2)/2, (x1+x2)/2, y2);
      }
      else
      {
         g.setColor(Color.red);
         int[] xValues1 = {(x1+x2)/2, x2, x2, (x1+x2)/2};
         int[] yValues1 = {y1, y1, (y1+y2)/2, (y1+y2)/2};
         g.fillPolygon( xValues1, yValues1, 4 );
         try{ Thread.sleep(SLEEP_TIME); } catch (InterruptedException e){}

         g.setColor(Color.green);
         int[] xValues2 = {x1, (x1+x2)/2, (x1+x2)/2, x1};
         int[] yValues2 = {(y1+y2)/2, (y1+y2)/2, y2, y2};
         g.fillPolygon( xValues2, yValues2, 4 );
         try{ Thread.sleep(SLEEP_TIME); } catch (InterruptedException e){}

         recursiveDrawing(n-1, g, x1, y1, (x1+x2)/2, (y1+y2)/2);

         recursiveDrawing(n-1, g, (x1+x2)/2, (y1+y2)/2, x2, y2);
      }
   }

   public void paint( Graphics g )
   {
      recursiveDrawing(6, g, 0, 0, 500, 500);
   }


   // The main method creates an instance of this class
   // thereby "running" this program.
   public static void main( String args[] )
   {
      RecursivePolygons3 app = new RecursivePolygons3();
   }//main

}//RecursivePolygons3
