java实现Julia_ifs

利用java实现分形图形,按照书上面码的!记录一下。
Julia_ifs.java

package wyc;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Event;
import java.awt.Color;
import java.applet.Applet;

public class julia_ifs extends Applet implements Runnable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 0L;`	
	Thread drawThread = null;
	
	int h,w;
	int depth=4;
	double t = 1.5, b=-1.5, l=-2.0,r= 2.0;
	boolean drawing=false;
	
	double c_x=-0.77;//这里的x,y值修改后分形图像改变
	double c_y=0.08;
	
	byte map[][];

	public void init ()
	{
		String s;
		setBackground (new Color (0,51,204)) ; //没置背景色为白色
		s= getParameter ("depth") ; //得到depth
		if (s!=null) depth=Integer.valueOf(s).intValue();
		
		s = getParameter ( "c_x") ;  //得到C的实部
		if (s != null) c_x= Double.valueOf(s).doubleValue();
		
		s= getParameter ("c_y"); //得到C的虚部		  
		if (s != null) c_y = Double.valueOf(s).doubleValue();
		
		s= getParameter ("bgcolor") ;//得到背景颜色		  
		if (s != null) 
			setBackground (new Color (Integer.valueOf(s,16).intValue()));
	}	
				
public void stop ()
{
	drawThread = null;
}

public boolean action(Event e)
{
	switch (e.id)
	{
	   case Event.WINDOW_DESTROY:
		   System.exit(0);
		   return true;
	   default:
	   return false;
	}
}

public boolean mouseMove(Event e,int x,int y)
{
	double x1,y1;
	
	x1=(double)x/w*(r-l)+l;
	y1=(double)y/h*(b-t)+t;
	
	showStatus("("+x1+","+y1+")");
	return false;
}

public boolean mouseup(Event e,int x,int y)
{
	if(!drawing)
	{
		c_x=(double)x/w*(r-l)+l;
		c_y=(double)y/h*(b-t)+t;
		repaint();
		return true;
	}
	else
		return false;
}

int transX(double x)
{
	return(int)((double)(x-l)/(r-l)*w);
}

int transY(double y)
{
	return(int)((double)(y-t)/(b-t)*h);
}

Graphics g;

public void paint (Graphics g)
{
	this.g = g.create();
	drawThread = new Thread(this,"Draw it") ;
	drawThread.start();
}

public void run()
{
	Rectangle rect=bounds();
	drawing=true;
	h=rect.height;w=rect.width;
	map=new byte[w][h];
	plot(1.0,1.0);
	map=null;
	drawing=false;
}

void plot(double x,double y)
{
	double r,newx,newy;
	int sgn;
	int xtmp=transX(x),ytmp=transY(y);
	
	map[xtmp][ytmp]++;
	g.drawLine(xtmp, ytmp, xtmp, ytmp);
	drawThread.yield();
	
	if(map[xtmp][ytmp]<depth)
	{
		x-=c_x;
		y-=c_y;
		r=Math.sqrt(x*x+y*y);
		sgn=(y>0)?1:(y<0)?-1:0;
		newx=Math.sqrt((r+x)/2);
		newy=Math.sqrt((r-x)/2)*sgn;
		
		plot(newx,newy);
		plot(-newx,-newy);
	}

 }
}
		



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值