欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入 下面是绘制次分形树的方法: import java.awt.Graphics; import javax.swing.JFrame; public class Main extends JFrame { public static void main(String[] args) { Main a = new Main(); a.draw();

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

下面是绘制次分形树的方法:

import java.awt.Graphics;

import javax.swing.JFrame;

public class Main extends JFrame {

public static void main(String[] args) {

Main a = new Main();

a.draw();

}

public void draw(){//绘制窗体,添画布

this.setSize(1000,700);//

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(3);

this.setVisible(true);

Graphics g = this.getGraphics();

}

public void paint(Graphics g){

super.paint(g);

this.Show(500,550,100, Math.PI/2,0,Math.PI/6,25,g);

//(Math.PI为180°)

}

public void Show(double x0,double y0,double l,double a,double b,

double c,double count,Graphics g){

double x2;

double y2;

double x3;

double y3;

double x4;

double y4;

double x5;

double y5;

if(count< 1)

{

return;

}

//判断是否继续进行递归调用,注意:判断一定要放在递归调用之前,否则这段代码将永远不会被执行

x2 = x0 - l*Math.cos(a);

y2 = y0 - l*Math.sin(a);

x3 = x2 - l*Math.cos(b);

y3 = y2 - l*Math.sin(b);

x4 = x0 - l*Math.cos(b);

y4 = y0 - l*Math.sin(b);

x5 = x2 - l*Math.cos(Math.PI/6)*Math.cos(c);

y5 = y2 - l*Math.cos(Math.PI/6)*Math.sin(c);

//计算五个点的位置,以右下点为(X0,Y0)

g.drawLine((int)x0, (int)y0, (int)x2, (int)y2);

g.drawLine((int)x2, (int)y2, (int)x3, (int)y3);

g.drawLine((int)x3, (int)y3, (int)x4, (int)y4);

g.drawLine((int)x4, (int)y4, (int)x0, (int)y0);

g.drawLine((int)x2, (int)y2, (int)x5, (int)y5);

g.drawLine((int)x5, (int)y5, (int)x3, (int)y3);

//划线――注意方法所需要的数据类型

Show(x2,y2,l*Math.cos(Math.PI/6),a+Math.PI/6,b+Math.PI/6,c+Math.PI/6,count-1,g);

Show(x5,y5,l*Math.sin(Math.PI/6),a-Math.PI/3,b-Math.PI/3,c-Math.PI/3,count-1,g);

//进行递归调用(注意传到方法里的点是相对于新正方形的右下点)

}

}

[1] [2]

test.jsp?url=http%3A%2F%2Fwww.chinaitlab.com%2Fcms%2Fimages%2Flogo14.gif&refer=http%3A%2F%2Fjava.chinaitlab.com%2Fadvance%2F945152_2.html

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐