import java.applet.*;
import java.awt.*;
public class Julia extends Applet {
   private int last_x=0;
   private int last_y=0;
   private Color current_color=Color.black;
   private Button clear_button;
   private Button draw_button;
private int xSize=350;
private int ySize=480;
private int limit=50;
private int steps;
private int curColor;
private double xStep;
private double yStep;
private double xPos;
private double yPos;
private double xC=0.4;
private double yC=0.325;
private double xOrg=-1.25;
private double yOrg=-1.25;
private double xMax=1.25;
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.white);

//create a button
    clear_button=new Button("Clear");
    clear_button.setForeground(Color.black);
    clear_button.setBackground(Color.lightGray);
    this.add(clear_button);

//create a button
    draw_button=new Button("Draw the Julia Set");
    draw_button.setForeground(Color.black);
    draw_button.setBackground(Color.lightGray);
    this.add(draw_button);
   }
//end init applet




//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){

          if(event.target==clear_button){
               Graphics g=this.getGraphics();
               Rectangle r=this.bounds();
               g.setColor(this.getBackground());
               g.fillRect(r.x,r.y,r.width,r.height);
               return true;
              }
              //end clear

          // in case of click
         else if(event.target==draw_button){
               Graphics g=this.getGraphics();
               g.setColor(Color.black);

 
// begin

               xStep=(xMax - xOrg)/xSize;
               yStep=(yMax - yOrg)/ySize;

      for(int i=1; i<xSize;i++){
       for(int j=1; j<ySize;j++){
        xPos = xOrg + i * xStep;
        yPos = yOrg + j * yStep;
        xIter = xPos;
        yIter = yPos;
        steps = 0;
        xTemp=  xPos;
        yTemp=  yPos;
        notDone=true;

         while(notDone) {
               xTemp= xIter*xIter - yIter*yIter + xC;
               yTemp= 2 * xIter * yIter + yC;
               xIter= xTemp;
               yIter= yTemp;

               steps++; 
            
if((xIter)*(xIter) + (yIter)*(yIter) > 4.0){notDone=false;}

if( steps == limit){notDone=false;}
                 }
                 //end for while(notdone)
         
                 if(steps==limit){
                           g.setColor(Color.black);
                           g.drawLine(i,j,i,j);
                    }
                    //end steps eq. limit
                   
                 if(j==ySize-1){
                   g.setColor(Color.red);
                   g.drawLine(i,j,i,j);
                   }
                   // end last j



           }
           //end for j 
          }
         //end for i
              
         return true;
        }
        // end draw button

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

