import java.awt.*;
import java.awt.image.*;
import java.applet.Applet;
import Complex;
public class sala8 extends Applet implements Runnable {
  Thread kicker = null;
    int maxcol = 160;
    int ns = 8;
    int ncolr = 30;
    byte redm[] = new byte[maxcol];
    byte greenm[] = new byte[maxcol];
    byte bluem[] = new byte[maxcol];
    byte b255 = (byte)255;
    Image img;
    Image s;
    IndexColorModel icm;
    int hh=50;
    int height=100;
    int width=50;
    double Left1,Right1,Top1,Bottom1;
      byte data[] ;
    double c1 = .5;
    double c2 = -.5;
    Panel control;
    Label c1val,c2val;
    Scrollbar slider1,slider2;
    Font font;
    TextField f1,f2;
    Label workdone,xmouse,ymouse;
    int heights = 200;
    int widths =  200;
    int icy[][];
    String message = null;
  public byte interp(int a,int b,double f) {
                return (byte)(a*(1-f)+b*f);
        }


 public void start() {
                if (kicker == null) {
                   kicker = new Thread(this);
                   kicker.start();       
        }
        }
 public void stop() {
                if (kicker!=null) {
                        kicker.stop();
                        kicker = null;
                }
        }
 public void init() {
       int i;
      
       s = getImage(getCodeBase(),"porta8.gif");
       Left1 = -1.15;
       Right1 = 1.;
       Top1 = 1.15;
       Bottom1 = -1.15;
   if(getParameter("NXPIX") != null)
         {
           width =  new Integer(new String(getParameter("NXPIX"))).intValue(); 
           }
   if(getParameter("NYPIX") != null)
         {
           hh =  new Integer(new String(getParameter("NYPIX"))).intValue(); 
           }
   if(getParameter("HEIGHT") != null)
         {
           height =  new Integer(new String(getParameter("HEIGHT"))).intValue(); 
           }
       data = new byte[hh*width];
   redm[0] = 0; greenm[0] = 0; bluem[0] = 0;
            for (i=0;i<5;i++) {
                    redm[i+1] = interp(255,0,i/4.);
                    greenm[i+1] = interp(0,0,i/4.);
                    bluem[i+1] = interp(255,0,i/4.);
            }
            for (i=0;i<4;i++) {
                    redm[i+6] = interp(0,255,i/3.);
                    greenm[i+6] = interp(0,255,i/3.);
                    bluem[i+6] = interp(255,0,i/3.);
            }
 


              icm = new IndexColorModel(8,maxcol,redm,greenm,bluem);
              img = createImage(new  MemoryImageSource(width,hh,icm,data,0,width));
             font = new Font("Helvetica", Font.BOLD, 12);
             setLayout(new BorderLayout());
             control = new Panel();
             control.setLayout(new GridLayout((int)(height/40),1));
             add("East", control);
             c1val = new Label("c1 (-.6,2.) .5");
             control.add(c1val);
             slider1 = new Scrollbar(Scrollbar.HORIZONTAL, 50, 1, 1, 10000);
             control.add(slider1);
             c2val = new Label("c2 (-1.,1.) .5");
             control.add(c2val);
             slider2 = new Scrollbar(Scrollbar.HORIZONTAL, 50, 1, 1, 10000);
             control.add(slider2);
              control.add(new Button("Random"));
             f1 = new TextField(String.valueOf(c1),10);
             control.add(f1);
             f2 = new TextField(String.valueOf(c2),10);
             control.add(f2);
             workdone = new Label("work done");
             control.add(workdone);
             xmouse = new Label("mouse off");
             control.add(xmouse);
             ymouse = new Label("mouse off");
             control.add(ymouse);
             
        }
 public  boolean mouseDown(Event evt, int x, int y) {
          int ns1,ns2;
          ns1 = 0;
          ns2 = 0;
                        if ( (x < widths) && (y <heights )){
                        xmouse.setText(String.valueOf(x));
                        ymouse.setText(String.valueOf(y));
                        if(x < 100 && y < 100) ns=8;
                        if(x > 100 && y < 100) ns=9;
                        if(x < 100 && y > 100) ns=10;
                        if(x > 100 && y > 100) ns=11;
       if (ns == 8) {
       ns1=x;
       ns2=y;
       Left1 = -1.15;
       Right1 = 1.;
       Top1 = 1.15;
       Bottom1 = -1.15;
       }
       if (ns == 9) {
       ns1=x-100;
       ns2=y;
       Left1 = -1.;
       Right1 = 1.;
       Top1 = 1.;
       Bottom1 = -1.;
       }
       if (ns == 10) {
       ns1=x;
       ns2=y-100;
       Left1 = -1.1;
       Right1 = 1.;
       Top1 = 1.1;
       Bottom1 = -1.1;
       }
       if (ns == 11) { 
      ns1=x-100;
       ns2=y-100;
       Left1 = -1.;
       Right1 = 1.;
       Top1 = 1.1;
       Bottom1 = -1.1;
       }
                        c1 = (double)ns1/widths*(Right1-Left1)+Left1;
                        c2 = (double)(101-ns2)/heights*(Top1-Bottom1)+Bottom1;
                        f1.setText(String.valueOf(c1));
                        f2.setText(String.valueOf(c2));
                        c1val.setText(String.valueOf(c1));
                        c2val.setText(String.valueOf(c2));
                        if (kicker == null) {
                           start();
                         }
       return true;
    }
 return true;
 }
 public boolean handleEvent(Event e)
        {
                if (e.target instanceof Scrollbar) {
                        int value = ((Scrollbar)e.target).getValue();
                        if (e.target == slider1) {
                                c1 = value;
                                c1 = c1/10000*(Right1-Left1)+Left1;
                        c1val.setText(String.valueOf(c1));
                        f1.setText(String.valueOf(c1));
                        if (kicker == null) {
                           start();
                         }
                        }
                        if (e.target == slider2) {
                                c2 = value;
                                c2 = c2/10000*(Top1-Bottom1)+Bottom1;
                        c2val.setText(String.valueOf(c2));
                        f2.setText(String.valueOf(c2));
                        if (kicker == null) {
                           start();
                         }

                         }
               return true;
        }
 return super.handleEvent(e);
  }

public boolean action(Event e, Object arg)
        {
                if("Random".equals(arg)){
                        c1 = Math.random()*(Right1-Left1)+Left1;
                        c2 = Math.random()*(Top1-Bottom1)+Bottom1;
                        f1.setText(String.valueOf(c1));
                        f2.setText(String.valueOf(c2));
                        c1val.setText(String.valueOf(c1));
                        c2val.setText(String.valueOf(c2));
                        if (kicker == null) {
                           start();
                         }
                        return true;
                }
                if(e.target instanceof TextField) {
                  if(e.target == f1) c1 = Double.valueOf(f1.getText()).doubleValue();
                  if(e.target == f2) c2 = Double.valueOf(f2.getText()).doubleValue();
                  if (kicker == null) {
                           start();
                         }
                  return true;
                  }
                return true;
        }

 public 
void paint(Graphics g) {
                            g.drawImage(img,0,heights+20,width*3,width*3,this);
                            g.drawImage(s,0,0,this);
  }
public  
 void update(Graphics g) {
  paint (g);
 }
public void run() {
            int Npix = width;
            double Left = -1.2;
            double Right = 1.2;
            double Bottom =-1.2;
            double Top = 1.2;
            double dx = (Right -Left)/Npix;
            double dy = (Top - Bottom)/Npix;
            double x,y,x1,y1,r;
            int i,j,ncol;
            Complex xc,cx,xc0;
            workdone.setText("First phase");
            message ="First phase";
            showStatus(message);
                repaint();
       try { Thread.sleep(1000);}
       catch (InterruptedException e) { }
            cx = new Complex(c1,c2);
            for ( i=0;i<Npix;i++){
              x = Left + i*dx;
              for ( j=0;j<Npix;j++){
                 y = Bottom + j*dy;
                 xc0 = new Complex(x,y);
                      xc =new Complex(xc0);
                 int l;
                 for (l=0;l<50;l++) {
                      for (int k=0;k<(ns-1);k++){
                        xc =  xc.multiply(xc0,xc);
                        }
                       xc = xc.add(xc,cx);
                       x1 = xc.real(xc);
                       y1 = xc.imag(xc);
                       r = x1*x1 + y1*y1;
                                  if (r>4) break;
                               }
                 ncol = l;
                 if(l==50) ncol = 0; 
                 data[i+j*width] = (byte)((ncol%9)+1);
                 }
            message =String.valueOf(i*2)+"%";
            showStatus(message);
                repaint();
            workdone.setText(message);
               }
           img = null;
           img = createImage( new MemoryImageSource(width,hh,icm,data,0,width));
                        repaint();
            message ="Done!";
            showStatus(message);
                repaint();
            workdone.setText("Done!");
            kicker = null;
        }
}

