Java递归与图形绘制
一个简单的递归函数
public class DrawInt {
int a=10;
public void Init() {
if(a>0) {
System.out.print(a+"\n");
a--;
Init();
}
}
public static void main(String[] args) {
DrawInt dr=new DrawInt();
dr.Init();
}
}
输出为
10
9
8
7
6
5
4
3
2
1
使用该方法进行图形绘制
1、建立绘图窗口
package csl0331;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.Graphics;
public class DrawInt {
public void Init() {
JFrame jf=new JFrame();
jf.setTitle("递归图形");
jf.setSize(1200,800);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout fl=new FlowLayout();
jf.setLayout(fl);
JButton jb1=new JButton("递归");
jf.add(jb1);
jf.setVisible(true);
Graphics gr=jf.getGraphics();
DrawMouse mouse=new DrawMouse(gr);
jb1.addActionListener(mouse);
}
public static void main(String[] args) {
DrawInt dr=new DrawInt();
dr.Init();
}
}
2、绘制图形1
package csl0331;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class DrawMouse implements ActionListener{
public Graphics g;
public DrawMouse(Graphics g) {
this.g=g;
}
public void actionPerformed(ActionEvent e) {
this.initer1(100,100,300,300,8);
}
public void initer1(int x1,int y1,int x2,int y2,int count){
if(count>1) {
g.drawLine(x2,y1,x2/2+x1/2,y2/2+y1/2);
g.drawLine(x2,y1,3*x2/2-x1/2,y2/2+y1/2);
g.drawLine(x2/2+x1/2,y2/2+y1/2,3*x2/2-x1/2,y2/2+y1/2);
count=count-1;
initer1(x1,y1,x2/2+x1/2,y1/2+y2/2,count);
initer1(x2/2+x1/2,y1/2+y2/2,x2,y2,count);
initer1(x2,y1,3*x2/2-x1/2,y2/2+y1/2,count);
}
else {
g.drawLine(x1,y1,x2,y2);
g.drawLine(x1,y1,2*x2-x1,y1);
g.drawLine(x2,y2,2*x2-x1,y1);
System.out.println("x1:"+x1+" y1:"+y1);
}
}
}
输出结果
3、绘制图形2
package csl0331;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class DrawMouse implements ActionListener{
public Graphics g;
public DrawMouse(Graphics g) {
this.g=g;
}
public void actionPerformed(ActionEvent e) {
this.initer2(600,100,300,10);
}
public void initer2(int x1,int y1,int width,int count) {
if(count>1) {
g.drawRect(x1+width/3, y1+width/3, width/3, width/3);
count=count-1;
initer2(x1,y1, width/3,count);
initer2(x1+width/3,y1, width/3,count);
initer2(x1+2*width/3,y1, width/3,count);
initer2(x1,y1+width/3, width/3,count);
initer2(x1+2*width/3,y1+width/3, width/3,count);
initer2(x1,y1+2*width/3, width/3,count);
initer2(x1+width/3,y1+2*width/3, width/3,count);
initer2(x1+2*width/3,y1+2*width/3, width/3,count);
}
else{
}
}
}
输出图形
4、绘制山脉
package csl0331;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class DrawMouse implements ActionListener{
public Graphics g;
public DrawMouse(Graphics g) {
this.g=g;
}
public void actionPerformed(ActionEvent e) {
this.initer3(10,600,522,600,200);
}
public void initer3(int x1,int y1,int x2,int y2,int n) {
if(n>0) {
Random r=new Random();
int y3=r.nextInt(2*n)-n+y1/2+y2/2;
int x3=x1/2+x2/2;
if(n<2) {
g.drawLine(x1, y1, x2, y2);
System.out.println("x1:"+x1+" y1:"+y1+" x2:"+x2+" y2:"+y2);
}
n=(int)(0.4*n);
initer3(x1,y1,x3,y3,n);
initer3(x3,y3,x2,y2,n);
}
else {
}
}
}
输出图形