所以我在返回之前和之后发布了这个程序的问题并进行了一些更改我仍然有另一个问题。我运行该程序,它只处理一个' branch()'的调用。我不确定会导致问题的原因是什么,我改变了我的程序以使用类和applet,而不是将所有内容都放在一个类中。谢谢你的帮助。一切都是进口的。
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
}