Java画布的简化,保存迭代递归画图

基于前面一直在写这个Java画布,我们发现一个特点,那就是按钮好多,颜色好多,所以本节就是要简化我们的画布程序,从而也实现一个复习。
1.关于简化
相信学过编程语言的同学都知道数组,因为不同的图形,不同的颜色就要创建一个按钮,所以我们想能不能创建一个按钮的数组呢!如下:

  String[] shape={"直线1","直线2","矩形","三角形","椭圆","多边形","曲线","橡皮擦","迭代","递归"};
  Color[] color={Color.BLACK,Color.LIGHT_GRAY,Color.GREEN,Color.GRAY,Color.PINK};

在这里插入图片描述
加按钮颜色的时候直接一个for循环即可。

		for(int i=0;i<shape.length;i++)
		{
			JButton jbu=new JButton(shape[i]);
			jp.add(jbu);
			jbu.addActionListener(mouse);
		}
		for(int i=0;i<color.length;i++)
		{
			JButton jbu=new JButton();
			jbu.setBackground(color[i]);
			jbu.setPreferredSize(new Dimension(30,30));
			jp.add(jbu);
			jbu.addActionListener(mouse);
		}

简化就到这。
2.保存
JFrame里面有个默认的绘图法pait,所以每次一旦改变窗体大小或者把它收到最小化,他就会重绘默认的界面,没有保存我们画出的图形。那么我们就会想办法保存我们画的图形,首先我们要知道保存的内容是什么:换个角度来说,我们是怎么画出来这个图形。

  1. 保存坐标
  2. 保存画的方法
  3. 保存颜色
  4. 重绘
    所以我们可以设计一个类,用来写一些绘画方法。
public class Shape {
	private Shape[] arrayShape=new Shape[9000];
	private int x1,x2,y1,y2;
	private String name;
	private Color color;
	public Shape(int x1,int y1,int x2,int y2,String name,Color color)
	{
		this.x1=x1;
		this.x2=x2;
		this.y1=y1;
		this.y2=y2;
		this.name=name;
		this.color=color;
	}
	public void drawShape(Graphics g)
	{
		switch(name)
		{
			case "直线":
				g.setColor(color);
				g.drawLine(x1, y1, x2, y2);
				
				break;
			case "矩形":
				g.setColor(color);
				g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
				break;
			case "椭圆":
				g.setColor(color);
				g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
				System.out.println("000");
				break;
			case "橡皮擦":
				g.setColor(color);
				g.drawRect(x1, y1, x2, y2);
				break;
		}
	}
}

PS:在这里提一下this的用法,this很简单,就是表示本来的意思。
当我们存储的时候就类似这样:

		if ("直线1".equals(name)) {
			gr.drawLine(x1, y1, x2, y2);
			Shape shape=new Shape(x1,y1,x2,y2,"直线",color);
			arrayShape[index++]=shape;
		}

一个图形数组来存储

	private Shape[] arrayShape=new Shape[9000];

重绘方法需要把数组里的内容全画出来

	public void paint(Graphics g)
	{
		super.paint(g);
		for(int i=0;i<arrayShape.length;i++)
		{
			Shape shape= arrayShape[i];
			if(shape!=null)
				shape.drawShape(g);
			else
				break;
		}
	}

就这样大功告成了,也许讲得也没有那么清晰,欢迎留言。
3.画迭代递归图形
(1)迭代
在这里插入图片描述

	public void iterate(double x,double y)
	{
		a=-1.2;b=1.6;c=-1;d=-1.5;
		for(int i=0;i<25500;i++)
		{
			double tex=Math.sin(a*y)+c*Math.cos(a*x);
			double tey=Math.sin(b*x)+c*Math.cos(b*y);
			int x3=(int)(tex*130+400);
			int y4=(int)(tey*130+400);
			gr.setColor(new Color(i/20,i/100,i/100));
			gr.drawLine(x3, y4, x3, y4);
			x=tex;
			y=tey;
		}
	}
根据abcd的值不同,迭代图形也不同;你们可以自己尝试一下

递归

	public void drawkoach(Graphics g,double a,double b,double c,double d,int count)
	{
		if(count<=1)
		{
			g.drawLine((int)a,(int)b,(int)c,(int)d);
		}
		else
		{
			double x6=(2*a+c)/3;
			double y6=(2*b+d)/3;
			double x4=(a+2*c)/3;
			double y4=(b+2*d)/3;
			double k=(a-c)*(b-d);
			double x5=a,y5=b;
			if(y6==y4)
			{
				x5=(x6+x4)/2;
				y5=y6-(x4-x6)*Math.sqrt(3)/2;
			}
			else if(k<0)
			{
		           x5 = a;
		           y5 = y4;
			}
			else if(k>0)
			{
		           x5 = c;
		           y5 = y6;
			}
			if(x6==x4)
			{
				x5=x6;
				y5=y6;
			}
			drawkoach(g,x6,y6,x5,y5,count-1);
			drawkoach(g,x5,y5,x4,y4, count-1);
			drawkoach(g,a,b,x6,y6, count-1);
			drawkoach(g,x4,y4,c,d, count-1);
		}
	}

这个画出的一个是雪花,但是不知道为啥我只画出了半个雪花。若读者知道为啥可以给我留个言。/哭笑不得

猜你喜欢

转载自blog.csdn.net/chan_fan/article/details/83477800
今日推荐