ronald wrote ...

2019/8/21

fractal Tree

ronald

2019/8/21

```private void drawTree(Graphics g, int x1, int y1, double angle, int depth)
{
GreenfootImage gfim = new GreenfootImage(200,200);

if (depth == 0) return;
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0);
g.drawLine(x1, y1, x2, y2);
drawTree(g, x2, y2, angle - 20, depth - 1);
drawTree(g, x2, y2, angle + 20, depth - 1);
}

public void paint(Graphics g)
{
g.setColor(Color.BLACK);
drawTree(g, 400, 500, -90, 9);
}```
i have found a source code and i try to apply it to greenfoot, i can not do it. It's a code of a simple fractal tree. Thank you for your help
danpost

2019/8/21

Remove line 3 (not used anywhere in code). Remove "Graphic g, " from lines 1 and 13 (will use background of world). Remove "g, " from lines 9, 10 and 16. Change "g." to "getBackground." in lines 8 and 15.
ronald

2019/8/21

```int x1;
int y1;
double angle;
int depth;

public void act()
{
drawTree();
}

private void drawTree()
{

if (depth == 0) return;
int x2 = x1 + (int) (Math.cos(Math.toRadians(angle)) * depth * 10.0);
int y2 = y1 + (int) (Math.sin(Math.toRadians(angle)) * depth * 10.0);
getBackground.drawLine(x1, y1, x2, y2);
drawTree(x2, y2, angle - 20, depth - 1);
drawTree(x2, y2, angle + 20, depth - 1);
}

public void paint()
{
getBackground.setColor(Color.BLACK);
drawTree(400, 500, -90, 9);
}
}
```
I give you the code as you requested, it seems to me that background is from the class World, I try to build with the Actor class, that's why I used GreenfootImage gfim
danpost

2019/8/21

ronald wrote...
<< Code Omitted >> I give you the code as you requested, it seems to me that background is from the class World, I try to build with the Actor class, that's why I used GreenfootImage gfim
But, you did not use it. Nowhere, after line 3, is gfim being used. Also, it does not make any sense to create an image inside the drawTree method. You can create the image in the paint method and pass it in the drawTree calls; then set it as the image of the actor. In your latest given code, you cannot use instance fields for x1, y1, angle and depth. They need to have independent values for the different re-iterative calls to drawTree. Also, having the act method call drawTree, which just creates a single line of the tree, does not make sense. Use the class constructor for that (or for code in paint method):
```int depth = 9;
int len = 10;

public FracTree() // "FracTree" used as class name (replace with class name)
{
int high = len*deep*(deep+1)/2;
GreenfootImage g = new GreenfootImage(3*high/2, high);
drawTree(g, 3*high/4, high-1, -90, depth);
setImage(img);
}

private void drawTree(GreenfootImage g, int x, int y, int a, int d)
{
if (d == 0) return;
int x2 = x+(int)(Math.cos(Math.PI*a/180)*d*len);
int y2 = y+(int)(Math.sin(Math.PI*a/180)*d*len);
img.drawLine(x, y, x2, y2);
drawTree(img, x2, y2, a-20, d-1);
drawTree(img, x2, y2, a+20, d-1);
}```
ronald

2019/8/22

thank you for the help