// mandelbr.java

/*
Released under the GNU Public License, version 2
http://www.opensource.org/licenses/gpl-license.html
William T. Prewitt
21 December, 2001
*/
/*
    printf ("\nSuggested starting settings:");
    printf ("\n    X,         Y,    width & height,  iterations,  comment");
    printf ("\n  -2.        -1.5        3.0           64      All of Mset");
    printf ("\n   -.76        .01        .02         256");
    printf ("\n  -1.26        .01        .02         256");
    printf ("\n    .26       0.0         .01         256");
    printf ("\nPretty spots: (takes a while)");
    printf ("\n   -.745468   -.113039    .00008     1024 (double spiral)");
    printf ("\n   -.74543340 -.11300743  .00000005  1024 (whirlpool)");
    printf ("\n   -.7454353  -.1130137   .000015    1024 (beetle in double spiral)");
    printf ("\n   -.15125163 1.02824338  .00004     1024 (maltese beetle)");
*/
/*
<applet code="mandelbr" width=800 height=600 >
</applet>
*/


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.lang.String;
import java.util.Random;

public class mandelbr extends Applet
  implements ActionListener {
  TextField myx;
  TextField myy;
  TextField myxdelta;
  TextField myydelta;
  TextField mymaxit;
  Button updateButton;

  public void init() {

    Label juliap = new Label("Mandelbrot Set Plot ",Label.LEFT);
    add(juliap);

    Label myxp = new Label("LLeft REAL (X) coord (-2 to +1): ",Label.LEFT);
    myx= new TextField(10);
    myx.setText("-2");
    add(myxp);
    add(myx);

    Label myyp = new Label("LLeft IMAGINARY (Y) coord (-1.5 to +1.5): ",Label.LEFT);
    myy= new TextField(10);
    myy.setText("-1.5");
    add(myyp);
    add(myy);

    Label myxdeltap = new Label("screen width (0 to 3): ",Label.LEFT);
    myxdelta= new TextField(3);
    myxdelta.setText("3");
    add(myxdeltap);
    add(myxdelta);
//   xdelta ;

    Label myydeltap = new Label("screen height (0 to 3): ",Label.LEFT);
    myydelta= new TextField(3);
    myydelta.setText("3");
    add(myydeltap);
    add(myydelta);
//   ydelta;

    Label mymaxitp = new Label("max iterations (16 to 16384 by powers of 2): ",Label.LEFT);
    mymaxit= new TextField(8);
    mymaxit.setText("256");
    add(mymaxitp);
    add(mymaxit);
   // maxit


    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) {


     mandelbr(g, myx, myy, myxdelta, myxdelta, mymaxit );             // mandelbrot set

  }





public void mandelbr ( Graphics g, TextField myx, TextField myy, TextField myxdelta, TextField myydelta, TextField mymaxit){

   float f ;
   double x , y , x0 , y0 , xdelta , ydelta , stepx , stepy ;
   int i , j , k , l , n , nn , maxit , itdiv , itbyte, isay, modcolor ;
   int numxpixels=800;
   int numypixels=600;

     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};


// ("lower left REAL (X) coordinate (-2 to +1): ")
     try {
     x0 = Double.parseDouble(myx.getText().trim());
     }
     catch (NumberFormatException e) {
     x0=-2;
     }

// ("lower left IMAGINARY (Y) coordinate (-1.5 to +1.5): ")
     try {
     y0 = Double.parseDouble(myy.getText().trim());
     }
     catch (NumberFormatException e) {
     y0=-1.5;
     }

// ("screen width (0 to 3): ")
     try {
     xdelta = Double.parseDouble(myxdelta.getText().trim());
     }
     catch (NumberFormatException e) {
     xdelta=3;
     }

// ("screen height (0 to 3): ") ;
     try {
     ydelta = Double.parseDouble(myydelta.getText().trim());
     }
     catch (NumberFormatException e) {
     ydelta=3;
     }

// ("maximum iterations (16 to 16384 by powers of 2): ") ;
     try {
     maxit = (int)Double.parseDouble(mymaxit.getText().trim());
     }
     catch (NumberFormatException e) {
     maxit=256;
     }
   if(maxit < 16)maxit=16 ;
   if(maxit > 16384)maxit=16384 ;

//   printf ("Modulo divisor for color display (2-16) or 0 for levels ") ;
   modcolor=16;
   if(modcolor > 16)modcolor=16 ;

//   _clearscreen (_GCLEARSCREEN) ;
   stepx = xdelta / numxpixels ;
   stepy = ydelta / numypixels ;

   itbyte=1 ;
   if(maxit > 256) itbyte = maxit / 256 ;
   itdiv=maxit/16 ;
   if(itdiv < 1) itdiv = 1 ;

   k=0 ;

   g.setColor(c[k]);

   x=x0 ;

   for ( i=0; i <= numxpixels; i++ ) {

      y=y0 ;

      for ( j= numypixels; j >= 0; j-- ) {

	 n = mandelcount(x,y,maxit) ;
	 nn=n/itbyte - 1 ;
	 nn=n/itdiv ;
	 if (nn > 15 ) nn=15 ;
	 k = 16 - nn ;
	 if (modcolor > 1) k = n % modcolor ;
         g.setColor(c[k]);
         g.drawLine(i,j,i,j);
	 y = y + stepy ;
      }
      x = x + stepx ;
   }

}

public int mandelcount ( double x , double y , int maxit )
{
   double  cx , cy , zx , zy , siz  ;
   int m, isiz;

   zx = 0. ;
   zy = 0. ;
   for  ( m = 0; m < maxit ; m++ ) {
      cx = (zx * zx) - (zy * zy) + x ;
      cy = (2 * (zx * zy)) + y ;

      zx = cx ;
      zy = cy ;

      siz = (cx * cx) + (cy * cy) ;

      if ( siz >= 4. ) break ;
   }
   return (m);
}

}



