import java.lang.Thread; public class Mandelbrot extends Thread { public Mandelbrot(int width, int height, double left, double right, double top, double bottom) { setSize(width, height); setBoundaries(left, right, top, bottom); setMaxIterations(10000); } public void run() { while (true) { if (_recalculate) { calculate(); _recalculate = false; } } } public void setBoundaries(double left, double right, double top, double bottom) { // Which bit of the set to view. _left = left; _right = right; _top = top; _bottom = bottom; _recalculate = true; } public void setSize(int width, int height) { // Size of our view (zoom). _width = width; _height = height; _set = new int[width][height]; _recalculate = true; } public void setMaxIterations(int n) { _iterations = n; _recalculate = true; } public int getMaxIterations() { return _iterations; } public int value(int x, int y) { return _set[x][y]; } private void calculate() { for (int x = 0; x < _width; x++) { for (int y = 0; y < _height; y++) { double zr = 0.0, zi = 0.0; double cr = _left + x * (_right - _left) / _width; double ci = _top + y * (_bottom - _top) / _height; double zrsq = zr * zr; double zisq = zi * zi; for (_set[x][y] = 0; (zrsq + zisq <= 4.0) && (_set[x][y] < _iterations); _set[x][y]++) { zr = zrsq - zisq + cr; zi = 2 * zr * zi + ci; zrsq = zr * zr; zisq = zi * zi; } } } } private int[][] _set; private boolean _recalculate; private double _left; private double _right; private double _top; private double _bottom; private int _width; private int _height; private int _iterations; }