// julia.java

/*
Released under the GNU Public License, version 2
http://www.opensource.org/licenses/gpl-license.html
William T. Prewitt
19 December, 2001
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.lang.String;
import java.util.Random;

/*
<applet code="julia" width=800 height=600 >
</applet>
*/

public class julia extends Applet
  implements ActionListener {
  TextField myx;
  TextField myy;
  Button updateButton;

  public void init() {

    Label juliap = new Label("Julia Set Plot ",Label.LEFT);
    add(juliap);

    Label myxp = new Label("Real X constant: ",Label.LEFT);
    myx= new TextField(10);
    myx.setText("-1.15");
    add(myxp);
    add(myx);

    Label myyp = new Label("Imaginary Y constant: ",Label.LEFT);
    myy= new TextField(10);
    myy.setText("0.25");
    add(myyp);
    add(myy);


    updateButton = new Button("Draw");
    add(updateButton);


    // register to receive action events
    updateButton.addActionListener(this);
//    myx.addActionListener(this);   // Enter the x
//    myy.addActionListener(this);   // Enter the y
  }


  // User pressed Button
  public void actionPerformed(ActionEvent ae) {
    repaint();
  }

  public void paint( Graphics g) {

    g.drawString("(-2,1.5i)",10,40);
    g.drawString("(-2,-1.5i)",10,470);
    g.drawString("(2,1.5i)",590,40);
    g.drawString("(2,-1.5i)",590,470);

     julia(g, myx, myy );             // julia set

//    g.drawString(" .... done ",10,500);
  }



public void julia ( Graphics g, TextField myx, TextField myy){
   float f ;
   double x0, y0;
   double x , y , xdelta , ydelta , stepx , stepy ;
   int i , j , k , l , n , nn , maxit , itdiv ;

   Color c[] = { Color.black,
     Color.gray,
     Color.lightGray,
     Color.red,
     Color.green,
     Color.blue,
     Color.cyan,
     Color.magenta,
     Color.yellow,
     Color.pink,
     Color.green,
     Color.blue,
     Color.cyan,
     Color.magenta,
     Color.yellow,
     Color.white};

     try {
     x0 = Double.parseDouble(myx.getText().trim());
     }
     catch (NumberFormatException e) {
     x0=0;
     }
//     g.drawString(" x0 is: " + x0,10,160);


     try {
     y0 = Double.parseDouble(myy.getText().trim());
     }
     catch (NumberFormatException e) {
     y0=0;
     }
//     g.drawString(" y0 is: " + y0,10,200);


   maxit=16;
//   xdelta = 3. ;
//   ydelta = 2.5. ;
   xdelta = 4. ;
   ydelta = 3. ;

//   _clearscreen (_GCLEARSCREEN) ;
   stepx = xdelta / 640 ;
   stepy = ydelta / 480 ;

   itdiv=maxit/16 ;
   if(itdiv < 1) itdiv = 1 ;

//      x = -1.5 ;
      x = -2 ;

      for ( i=0; i < 640; i++ ) {

//	 y = -1.25 ;
	 y = -1.5 ;

	 for ( j = 480; j >= 0; j-- ) {

	    n = juliacount(x,y,x0,y0,maxit) ;
	    k = 16 - (n / itdiv)  ;
            g.setColor(c[k]);
            g.drawLine(i,j,i,j);
	    y = y + stepy ;
	 }
	 x = x + stepx ;
      }
      g.setColor(Color.black);
      g.drawString("(-2,1.5i)",10,40);
      g.drawString("(-2,-1.5i)",10,470);
      g.drawString("(2,1.5i)",590,40);
      g.drawString("(2,-1.5i)",590,470);

      g.drawString("Given complex constant (X,Yi)",40,370);
      g.drawString("cx = (zx * zx) - (zy * zy) + X",40,390);
      g.drawString("cy = (2 * (zx * zy)) + Y",40,410);
      g.drawString("mag = (cx * cx) + (cy * cy)",40,430);
      g.drawString("Plot color corresponding to #iterations until mag > 100",40,450);

}


public int juliacount ( double x , double y , double x0 , double y0 , int maxit )
{
   double  cx , cy , zx , zy , siz  ;
   int m ;

   zx = x ;
   zy = y ;

   for  ( m = 0; m < maxit ; m++ ) {
      cx = (zx * zx) - (zy * zy) + x0  ;

      cy = (2 * (zx * zy)) + y0 ;

      zx = cx ;
      zy = cy ;

      siz = (cx * cx) + (cy * cy) ;

      if ( siz >= 100. ) break ;
   }
   return (m);
}

}






