import java.applet.*;
import java.awt.*;
public class Mandelb extends Applet {
   private int last_x=0;
   private int last_y=0;
   private Color current_color=Color.red;
   private Button draw_button;
private int xSize=640;
private int ySize=480;
private int limit=30;
private int steps;
private int curColor;
private double xStep;
private double yStep;
private double xPos;
private double yPos;
private double xOrg=-2.0;
private double yOrg=-1.25;
private double xMax=0.5;
private double yMax=1.25;
private double xIter;
private double yIter;
private double xTemp;
private double yTemp;
private boolean notDone=true;
  

//init applet
   public void init(){

//set backgd color
    this.setBackground(Color.black);

//create a button
    draw_button=new Button("Draw Mandelbrot Set");
    draw_button.setForeground(Color.black);
    draw_button.setBackground(Color.lightGray);
    this.add(draw_button);


   }
//end init applet

public double distance( double xVal,double yVal){ 
       return java.lang.Math.sqrt((xVal)*(xVal) + (yVal)*(yVal)); }
// end distance

public void Calculate(double xPos, double yPos, double xIter, double yIter){

 xTemp= xIter*xIter - yIter*yIter + xPos;
 yTemp= 2 * xIter * yIter + yPos;
 xIter= xTemp;
 yIter= yTemp;
 }
// end Calculate



//called when mouse click
    public boolean mouseDown(Event e, int x, int y){
          last_x=x; last_y=y;
           return true;
       }


//called when mouse moves
    public boolean mouseDrag(Event e, int x, int y){
          Graphics g=this.getGraphics();
          g.setColor(current_color);
          g.drawLine(last_x,last_y,x,y);
          last_x=x;
          last_y=y;
          return true;
      }


//user clicks button or chooses a color
     public boolean action(Event event, Object arg){
          // in case of click
          if(event.target==draw_button){
               Graphics g=this.getGraphics();
               xStep=(xMax - xOrg)/xSize;
               yStep=(yMax - yOrg)/ySize;

               for(int i=1; i<640;i++){
                for(int j=1; j<480;j++){
         xPos = xOrg + i * xStep;
        yPos = yOrg + j * yStep;
        xIter = 0.0;
        yIter = 0.0;
        steps = 0;
        curColor=0;
        notDone=true;
         while(notDone) {
               xTemp= xIter*xIter - yIter*yIter + xPos;
               yTemp= 2 * xIter * yIter + yPos;
               xIter= xTemp;
               yIter= yTemp;

            steps++; curColor++;
             if(curColor > 9){ curColor=0;} 
            if(java.lang.Math.sqrt((xIter)*(xIter) + (yIter)*(yIter)) >= 2.0){notDone=false;}
            if( steps == limit) {notDone=false;}
                 }
          //end for while(notdone)
         
                 if(steps < limit){
                                 if(curColor==0){current_color=Color.blue;}
                                 if(curColor==1){current_color=Color.green;}
                                 if(curColor==2){current_color=Color.yellow;}
                                 if(curColor==3){current_color=Color.red;}
                                 if(curColor==4){current_color=Color.magenta;}
                                 if(curColor==5){current_color=Color.blue;}
                                 if(curColor==6){current_color=Color.green;}
                                 if(curColor==7){current_color=Color.red;}
                                 if(curColor==8){current_color=Color.yellow;}
                                 if(curColor==9){current_color=Color.magenta;}

                                  g.setColor(current_color);
                                  g.drawLine(i,j,i+2,j);
                                   }
                                 //end steps less than limit
                   
                 }
                //end for j
               }
               //end for i
               return true;
              }

           // Otherwise let the superclass handle it.
           else return super.action(event, arg);
     }
}

