用递归画的有意思的图形
来源:互联网 发布:淘宝休闲桌椅价格 编辑:程序博客网 时间:2024/04/30 21:29
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);}}@Overridepublic 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);}}@Overridepublic 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();}}
这个在重绘的递归绘画的方法里修改了坐标形成高度对称的图形
0 0
- 用递归画的有意思的图形
- 有意思的MSN图形
- 有意思的递归
- 一个有意思的递归-字符串
- 一个有意思的递归定义
- 一些有意思的数据图形化分析
- 两个有意思的递归小程序
- 两个有意思的递归小程序
- 两个有意思的递归小程序
- for循环嵌套的几种有意思的图形
- 有意思的frameset
- 很有意思的贺卡
- 真的挺有意思
- 有意思的计算题
- 有意思的一个算法。
- 有意思的问答
- 有意思的email
- 非常有意思的试题
- 第一个Java程序经常出现的问题(一)
- POJ 1330 (LCA的在线算法,转RMQ用ST算法)
- Java Learning
- iOS面试题2(答案)
- 从jar包中读取资源文件
- 用递归画的有意思的图形
- 在HortonWorks HDP 2.1 和2.2 集群间进行数据迁移 包括(Hive数据表)
- [LeetCode OJ][006]ZigZag Conversion
- 【持续更新】D3 的学习资料
- Deep Learning(深度学习)学习笔记整理系列(六)
- 数据结构和算法(一):冒泡排序和二分查找
- Deep Learning(深度学习)学习笔记整理系列(七)
- Effective Java——类和接口(下)
- Android 手机上获取物理唯一标识码