import javax.swing.*;
import java.awt.*;
public class Mandel extends JFrame{
public static void main(String[] args){
new Mandel();
}
public Mandel(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new MyCanvas());
setSize(500,400);
setVisible(true);
show();
}
}
class MyCanvas extends Canvas{
private final int MAX=32;
public void paint(Graphics g){
Dimension d = getSize();
int n;
double MinRe = -2.0;
double MaxRe = 1.0;
double MinIm = -1.2;
double MaxIm = MinIm+(MaxRe-MinRe)*d.height/d.width;
double Re_factor = (MaxRe-MinRe)/(d.width-1);
double Im_factor = (MaxIm-MinIm)/(d.height-1);
double Z_re, Z_im;
double Z_re2, Z_im2;
double value;
for(int y=0; y<d.height; ++y){
double c_im = MaxIm - y*Im_factor;
for(int x=0; x<d.width; ++x){
double c_re = MinRe + x*Re_factor;
Z_re = c_re; Z_im = c_im;
boolean isInside = true;
for( n=0; n<MAX; ++n){
Z_re2 = Z_re*Z_re;
Z_im2 = Z_im*Z_im;
if( Z_re2 + Z_im2 > 4){
g.setColor(new Color(0,0,0));
g.drawLine(x,y,x,y);
break;
}
Z_im = 2*Z_re*Z_im + c_im;
Z_re = Z_re2 - Z_im2 + c_re;
if(Z_re2 + Z_im2 < 4){
g.setColor(new Color(255,255,255));
g.drawLine(x, y, x, y);
}else{
g.setColor(new Color( MAX/2, n/2, n*4));
g.drawLine(x, y, x, y);
}
}
}
}
}
}