Sample Of Java Source


/**

        Program:        Julia5

        Purpose:        Reccurence figure

        @author:        Sanae.Masasi

        @version:       1.00; 14.June.96

*/





import java.awt.*;

import java.applet.*;



public class Julia5 extends Applet{



    CtrlJ5 controls;



    public void init(){

	setLayout(new BorderLayout());

	CvsJ5 c = new CvsJ5();

	add("Center",c);

	add("South",controls = new CtrlJ5(c));

    }



    public void start(){

	controls.enable();

    }



    public void stop(){

	controls.disable();

    }



    public boolean handleEvent(Event e){

	if(e.id == Event.WINDOW_DESTROY){

	    System.exit(0);

	}

	return false;

    }



}



class CvsJ5 extends Canvas{

	

    public static final int MAXPRD = 8;

    double Box = 0.01d;

    int calcNum = 30;

    int step = 3;

    int colStep = 255 / MAXPRD;

    double CX = 0d,CY = 0d;

    double W = 1.2d;

    double lx1 = CX - W,lx2 = CX + W;

    double ly1 = CY - W,ly2 = CY + W;



    Rectangle r;

    double scale;



    double x[] = new double [calcNum + 1];

    double y[] = new double [calcNum + 1];



    Font font = new Font("TimesRoman",Font.PLAIN,10);



    public void paint(Graphics g) {



	int rectW = 50;

    	r =bounds();

    	scale = 2 * W / r.height;



	g.setColor(Color.white);

	g.fillRect(0,0,r.height,r.height);

	g.setColor(Color.lightGray);

	g.fillRect(r.height,0,r.width - r.height,r.height);

	g.setColor(Color.black);

	g.setFont(font);

	g.drawString("x=" + (int)(lx1 * 100)/100d +

		     " ~ " + (int)(lx2 * 100)/100d,10,10);

	g.drawString("y=" + (int)(ly1 * 100)/100d +

		     " ~ " + (int)(ly2 * 100)/100d,10,20);

	int row,col;

	String str;



	for(int i = 0 ;i <= MAXPRD + 1;i ++){

	    

	    row = i % 5;

	    if(i < 5){col = 0;}else{col = 1;}

	    

	    if(i == 0){

		g.setColor(new Color(255 - i * colStep,i * colStep,0));

	 	str = "converge";

	    }else if(i == MAXPRD ){

		g.setColor(Color.black);

		str = "prd>" + MAXPRD + ",chaos";

	    }else if(i == MAXPRD + 1){

		g.setColor(Color.white);

		str = "diverge";

	    }else{

		g.setColor(new Color(255 - i * colStep,i * colStep,0));

		str = "piriod=" + (i + 1);

	    }

	    

            g.fillRect(r.height + col * rectW + 50,

		row * rectW + 20,35,35);

	    g.setColor(Color.black);

	    g.setFont(font);

	    g.drawString(str,r.height + col * rectW + 50,

		row * rectW + 15);

	}

    }



    public void update(Graphics g){



	g.setColor(Color.white);

	g.fillRect(0,0,r.height,r.height);

	g.setColor(Color.black);

	g.setFont(font);

	g.drawString("x=" + (int)(lx1 * 100)/100d +

		     " ~ " + (int)(lx2 * 100)/100d,10,10);

	g.drawString("y=" + (int)(ly1 * 100)/100d +

		     " ~ " + (int)(ly2 * 100)/100d,10,20);

	boolean drawflag = false;



	for(int xx = 0 ;xx <= r.height;xx += step){

	    x[0] = lx1 + xx * scale;

	    for(int yy = 0; yy <= r.height ;yy += step){

	        y[0] = ly2 - yy * scale;

	        for (int k = 1;k<= calcNum;k++){

	    	    x[k] = x[k-1] * x[k-1] * x[k-1]

			-3 * x[k-1] * y[k-1] * y[k-1] +x[0];

	    	    y[k] = 3 * x[k-1] * x[k-1] * y[k-1] 

			-y[k-1] * y[k-1] * y[k-1] + y[0];

	           if(x[k] * x[k] + y[k] * y[k] >= 4.0){

	     	        drawflag = false;

		        break;

		    }

		    drawflag =true;

		}

	    	if(drawflag){

		    for(int k = 1;k <= MAXPRD;k++){

		        if(Math.abs(x[calcNum] - x[calcNum - k]) < Box

		           &&

		           Math.abs(y[calcNum] - y[calcNum - k]) < Box){

		            g.setColor(new Color(255 - (k -1) * colStep,

		                (k - 1) * colStep,0));

			    g.drawLine(xx,yy,xx,yy);

			    break;

		    	}else{

		 	    g.setColor(Color.black);

			    g.drawLine(xx,yy,xx,yy);

		    	}

		    }

		}

	    }

	}

    }



    public void redraw(double CX,double CY,double W,int step){



	this.lx1 = CX - W; this.lx2 = CX + W;

	this.ly1 = CY - W; this.ly2 = CY + W;

	this.scale = 2 * W / r.height;

	this.step = step;

	repaint();



    }



}



class CtrlJ5 extends Panel{



    CvsJ5 canvas;



    Choice choiceCX,choiceCY,choiceW,choiceStep;

    Label labelC,labelW,labelStep;

     

    Font font = new Font("TimesRoman",Font.ITALIC,12);



    public CtrlJ5(CvsJ5 canvas){



	this.canvas = canvas;

	setLayout(new FlowLayout());



	labelC = new Label("center ",Label.LEFT);

	labelC.setFont(font);

	add(labelC);

	choiceCX = new Choice();

	for(int a = -200;a <= 200;a ++){

	    choiceCX.addItem(""+a/100d);

	}

	choiceCX.select(String.valueOf(canvas.CX));

	add(choiceCX);

	choiceCY = new Choice();

	for(int a = -200;a <= 200;a ++){

	    choiceCY.addItem(""+a/100d);

	}

	choiceCY.select(String.valueOf(canvas.CY));

	add(choiceCY);



	labelW = new Label("width ",Label.LEFT);

	labelW.setFont(font);

	add(labelW);

	choiceW = new Choice();

	for(int a = 1;a <= 200;a ++){

	    choiceW.addItem(""+a/100d);

	}

	choiceW.select(String.valueOf(canvas.W));

	add(choiceW);



	labelStep = new Label("step ",Label.LEFT);

	labelStep.setFont(font);

	add(labelStep);

	choiceStep = new Choice();

	for(int a = 1;a <= 4;a ++){

	    choiceStep.addItem(""+a);

	}

	choiceStep.select(String.valueOf(canvas.step));

	add(choiceStep);



	add(new Button("DRAW"));



    }



    public boolean action(Event ev, Object arg) {

	if (ev.target instanceof Button) {



	    String tempCX,tempCY,tempW,tempStep;

	    Double getCX,getCY,getW;



	    tempCX = choiceCX.getSelectedItem();

	    getCX = Double.valueOf(tempCX);

	    tempCY = choiceCY.getSelectedItem();

	    getCY = Double.valueOf(tempCY);



	    tempW = choiceW.getSelectedItem();

	    getW = Double.valueOf(tempW);



	    tempStep = choiceStep.getSelectedItem();



	    canvas.redraw(getCX.doubleValue(),getCY.doubleValue(),

			getW.doubleValue(),Integer.parseInt(tempStep));

	

	}

	return false;

    }



}