import java.applet.*;import java.awt.*;import java.awt.event.*;import java.awt.image.*;public class Fragment extends Applet implements ActionListener, MouseListener, Runnable {  volatile boolean stop = false;  int itterations = 500;  Thread theThread;  Image im;  int width = 400, height = 400;  double xmin=-2, xmax=2, ymin=-2, ymax=2;  int lastMinI=0;  int[] data, colorData;  MemoryImageSource imSource;  public void init() {    if (getParameter("itterations")!=null) {      try {        itterations = Integer.parseInt(getParameter("itterations"));      }      catch (NumberFormatException e) {        System.out.println(e);      }    }    setLayout(new BorderLayout());    Button b = new Button("Start");    b.addActionListener(this);    addMouseListener(this);    add(b, "South");  }  public void start() {    //updateData();  }  public void stop() {    if (theThread != null) theThread.stop();  }  public void destroy() {    if (theThread != null) theThread.stop();  }  public void actionPerformed(ActionEvent e) {    updateData();  }  public void mouseClicked(MouseEvent e) {    //zoom(xmin+(xmax-xmin)*e.getX()/width, ymin+(ymax-ymin)*e.getY()/height, .25);    //updateData();    stop = true;  }  public void mousePressed(MouseEvent e) { }  public void mouseReleased(MouseEvent e) { }  public void mouseEntered(MouseEvent e) { }  public void mouseExited(MouseEvent e) { }    public void updateData() {    if (theThread != null) {      try {        stop=true;        Thread.yield();        theThread.stop();      }      catch (Exception e) { System.out.println(e); }    }    try {      colorData = new int[width*height];      imSource = new MemoryImageSource(width, height, colorData, 0, width);      imSource.setAnimated(true);      im = createImage(imSource);      repaint();      theThread = new Thread(this);      theThread.setDaemon(true);      theThread.setPriority(Thread.currentThread().getPriority()-1);      theThread.start();    } catch (Exception ex) { ex.printStackTrace(); }  }    public void update(Graphics g) {    paint(g);  }  public void paint(Graphics g) {    g.drawImage(im, 0, 0, this);    //System.out.println("painted "+im);  }    public void zoom(double zfact) {    zoom((xmax+xmin)/2, (ymax+ymin)/2, zfact);  }  public void zoom(double x, double y, double zfact) {    double dx = xmax-xmin, dy = ymax-ymin;    dx *= zfact; dy *= zfact;    xmin = x - dx/2; xmax = x + dx/2;    ymin = y - dy/2; ymax = y + dy/2;  }    public void toRedOrange(int data[], int[] colorData) {    //int[] colorData = new int[data.length];    for(int i=0; i<data.length; i++) {      if (data[i] == -1) colorData[i] = (255 << 24);      else colorData[i] = (data[i] << 8) | (255 << 16) | (255 << 24);  // red & orange    }    //return colorData;  }    public void run() {    stop=false;    for(int i=0; i<itterations; i++) {      if (data==null) data = getImageData(width, height);      else data = fragment(data, width, height);      if (i%25 == 0) {        toRedOrange(data, colorData);        imSource.newPixels();        //System.out.println(i);        //imSource = new MemoryImageSource(width, height, toRedOrange(data), 0, width);        //im = createImage(imSource);        //System.out.println("made image "+im);        //repaint();        try { Thread.sleep(100); } catch (Exception e) { }      }      if (stop) break;    }    System.out.println("done");  }      public int[] getImageData(int w, int h) {    int[] data = new int[w*h];    for(int i=0; i<data.length; i++) data[i] = 128;    return data;  }    public int[] fragment(int[] data, int w, int h) {    int x0=(int)(w*Math.random()), y0=(int)(h*Math.random());    double ang = Math.random()*2*Math.PI;    double m = Math.tan(ang);    int dz;    if (Math.random() < .5) dz=1;    else dz=-1;    for(int x=0; x<w; x++) {      for(int y=0; y<h; y++) {        if (y > (x0-x)*m) data[w*y+x]+=dz;        else data[w*y+x]-=dz;      }    }    return data;  }    }