-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeFractal.java
More file actions
58 lines (51 loc) · 1.89 KB
/
TreeFractal.java
File metadata and controls
58 lines (51 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.applet.Applet;
import java.awt.*;
/**
* Created by ignacioojanguren on 10/11/16.
*/
public class TreeFractal extends Applet {
private Graphics graphic;
private Image drawImage;
/**
* This class initialize the AWT applet
* Also it initialize the recursive method RandomTree()
*/
public void init(){
int height = getSize().height;
int width = getSize().width;
drawImage = createImage(width,height);
graphic = drawImage.getGraphics();
randomTree(10, width/2,0, 90, graphic);
}
/**
* This is a recursive method, this method will print the branches of the Fractal tree.
* It creates per each branch 2 subranches.
* @param branch
* Is the number of subranches we want in the Fractal tree, when highest the number, the number of branches will
* increase
* @param bottomx
* @param bottomy
* Bottomx and Bottomy are the two points from the bottom of the line
* @param angle
* Angle is the distance between the subranches we want to see.
* @param graphic
* Passing the initialization of the java awk window, this will allow the method to draw in the window.
*
* @postcondition
* This method will draw the proper fractal tree.
*
*/
public void randomTree(int branch, int bottomx, int bottomy, double angle, Graphics graphic){
int topx, topy;
if (branch == 0){
return;
}
topx = bottomx + (int)(Math.cos(Math.toRadians(angle)) * branch * 9.0);
topy = bottomy + (int)(Math.sin(Math.toRadians(angle)) * branch * 9.0);
graphic.drawLine(bottomx, bottomy, topx, topy);
randomTree(branch-1, topx,topy, angle - 20, graphic);
randomTree(branch-1, topx,topy, angle + 20, graphic);
}
@Override
public void paint(Graphics graphic){graphic.drawImage(drawImage,0,0,null);}
}