Ryoichi Mizuno - Scientific Computer Graphics | Upper Directory |

//////////////////////////////////////////////////////////
/* Bezier Surface, Copyright 2001-2010 Ryoichi Mizuno   */
/* ryoichi[at]mizuno.org                                */
/* Dept. of Complexity Science and Engineering          */
/* at The University of Tokyo                           */
//////////////////////////////////////////////////////////
spacer
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class logisticMap extends Applet implements Runnable{

	Thread th;
	Image original_i,buf_i,info_i;
	Graphics original_g,buf_g,info_g;
	boolean goFlag=true,dragFlag=false;
	int w,h,x_margin=35,y_margin=15;
	Button db,hf,reset;
	int mx,my,sx0,sy0,sx1,sy1;
	double a_now,x_now;
	
	int plot_max=30000;
	int rep_max=1000;
	int xp[]=new int[plot_max];
	int ap[]=new int[plot_max];
	double a_min,a_max,x_min,x_max;
	Applet applet = this;
	
	public void init(){
		a_min=2.8;a_max=4.0;x_min=0.0;x_max=1.0;
		//get screen size
		w=getSize().width;
		h=getSize().height;
		//redefine w and h for buffer layer
		w-=20; h-=50;
		//create buffer layer
		buf_i=createImage(w,h);
		buf_g=buf_i.getGraphics();
		//create original (temp.) layer
		original_i=createImage(w,h);
		original_g=original_i.getGraphics();
		//create info layer
		info_i=createImage(100,10);
		info_g=info_i.getGraphics();
		//GUI
		setLayout(new FlowLayout(1,20,h+20));
		add(db=new Button("X2"));
		add(hf=new Button("/2"));
		add(reset=new Button("reset"));
		db.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
			    db_fig();
			}
		    });
		hf.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
			    hf_fig();
			}
		    });
		reset.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
			    reset_fig();
			}
		    });
		//mouse
		addMouseMotionListener(
							   new MouseMotionAdapter(){
								   public void mouseMoved(MouseEvent e){
									   mx=e.getX(); my=e.getY();
										//ajustment for out of range
										if(mx<x_margin) mx=x_margin;
										if(mx>w-x_margin) mx=w-x_margin;
										if(my<y_margin) my=y_margin;
										if(my>h-y_margin) my=h-y_margin;
										//set max and min for a and x
									   a_now=(double)(mx-x_margin)/(double)(w-x_margin*2)*(a_max-a_min)+a_min;
									   x_now=x_max-(double)(my-y_margin)/(double)(h-y_margin*2)*(x_max-x_min);
									   info_g.setColor(Color.white);
									   info_g.fillRect(0,0,w,10);
									   info_g.setColor(Color.black);
									   info_g.drawString("a="+java.lang.Double.toString(a_now).substring(0,5)+", x="+java.lang.Double.toString(x_now).substring(0,5),0,10);
									   repaint();
								   }
								   public void mouseDragged(MouseEvent e){
									   int i;
									   dragFlag=true;
									   mx=e.getX(); my=e.getY();
									   int vert_of_range_x[]={sx0,mx,mx,sx0};
									   int vert_of_range_y[]={sy0,sy0,my,my};
									   //ajustment for out of range
									   for(i=0;i<4;i++){
										   if(vert_of_range_x[i]<x_margin)vert_of_range_x[i]=x_margin;
										   if(vert_of_range_x[i]>w-x_margin)vert_of_range_x[i]=w-x_margin;
										   if(vert_of_range_y[i]<y_margin)vert_of_range_y[i]=y_margin;
										   if(vert_of_range_y[i]>h-y_margin)vert_of_range_y[i]=h-y_margin;
									   }
									   //Image buf_iをImage original_iにしたい...
									   buf_g.drawImage(original_i,0,0,applet);
									   buf_g.setColor(Color.blue);
									   buf_g.drawPolygon(vert_of_range_x,vert_of_range_y,4);
									   a_now=(double)(mx-x_margin)/(double)(w-x_margin*2)*(a_max-a_min)+a_min;
									   x_now=x_max-(double)(my-y_margin)/(double)(h-y_margin*2)*(x_max-x_min);
									   info_g.setColor(Color.white);
									   info_g.fillRect(0,0,w,10);
									   info_g.setColor(Color.black);
									   info_g.drawString("a="+java.lang.Double.toString(a_now).substring(0,5)+", x="+java.lang.Double.toString(x_now).substring(0,5),0,10);
									   repaint();
								   }
			}
							   );
		addMouseListener(
									 new MouseAdapter(){
										 public void mousePressed(MouseEvent e){
											sx0=e.getX(); sy0=e.getY();
											//現在のImage buf_iをImage original_iに格納したい...
											original_g.drawImage(buf_i,0,0,applet);
										//System.out.println(sx0);
										 }
										 public void mouseReleased(MouseEvent e){
											 if(dragFlag){
												 double a_min_n,a_max_n,x_min_n,x_max_n;
												 int s_buf;
												 sx1=e.getX(); sy1=e.getY();
												 if(sx0>sx1){
													 s_buf=sx0; sx0=sx1; sx1=s_buf;
												 }
												 if(sy0>sy1){
													 s_buf=sy0; sy0=sy1; sy1=s_buf;
												 }
												 //ajustment for out of range
												 if(sx0w-x_margin) sx0=w-x_margin; if(sx1>w-x_margin) sx1=w-x_margin;
												 if(sy0<y_margin) sy0=y_margin; if(sy1<y_margin) sy1=y_margin;
												 if(sy0>h-y_margin) sy0=h-y_margin; if(sy1>h-y_margin) sy1=h-y_margin;
												 //set max and min for a and x
												 a_min_n=(double)(sx0-x_margin)/(double)(w-x_margin*2)*(a_max-a_min)+a_min;
												 a_max_n=(double)(sx1-x_margin)/(double)(w-x_margin*2)*(a_max-a_min)+a_min;
												 x_min_n=x_max-(double)(sy1-y_margin)/(double)(h-y_margin*2)*(x_max-x_min);
												 x_max_n=x_max-(double)(sy0-y_margin)/(double)(h-y_margin*2)*(x_max-x_min);
												 a_min=a_min_n; a_max=a_max_n;
												 x_min=x_min_n; x_max=x_max_n;
												 dragFlag=false;
												 goFlag=true;
												 repaint();
											 }
										 }
			}
									 );
	setBackground(Color.white);
	}
	
    public void start(){
		th=new Thread(this);
		th.start();
    }
    
    public void stop(){
		goFlag=false;
    }
    
    public void run(){
		while(goFlag){
		    repaint();
		    try{Thread.sleep(10);}
		    catch(InterruptedException e){}
		}	
	}
    
    public void update(Graphics g){
		this.paint(g);
    }

    public void paint(Graphics g){
		if(goFlag){
			//initialize buffer layer
			init_b();
			//draw grid
			grid(a_min,a_max,x_min,x_max);
			//draw logistic
			logistic(a_min,a_max,x_min,x_max);
			//draw blank info layer
			no_info();
			g.drawImage(info_i,x_margin+20,30,this);
			//show buffer layer
			g.drawImage(buf_i,10,10,this);
		}
		else if(dragFlag){
			//show buffer layer
			//g.drawImage(original_i,10,10,this);
			g.drawImage(buf_i,10,10,this);
		}
		//show info layer
		g.drawImage(info_i,x_margin+20,30,this);
    }
	
    public void init_b(){
		buf_g.setColor(Color.white);
		buf_g.fillRect(0,0,w,h);
    }

	public void grid(double a_min,double a_max,double x_min,double x_max){
		String str_a_min=java.lang.Double.toString(a_min)+"00000";
		String str_a_max=java.lang.Double.toString(a_max)+"00000";
		String str_x_min=java.lang.Double.toString(x_min)+"00000";
		String str_x_max=java.lang.Double.toString(x_max)+"00000";
		buf_g.setColor(Color.black);
		buf_g.drawRect(x_margin,y_margin,w-x_margin*2,h-y_margin*2);
		buf_g.drawString(str_a_min.substring(0,5),x_margin,h-1);
		buf_g.drawString(str_a_max.substring(0,5)+"[a]",w-x_margin*2+5,h-1);
		buf_g.drawString(str_x_min.substring(0,5),5,h-y_margin*2+10);
		buf_g.drawString(str_x_max.substring(0,5),5,y_margin+15);
		buf_g.drawString("[x]",5,y_margin);
	}
	
	public void logistic(double a_min,double a_max,double x_min,double x_max){
		int plot,rep;
		double a,x0,x1;
		for(plot=0;plot<plot_max;plot++){
			a=(a_max-a_min)*(double)plot/(double)plot_max+a_min;
			//System.out.println(a);
			ap[plot]=(int)((a-a_min)*(double)(w-x_margin*2)/(a_max-a_min))+x_margin;
			x0=Math.random();
			for(rep=0;rep<rep_max;rep++){
				x1=a*x0*(1-x0);
				x0=x1;
			}
			//System.out.println(x0);
			xp[plot]=(int)((double)(h-y_margin*2)-((x0-x_min)*(double)(h-y_margin*2)/(x_max-x_min)))+y_margin;
		}
		draw_logistic();
		goFlag=false;
	}
	
	public void draw_logistic(){
		int plot;
		buf_g.setColor(Color.red);
		for(plot=0;plot<plot_max;plot++){
			//System.out.println(ap[plot]+", "+xp[plot]);
			if(xp[plot]>=y_margin&&xp[plot]<=(h-y_margin)){
				buf_g.drawLine(ap[plot],xp[plot],ap[plot],xp[plot]);
			}
		}
	}

	public void no_info(){
							info_g.setColor(Color.white);
							info_g.fillRect(0,0,w,10);
							info_g.setColor(Color.black);
					   }
	
	public void db_fig(){
		double a_range=(a_max-a_min)/2;
		double x_range=(x_max-x_min)/2;
		a_min+=a_range/2; a_max-=a_range/2;
		x_min+=x_range/2; x_max-=x_range/2;
		goFlag=true;
		start();
	}
	
	public void hf_fig(){
		double a_range=(a_max-a_min)/2;
		double x_range=(x_max-x_min)/2;
		a_min-=a_range; a_max+=a_range;
		x_min-=x_range; x_max+=x_range;
		if(a_min<2.8)a_min=2.8;
		if(a_max>4)a_max=4.0;
		if(x_min<0)x_min=0.0;
		if(x_max>1)x_max=1.0;
		goFlag=true;
		start();
	}
	
	public void reset_fig(){
		a_min=2.8;a_max=4.0;x_min=0.0;x_max=1.0;
		goFlag=true;
		start();
	}
}

Ryoichi Mizuno - Scientific Computer Graphics
Supplementary Information: Ryoichi Mizuno - Google+ g+External link