import java.applet.*;
import java.awt.*;
public class JFracgen
 {
  public int x,y,xof,yof,w=400,h=400, n, iter=75, answer=0;
  Color colors[] = new Color[76];
  Thread runThread = null;
  Graphics2D offsg;
  boolean please_stop = false;
  static M parent;
  LC lc;
  Te te;
  double xi=0.001000, yi=0.0010000, c, d, l, a=0.40, b=0.320, hh, xj, yj, xk=0.2, yk=0.4, xf, yf;
  public JFracgen(LC lc) 
   {
    this.lc=lc;
   }
  public void prep()
   {
    te=parent.te;
    te.teS("aSl xinc xinc.type r xinc.v .001  xinc.ul 1");
    te.teS("aSl yinc yinc.type r yinc.v .001 yinc.ul 1");
    te.teS("aSl aval aval.type r aval.v .4 aval.ul 1");
    te.teS("aSl bval bval.type r bval.v .32 bval.ul 1");
    te.teS("aSl xinit xinit.type r xinit.v .2 xinit.ul 1");
    te.teS("aSl yinit yinit.type r yinit.v .4 yinit.ul 1");
    te.teS("aSl w w.v 400 w.ul 2000");
    te.teS("aSl h h.v 400 h.ul 2000");
    int rval, gval, bval;
    for (int i = 0; i < colors.length; ++i) 
     {
      rval=(int)Math.floor(Math.random()*256);
      gval=(int)Math.floor(Math.random()*256);
      bval=(int)Math.floor(Math.random()*256);
      colors[i]=new Color(rval,gval,bval);
     }
   }
  double dhg(String s)
   {
    return te.dlocordhg(s);
   }
  public void FractalMaker() 
   {
    xi=dhg("xinc");
    yi=dhg("yinc");
    a=dhg("aval");
    b=dhg("bval");
    xk=dhg("xinit");
    yk=dhg("yinit");
    w=(int)dhg("w");
    xof=(int)(w/2);
    h=(int)dhg("h");
    yof=(int)(h/2);
    offsg=lc.offsg;
    offsg.setColor(Color.red);
    n=0;
    xf=xk;
    yf=yk;
    for(x=0;x<w;x++)
     {
      for(y=0;y<h;y++)
       {
        xj=xk;
        yj=yk;
        while(n<iter && answer == 0)
         {
          c = (xj*xj)-(yj*yj)+a;     /* a distance of 2 from the ori.  */
          d = (2*xj*yj) - b;         /* should be + b, but - b gives interesting results*/
          hh = (c*c)+(d*d);
          l = Math.sqrt(hh);
          if( l > 2 ) answer = 1;
          else ++n;
          xj = c;       /* jump to the new location for reiteration  */
          yj = d;
         }
        if(  n >= 0 && n <= 75 ) offsg.setColor(colors[n]);
        offsg.drawLine(x-xof,y-yof,x-xof,y-yof);
        n=0;
        answer=0;
        yk=yk+yi;
       }
      lc.myRepaint();
      y=0;
      xk=xk+xi;
      yk=yf;
     }
    lc.myRepaint();
    //g.drawImage(fractal, 0,0, this);
   }
 }
