0

So I posted a problem with this program before and after going back in and making some changes I still have another problem. I run the program and it only processes one call of 'branch()'. I'm not sure what would cause the problem, I changed my program to use a class and applet instead of having everything in one class. Thanks for any help. Also everything is imported.

public class TreeApplet extends Applet{

int width,height;

Tree tree;
Image page;
Graphics draw;

public void init(){
    width = 1000;
    height = 600;
    setSize(width,height);
    setBackground(Color.black);

    page = this.createImage(width,height);
    draw = page.getGraphics();

    tree = new Tree(width,height,100);      
}

public void paint(Graphics g){
    tree.draw(draw);
    g.drawImage(page,0,0,this);
}
}


public class Tree{

int x,y,x1,x2,y1,y2;
int width,height;
int len,temp1 = 0,temp2 = 0;
int smallestBran = 10;
double fractionLength = .85;

double bran;//,count;
int ang;
double rand;

public Tree(int w,int h,int b){
    width = w;
    height = h;

    bran = b;

    x = width/2;
    y = height;

    x1=x;
    y1=y;

    x2=x1;
    y2=y1-100;      
}

public void draw(Graphics g){
    g.setColor(Color.green);
    g.drawLine(x1,y1,x2,y2);
    branch(g,x2,y2,20);     
}

public void setBran(int x){
    bran = x;
}

public void branch(Graphics g,int x,int y,int ang){             
        x1 = x;
        y1 = y;

        rand = ang * (Math.PI/180);

        bran = bran * fractionLength * (Math.random()/10.0 + .9);

        int xChange = (int) (Math.sin(rand)*bran); 
        int yChange = (int) (Math.cos(rand)*bran);
        y2 = y-yChange;

        /*System.out.printf("X1 | %3d \t X2 | %3d \t Y | %3d \t ChangeX | %3d \t ChangeY | %3d \n",
                x1-xChange,x1+xChange,y2,xChange,yChange);*/

        g.setColor(Color.blue);
        g.drawLine(x1,y1,x1-xChange,y2);
        g.setColor(Color.orange);
        g.drawLine(x1,y1,x1+xChange,y2);


        if(bran > smallestBran){
        branch(g,x1-xChange,y2,ang+10);
        branch(g,x1+xChange,y2,ang+10);
        }
}//End of branch
}
  • Although I hate Print Driven Development, but if you put a print after the first branch call (with `x1-xChange`) does it get printed? – karatedog Jan 22 '15 at 18:33
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Jan 23 '15 at 02:19
  • Sorry I fixed it by defining the new x values before drawing. Not sure why it works but it does. – Dontstealthisname Jan 25 '15 at 23:35

0 Answers0