Start Search Contents Index Links About
EightArm.java
/*
 * Author: Havard Rast Blok
 * E-mail: 
 * Web   : www.rememberjava.com
 */

import java.awt.*;
import javax.swing.*;

/*
 * Draws a recursive figure
 */
public class EightArm extends JFrame
{
    private double sqr2=Math.sqrt(2);
    private boolean colors = true;
  
    public EightArm()
    {
  super("EightArm");
  setDefaultCloseOperationJFrame.EXIT_ON_CLOSE );
  setSize(300300);
  show();
    }
  
    public void paintGraphics g )
    {
  g.setColorColor.white );
  g.fillRect00, getWidth(), getHeight() );
  eightArms(150150806, g);  
    }
  
    private void eightArms(int x, int y, int r, int depth, Graphics g)
    {
  int red = 0;
  int green = 0;
  int blue = 0;

  if(depth>0)
  {
      ifcolors )
      {
    red=(int)(127*(float)Math.cos(r/40.0))+127;
    green=(int)(127*(float)Math.cos(y/40.0))+127;
    blue=(int)(127*(float)Math.cos(x/40.0))+127;
      }
      
      depth--;
      r=(int)(r*0.6);
      
      g.setColor(new Color(red, green, blue));
      
      //North
      g.drawLine(x, y, x, y-r);
      eightArms(x, y-r, r, depth, g);
      
      //North East
      g.drawLine(x, y, (int)(x+r/sqr2)(int)(y-r/sqr2));
      eightArms((int)(x+r/sqr2)(int)(y-r/sqr2), r, depth, g);

      //East
      g.drawLine(x, y, x+r, y);
      eightArms(x+r, y, r, depth, g);
      
      //South East
      g.drawLine(x, y, (int)(x+r/sqr2)(int)(y+r/sqr2));
      eightArms((int)(x+r/sqr2)(int)(y+r/sqr2), r, depth, g);

      //South
      g.drawLine(x, y, x, y+r);
      eightArms(x, y+r, r, depth, g);

      //South West
      g.drawLine(x, y, (int)(x-r/sqr2)(int)(y+r/sqr2));
      eightArms((int)(x-r/sqr2)(int)(y+r/sqr2), r, depth, g);

      //West
      g.drawLine(x, y, x-r, y);
      eightArms(x-r, y, r, depth, g);

      //North West
      g.drawLine(x, y, (int)(x-r/sqr2)(int)(y-r/sqr2));
      eightArms((int)(x-r/sqr2)(int)(y-r/sqr2), r, depth, g);
  }
    }
  
    public static void mainString args[] )
    {
  new EightArm();
    }

}


site: Håvard Rast Blok
mail:
updated: 27 July 2007