//画谢宾斯基三角形的方法
private Graphics g; //画布对象要实例化
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
public void coordinate(double x1,double y1,double x2,double y2,double x3,double y3,int con)
{
if(con<1){
return;
}
this.x1=(int)x1;
this.x2=(int)x2;
this.x3=(int)x3;
this.y1=(int)y1;
this.y2=(int)y2;
this.y3=(int)y3;
//只是设置颜色 并且颜色与坐标有关
g.setColor(new Color(0,this.x1%255,this.y1%255));
//画一个三角
g.drawLine(this.x1, this.y1, this.x2, this.y2);
g.drawLine(this.x1, this.y1, this.x3, this.y3);
g.drawLine(this.x2, this.y2, this.x3, this.y3);
//计算出下一次画三角的坐标
double x13=(x1+x3)/2;
double y13=(y1+y3)/2;
double x12=(x1+x2)/2;
double x23=(x2+x3)/2;
double y23=(y2+y3)/2;
//计数器
con--;
coordinate(x1,y1,x12,y1,x13,y13,con);
coordinate(x12,y1,x2,y2,x23,y23,con);
coordinate(x13,y13,x23,y23,x3,y3,con);
}[/code]
调用这个方法并传参数 实例化画布对象 可以得到
[img]http://dl2.iteye.com/upload/attachment/0086/7078/e8570d1f-d8ac-3ed8-81b2-4d417c70c2e7.png[/img]
private Graphics g; //画布对象要实例化
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
public void coordinate(double x1,double y1,double x2,double y2,double x3,double y3,int con)
{
if(con<1){
return;
}
this.x1=(int)x1;
this.x2=(int)x2;
this.x3=(int)x3;
this.y1=(int)y1;
this.y2=(int)y2;
this.y3=(int)y3;
//只是设置颜色 并且颜色与坐标有关
g.setColor(new Color(0,this.x1%255,this.y1%255));
//画一个三角
g.drawLine(this.x1, this.y1, this.x2, this.y2);
g.drawLine(this.x1, this.y1, this.x3, this.y3);
g.drawLine(this.x2, this.y2, this.x3, this.y3);
//计算出下一次画三角的坐标
double x13=(x1+x3)/2;
double y13=(y1+y3)/2;
double x12=(x1+x2)/2;
double x23=(x2+x3)/2;
double y23=(y2+y3)/2;
//计数器
con--;
coordinate(x1,y1,x12,y1,x13,y13,con);
coordinate(x12,y1,x2,y2,x23,y23,con);
coordinate(x13,y13,x23,y23,x3,y3,con);
}[/code]
调用这个方法并传参数 实例化画布对象 可以得到
[img]http://dl2.iteye.com/upload/attachment/0086/7078/e8570d1f-d8ac-3ed8-81b2-4d417c70c2e7.png[/img]