1
2 import java.awt.Color;
3 import java.awt.Graphics;
4 import java.awt.Graphics2D;
5 import java.awt.image.BufferedImage;
6 import javax.swing.JApplet;
7 import javax.swing.JPanel;
8
9
10
11 @author
12
13 public class SimpleMandelbrot extends JApplet {
14
15 BufferedImage img;
16
17 public SimpleMandelbrot() {
18 super();
19
20 setContentPane(new JPanel() {
21
22 @Override
23 public void paint(Graphics g) {
24 super.paint(g);
25 Graphics2D g2d = (Graphics2D) g;
26 g2d.drawImage(img, 0, 0, this);
27 }
28 });
29 }
30
31 @Override
32 public void init() {
33 renderImage();
34 }
35
36
37
38
39 private void renderImage() {
40 int w = getWidth(), h = getHeight();
41 img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
42 double a, b;
43 int c = new Color(0, 0, 0).getRGB();
44
45 for (int i = 0; i < h; i++) {
46 b = (double) i * 3 / h - 1.5;
47 for (int j = 0; j < w; j++) {
48 a = (double) j * 3 / w - 2;
49 if (isElement(a, b)) {
50 img.setRGB(j, i, c);
51 }
52 }
53 }
54 }
55
56
57
58
59 @param
60 @param
61 @return
62
63 private boolean isElement(double a, double b) {
64 double x = 0, x2, y = 0;
65 for (int n = 0; n < 400; n++) {
66 x2 = x * x - y * y + a;
67 y = 2 * x * y + b;
68 x = x2;
69 if (x * x + y * y > 4) {
70 return false;
71 }
72 }
73 return true;
74 }
75 }
76