The code below is for the fractal mandelbrot. It's work perfectly, but now I want to use the notion of thread on it. The result should be the same but the job must be doing by multiple Thread +10.
Here is my code:
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
public class test extends JFrame {
private final int MAX_ITER = 570;
private final double ZOOM = 150;
private BufferedImage I;
private double zx, zy, cX, cY, tmp;
private static int x,y;
public test() throws InterruptedException {
super("Mandelbrot Set");
setBounds(100, 100, 800, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
I = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
for ( y = 0; y < getHeight(); y++) {
for ( x = 0; x < getWidth(); x++) {
Thread T = new Thread() {//*******
public void run() {
zx = zy = 0;
cX = (x - 400) / ZOOM;
cY = (y - 300) / ZOOM;
int iter = MAX_ITER;
while (zx * zx + zy * zy < 4 && iter > 0) {
tmp = zx * zx - zy * zy + cX;
zy = 2.0 * zx * zy + cY;
zx = tmp;
iter--;
}
I.setRGB(x, y, iter | (iter << 8));
System.out.println(Thread.currentThread().getId());
}
};//*******
T.start();//********
T.join();//**********
}
}
}
@Override
public void paint(Graphics g) {
g.drawImage(I, 0, 0, this);
}
public static void main(String[] args) throws InterruptedException {
new test().setVisible(true);
}
}
I tried to instantiate thread in the loop for but I didn't get the result I want any suggestion?