import java.awt.event.*;

import java.awt.*;

import java.awt.image.*;

public class myMandelCanvas extends Canvas {

 

  private static final int LOWER_X = 0;

  private static final int LOWER_Y = 1;

  private static final int UPPER_X = 2;

  private static final int UPPER_Y = 3;

 

  Rectangle       mandelRect=new Rectangle(20,0,300, 300);

  Image           mandelPicture;

  private float[] mandelLimits =new float[4];

 

  Rectangle       juliaRect=new Rectangle ( 330 ,0, 300,300);

  Image           juliaPicture;

  private float[] juliaLimits = new float[4];

  private float   juliaX,juliaY;

  Dimension       size;

  private int     w2, h2;

 

 public myMandelCanvas(float xx,float yy,int w1,int h1,float[]  mm_l,float[] mj_l){

   juliaX=xx;

   juliaY=yy;

   w2=w1;

   h2=h1;

   size = new Dimension(w2,h2);

   for(int n=0;n<mm_l.length;n++){

     mandelLimits[n]=mm_l[n];

     juliaLimits[n]=mj_l[n];

   }

  }

 

  public void paint(Graphics g) {

    if (mandelPicture != null) {

      g.clearRect(20,0,300, 300);

      g.drawImage(mandelPicture, 20,0 , this);

    }

    if (juliaPicture != null){

      g.clearRect(330,0,300, 300);

      g.drawImage(juliaPicture, 330, 0, this);

 

  }

 }

 

  // calculate new mandelbrot image

  public void createMandel(float[] mml) {

     for(int n=0; n<mandelLimits.length;n++){

       mandelLimits[n]=mml[n];

     }

    mandelPicture = null;

    int w = mandelRect.width;

    int h = mandelRect.height;

    int[] pixels = new int[w * h];

    int index = w * h - 1;

 

    float low_x = mandelLimits[LOWER_X], high_x =mandelLimits[UPPER_X];

    float low_y = mandelLimits[LOWER_Y], high_y =mandelLimits[UPPER_Y];

    float dy = (high_y - low_y) / (float) h;

    float dx = (high_x - low_x) / (float) w;   

    // loop up through y and x values

    float real_y = low_y;

    for (int y = 0; y < h; y++, real_y += dy) {

      float real_x = low_x;

      for (int x = 0; x < w; x++, real_x += dx) {

 

        // start with zn = (0, 0) and no colour set

        int R = 0, G = 0;

        float a = 0, b = 0;

        // check for divergent or simple convergent series

        for (int s = 0; s < 127; s++) {

          float oa = a, ob = b;

          a = oa * oa - ob * ob + real_x;

          b = 2 * oa * ob + real_y;

          if (a * a + b * b >= 4) {

            G = 2 * s;

            break;

          }

          if (a == oa & b == ob) {

            R = 2 * s;

            break;

          }

        }

       

        pixels[index--] = (255 << 24) | (R << 21) | (G << 9) | (0 << 0);

      }

    }

 

 

 

// make image and redisplay

    mandelPicture = createImage(new MemoryImageSource(w, h,ColorModel.getRGBdefault(), pixels, 0, w));

    repaint();

  }

 

  // calculate new julia set image

  public void createJulia(float cr, float ci,float[] jjl) {

     for(int n=0; n<juliaLimits.length;n++){

      juliaLimits[n]=jjl[n];

    }

 

    juliaPicture = null;

    int w = juliaRect.width;

    int h = juliaRect.height;

    int[] pixels = new int[w * h];

    int index = w * h - 1;

   

    // calculate boundary and step values

    float low_x = juliaLimits[LOWER_X], high_x =juliaLimits[UPPER_X];

    float low_y = juliaLimits[LOWER_Y], high_y =juliaLimits[UPPER_Y];

    float dy = (high_y - low_y) / (float) h;

    float dx = (high_x - low_x) / (float) w;

   

    // loop up through y and x values

    float real_y = low_y;

    for (int y = 0; y < h; y++, real_y += dy) {

      float real_x = low_x;

      for (int x = 0; x < w; x++, real_x += dx) {

       

        // start with zn = (real_x, real_y) and no colour set

        int R = 0, G = 0;

        float a = real_x, b = real_y;

        // check for divergent or simple convergent series

        for (int s = 0; s < 127; s++) {

          float oa = a, ob = b;

          a = oa * oa - ob * ob +cr;

          b = 2 * oa * ob + ci;

          if (a * a + b * b >= 4) {

            G = 2 * s;

            break;

          }

          if (a == oa & b == ob) {

            R = 2 * s;

            break;

          }

        }

 

pixels[index--] = (255 << 24) | (R << 16) | (G << 9) | (0 << 0);

      }

    }

      

    // make image and redisplay

    juliaPicture = createImage(new MemoryImageSource(w, h,ColorModel.getRGBdefault(), pixels, 0, w));

    repaint();

  }

}