java画谢尔宾斯基三角形

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

  //6、定义重绘方法,拖动窗口,改变窗口大小后,绘制的图片仍然留在窗体上

  public void paint(Graphics g){

  super.paint(g);

  Show(100,600,900,600,500,30,200,235,235,10,g);//调用递归函数Show(),所涉及的参数:

  // x1的位置,y1的位置,x2的位置,y2的位置,x3的位置,y3的位置,

  // (以下三个属性是颜色控制,如果需要渐变色彩的话)

  }

  public void Show(double x,double y,double x2,double y2,double x3,double y3,

  int a,int b,int c,int count,Graphics g){

  int tempx = (int)x;

  int tempy = (int)y;

  int tempx2 = (int)x2;

  int tempy2 = (int)y2;

  int tempx3 = (int)x3;

  int tempy3 = (int)y3;

  if(a< 0||b< 0||c< 0)

  {

  a=355;

  b=155;

  c=35;

  }

  //计算出三点的位置

  g.setColor(new Color(a,b,c));

  g.drawLine(tempx,tempy,tempx2,tempy2);

  g.drawLine(tempx,tempy,tempx3,tempy3);

  g.drawLine(tempx2,tempy2,tempx3,tempy3);

  if(count>=2){//判断是否进行递归

  int xm = tempx;

  int ym = tempy;

  int xm2 = tempx2;

  int ym2 = tempy2;

  int xm3 = tempx3;

  int ym3 = tempy3;

  tempx = (xm+xm2)/2;

  tempy = (ym+ym2)/2;

  tempx2 = (xm+xm3)/2;

  tempy2 = (ym+ym3)/2;

  tempx3 = (xm3+xm2)/2;

  tempy3 = (ym3+ym2)/2;

  this.Show(xm,ym,tempx,tempy,tempx2,tempy2,a-10,b-16,c-10,count-1,g);

  this.Show(tempx,tempy,xm2,ym2,tempx3,tempy3,a-14,b-16,c-18,count-1,g);

  this.Show(xm3,ym3,tempx2,tempy2,tempx3,tempy3,a-17,b-14,c-0,count-1,g);

  //由于下次分形要分成三个所以要在一次Show调用自己三次

  g.drawLine(tempx,tempy,tempx2,tempy2);

  g.drawLine(tempx,tempy,tempx3,tempy3);

  g.drawLine(tempx2,tempy2,tempx3,tempy3);

  }

  else{

  return ;

  }

  }

  }

[1][2]

世界上那些最容易的事情中,拖延时间最不费力。

java画谢尔宾斯基三角形

相关文章:

你感兴趣的文章:

标签云: