megadave
I am a FH squatter
- Joined
- Apr 3, 2006
- Messages
- 11,911
Ok i have some java bluej work i am stuck on, i am complete noob at it but need to get it out of the way so i can do some more important exam revision. anyone able to help?
All i've got left to do is;
*Add a random element to the construction so that the length of each branch and/or the angle between branches
may vary at each iteration.
*Adapt the applet so that it can draw more than one tree on the screen.
Thanks a lot to anyone who can help :|
All i've got left to do is;
*Add a random element to the construction so that the length of each branch and/or the angle between branches
may vary at each iteration.
*Adapt the applet so that it can draw more than one tree on the screen.
Code:
/*
* OneFractalTree.java
*/
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class OneFractalTree extends JApplet {
final double ONE_DEGREE = Math.PI/180;
final double BRANCHANGLE = ONE_DEGREE*30; //sets the angle the branches extend at
final double SHRINKFACTOR = .65; //sets the value at which the branches shrinks by
final int START_LENGTH = 75; //sets the length of the branches
public void drawbranch( Graphics g,
double startx,
double starty,
double length,
double angle )
{
double endx = startx + Math.sin(angle) * length;
double endy = starty + Math.cos(angle) * length;
if( 1 < length ) {
g.setColor( length < 5 ? Color.green : Color.black );
g.drawLine( (int)startx, (int)starty, (int)endx, (int)endy );
drawbranch( g, endx, endy, length * SHRINKFACTOR, angle -
BRANCHANGLE );
drawbranch( g, endx, endy, length * SHRINKFACTOR, angle +
BRANCHANGLE );
drawbranch( g, endx, endy, length * SHRINKFACTOR, angle
);
}
}
public void paint( Graphics g ) {
Rectangle r = getBounds();
drawbranch( g, r.width/2, r.height, START_LENGTH, Math.PI );
}
}