1.套公式的简单分形
因为觉得这个好漂亮,于是很想画出来,公式已经给出,只要套入程序递归就可以。先初始化常数的值,再代入公式计算,画点的时候注意将类型转化为整型,再改变其值,继续调用。
公式: d*Math.sin(a*x1)-Math.sin(b*y1);
c*Math.cos(a*x1)+Math.cos(b*y1);
这是显示窗体的类:
public class Beati {
public static void main(String [] args){
Beati window = new Beati();
window.showGUI();
}
public void showGUI(){
JFrame window = new JFrame();
window.setTitle("Draw");
window.setSize(600,600);
window.setLocation(200,200);
window.setLayout(new FlowLayout());
JButton bu =new JButton("我是艺术家");
window.add(bu);
window.setVisible(true);
Graphics g;
g=window.getGraphics();
Listener al =new Listener(g);
bu.addActionListener(al);
}
}
这是监听器的类:
public class Listener implements ActionListener{
private Graphics g;
double a,b,c,d;
double x1=0;
double y1=0;
double x2,y2;
public Listener(Graphics g){
this.g=g;
}
public void actionPerformed(ActionEvent e){
a = 1.40;b = 1.56;c = 1.40; d = -6.56;
for(int i=0;i<12133;i++){
for(int t=0;t<80;t++){
Color co = new Color(t++,t*2,t*3);
g.setColor(co); }
double x2=d*Math.sin(a*x1)-Math.sin(b*y1);
double y2=c*Math.cos(a*x1)+Math.cos(b*y1);
int x22= (int) (x2*30)+300;
int y22=(int) (y2*30)+200;
System.out.println(x22+","+y22);
g.drawLine(x22,y22,x22,y22);
x1=x2;
y1=y2;}
}
}