分形,递归

逻辑:
顺序
分支
循环

      system.out……
  for(int i=0;i<n;i++){
      system.out……(若没输出,则循环条件没成立过)
  }
 
递归
  终止方法,写return就可以。
1.算法通用。
2.有终止条件。
不是调用自身,而是调用的方法的内容和自身相同。

堆:一般是一种无序结构,一般保持创建的对象。heap
栈溢出

死递归会导致程序的退出
死循环不会

工具类
  暂停200毫秒
  try{
     Thread.sleep(200);
  }catch(Exception ef){}
  滑动条
  java.swing.JSlider js=new java.swing.JSlider();
  this.add(js);
  js.setValue(255);
    取得值 js.getValue();
  SliderListener sl=new SliderListener();
  设置最大值,最小值,js.setMaximum(10);
 
  js.addChangeListener(sl);

三角形代码:
 

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;



public class triangle extends JFrame{

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
triangle tr=new triangle();
tr.InitGUI();
}
private void InitGUI() {
// TODO Auto-generated method stub
// this.setTitle("三角形");
this.setSize(new Dimension(700,600));
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setVisible(true);
Graphics g=this.getGraphics();
MouseListener bl=new Listener(g);
this.addMouseListener(bl);

}

}
监听器listener:
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Listener implements MouseListener {

private Graphics g;
public Listener(Graphics g){
this.g=g;
}

public void draw(Graphics g,int a1,int a2,int b1,int b2,int c1,int c2, int count){
int d1,d2,e1,e2,f1,f2;
count++;
g.drawLine(a1, a2, b1, b2);
g.drawLine(c1, c2, b1, b2);
g.drawLine(a1, a2, c1, c2);
if (count>=6){return;}
d1=(a1+b1)/2;
d2=(a2+b2)/2;
e1=(a1+c1)/2;
e2=(a2+c2)/2;
f1=(c1+b1)/2;
f2=(c2+b2)/2;
draw(g,a1,a2,e1,e2,d1,d2,count);
draw(g,b1,b2,d1,d2,f1,f2,count);
draw(g,c1,c2,e1,e2,f1,f2,count);
}

@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
draw(g,300,50,100,(int)(200*Math.sqrt(3))+50,500,(int)(200*Math.sqrt(3))+50,0);
}

@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}

}

正方形代码:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;



public class square extends JFrame{

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
square tr=new square();
tr.InitGUI();
}
private void InitGUI() {
// TODO Auto-generated method stub

this.setSize(new Dimension(700,700));
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setVisible(true);
Graphics g=this.getGraphics();
MouseListener bl=new Listener3(g);
this.addMouseListener(bl);

}

}

监听器:
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Listener3 implements MouseListener {

private Graphics g;
public Listener3(Graphics g){
this.g=g;
}

public void draw(Graphics g,int x1,int x2,int h,int w, int count){

if (count==0){g.draw3DRect(x1, x2, h, w, false);}
count++;
g.fill3DRect(x1+(int)(h/3), x2+(int)(h/3), (int)(h/3), (int)(h/3), false);
if (count>=6){return;}
draw(g,x1,x2,(int)(h/3),(int)(h/3),count);
draw(g,x1+(int)(h/3),x2,(int)(h/3),(int)(h/3),count);
draw(g,x1,x2+(int)(h/3),(int)(h/3),(int)(h/3),count);
draw(g,x1+(int)(h/3),x2+(int)(h/3),(int)(h/3),(int)(h/3),count);

draw(g,x1+(int)(h/3)*2,x2,(int)(h/3),(int)(h/3),count);
draw(g,x1+(int)(h/3),x2+(int)(h/3)*2,(int)(h/3),(int)(h/3),count);
draw(g,x1+(int)(h/3)*2,x2+(int)(h/3)*2,(int)(h/3),(int)(h/3),count);

draw(g,x1,x2+(int)(h/3)*2,(int)(h/3),(int)(h/3),count);
draw(g,x1+(int)(h/3)*2,x2+(int)(h/3),(int)(h/3),(int)(h/3),count);

}

@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
draw(g,50,50,600,600,0);
}

@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub

}

}

你可能感兴趣的:(递归)