Java递归与图形绘制

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 {
		}
	}
}

输出图形
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值