import java.awt.*; class Drawing extends Canvas implements Runnable { private Image saved = null; private Graphics gSaved, gWindow; private int width, height; private Thread computation = null; private volatile boolean stop = false; private double x1, x2, y1, y2; private int nbIterations; public void init() { Dimension d = getSize(); width = d.width; height = d.height; saved = createImage(width, height); gSaved = saved.getGraphics(); gWindow = this.getGraphics(); this.delete(); } public void paint(Graphics g) { g.drawImage(saved, 0, 0, this); } public Dimension getMinimumSize() { return new Dimension(1200, 900); } public Dimension getPreferredSize() { return getMinimumSize(); } public synchronized void compute(double x1, double y1, double x2, double y2, int nb) { this.interrupt(); this.delete(); this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; this.nbIterations = nb; computation = new Thread(this, "computation of the fractal"); computation.start(); } public synchronized void interrupt() { stop = true; while (computation != null) { try { wait(); } catch(InterruptedException e) { } } stop = false; } public void run() { Mandelbrot m = new Mandelbrot(nbIterations); double dx = (x2 - x1) / width; double dy = (y2 - y1) / height; main: for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) if (stop) break main; else { Color c = m.color(x1 + i * dx, y1 + j * dy); gWindow.setColor(c); gWindow.drawLine(i, j, i, j); gSaved.setColor(c); gSaved.drawLine(i, j, i, j); } synchronized(this) { computation = null; notify(); } } public int width() { return width; } public int height() { return height; } private void delete() { gSaved.setColor(Color.black); gSaved.fillRect(0, 0, width, height); gWindow.setColor(Color.black); gWindow.fillRect(0, 0, width, height); } }