 |
|
|
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");
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setSize(300, 300);
show();
}
public void paint( Graphics g )
{
g.setColor( Color.white );
g.fillRect( 0, 0, getWidth(), getHeight() );
eightArms(150, 150, 80, 6, 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)
{
if( colors )
{
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 main( String args[] )
{
new EightArm();
}
}
|
|
|
 |