/*********************************************************************************************/
/*                                                                                           */
/*  Java Image Demo - Mandelbrot Pattern                                                     */
/*  Auth: bkenwright@xbdev.net                                                               */
/*  URL: www.xbdev.net                                                                       */
/*                                                                                           */
/*  Demo by:  Ben Kenwright (bkenwright@xbdev.net                                            */
/*                                                                                           */
/*********************************************************************************************/

//HOW TO COMPILE FOR JAVA 1.1.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//
//  C:>javac -target 1.1 mandelbrot.java
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//



import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import java.applet.Applet;

public class mandelbrot extends java.applet.Applet 
{
   Image img;
   Graphics backg;

   public void paint(Graphics g) 
   {
      g.drawImage( img , 0, 0, this );
   }// End of paint(..)
   
   public void init()
   {
	int width = getSize().width;
	int height = getSize().height;
	img = createImage(width, height );
	backg = img.getGraphics();
	    
        // backg.setColor(Color.black);	
        // backg.drawLine(px-3,py-3,px+10,py+10);
     
        DrawMandelbrot(backg);

     backg.setColor(Color.black);
     backg.drawString("Wonderful Mandelbrot Image (www.xbdev.net)", 30, 30);
  }// End of init(..)
  

  public void DrawMandelbrot(Graphics g)
  {

    Dimension d=getSize();
    int x;
    int y;
    int it;
    double r, i;
    double cr, ci;
    double tr, ti;

    int color_r;
    int color_g;
    int color_b;

    // Make a loop on all the pixels in the panel
    for(x=0; x<d.width; ++x){
      for(y=0; y<d.height; ++y){

        // Pixel scaling: map a 4x4 area with (0,0) at the center
        cr=((double) x-d.width/2)/d.width*4;
        ci=-((double) y-d.height/2)/d.height*4;

        // For the Mandelbrot set, we start fixing C=
        r=0;
        i=0;
        for(it=0; it<maxIter; ++it){
          // Z=Z*Z+C
          tr=r*r-i*i+cr;
          ti=2*r*i+ci;
          r=tr;
          i=ti;

          // If the distance of the point is greater than 2, exit
          if(r*r+i*i>4) break;
        }

        // We artificially boost colors
        it=(int)(maxIter*Math.sqrt((double)it/maxIter));

        color_r=255-it*255/maxIter;
        color_g=255-it*255/2/maxIter;
        color_b=255-it*255/maxIter;

        // Plot the point
        g.setColor(new Color(color_r,color_g,color_b));

        g.drawLine(x,y,x,y);
      }
    }


  }
  static int maxIter=64;


}// End of applet

