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();
}
}