用递归画的有意思的图形

本文介绍了一种使用Java Swing实现的图形生成方法,通过递归调用绘画线条来生成复杂的图形。用户可以通过按钮控制递归深度,进而改变图形的复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Draw extends JFrame implements ActionListener{
	int deepth=1;
	public Draw(){
		//创建窗体
		this.setSize(1000,700);
		this.setTitle("风形");
		this.setLocationRelativeTo(null);
		this.getContentPane().setBackground(Color.black);
		this.setDefaultCloseOperation(3);
		this.setLayout(new FlowLayout());
		JButton button_add=new JButton("增加深度");
		JButton button_sub=new JButton("减少深度");
		this.add(button_add);
		this.add(button_sub);
		button_add.addActionListener(this);
		button_sub.addActionListener(this);
		this.setVisible(true);
//		repaint();
	}	
		
//		int x1=x/3,y1=y/3;
//		int x2=x*2/3,y2=y/3;
	public void paint(Graphics g){
		super.paint(g);
		int y=getHeight();
		int x=getWidth();
	
		g.setColor(Color.red);
		change(g,x/3,y/3,2*x/3,y/3,deepth);
		Font f=new Font("",Font.ITALIC,30);
		g.setFont(f);
		g.drawString("深度="+deepth, 700, 100);
	
	}
	public void change(Graphics g,int x1,int y1,int x2,int y2,int deepth){
		if(deepth<2){
			//g.setColor(Color.red);
			g.drawLine(x1, y1, x2, y2);
			//System.out.println("x1="+x1+"y1="+y1);
		}else{
			int x3 = (x1 + y1 + x2 - y2) / 2;
			int y3 = (x2 + y2 + y1 - x1) / 2;
			change(g,x1,y1,x3,y3,deepth-1);
			change(g,x2,y2,x3,y3,deepth-1);
		}
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
			String label=e.getActionCommand();
			if(label.equals("增加深度")){
				deepth++;
			
			}else{
				deepth--;
				
			}
			repaint();
	}
	public static void main(String[] args){
		new Draw();
	}
}

Draw继承JFrame创建一个窗口

核心是递归方法public void change(Graphics g,int x1,int y1,int x2,int y2,int deepth)

通过深度的控制让电脑作几次递归画图,从而形成好看的图形

每一次增加深度都是在之前图形的基础上添加线条,而且所有线条都是重新绘画




点击打开链接

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Draw2 extends JFrame implements ActionListener{
	int deepth=1;
	JButton button_add;
	public Draw2(){
		setTitle("漂亮不");
		setSize(1000,800);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(3);
		this.getContentPane().setBackground(Color.black);
		//设置布局
		setLayout(new FlowLayout());
		 button_add=new JButton("增加深度");
		JButton button_sub=new JButton("减少深度");
		button_add.setBackground(Color.RED);
		button_sub.setBackground(Color.RED);
		
		
		add(button_add);
		add(button_sub);
		button_add.addActionListener(this);
		button_sub.addActionListener(this);
		//设置可见
		setVisible(true);
	}
	public void paint(Graphics g){
		super.paint(g);
		int x=getWidth();
		int y=getHeight();
		Font f=new Font("",Font.ITALIC,30);
		g.setFont(f);
		g.setColor(Color.red);
		g.drawString("深度="+deepth,800,100);
		
		change(g, x/3, y/3, x*2/3, y/3, deepth);
		
	}
	public void change(Graphics g,int x1,int y1,int x2,int y2,int deepth){
		if(deepth<2){
			g.drawLine(x1,y1,x2,y2);
		}else{
			g.drawLine(x1,y1,x2,y2);
			int x3 = (x1 + y1 + x2 - y2) / 2;
			int y3 = (x2 + y2 + y1 - x1) / 2;
			change(g,x1,y1,x3,y3,deepth-1);
			change(g,x3,y3,x2,y2,deepth-1);
		}
		
		
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		//String label=e.getActionCommand();
		if(e.getSource()==button_add){
			deepth++;
		}else{
			deepth--;
		}
		repaint();
	}
	public static void main(String []args){
		
		
		new Draw2();
	}
}

这个在重绘的递归绘画的方法里修改了坐标形成高度对称的图形

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值