/Users/lyon/j4p/src/ip/raul/Fractals.java

1    package ip.raul; 
2     
3    import gui.ClosableJFrame; 
4    import j2d.ShortImageBean; 
5     
6    import java.awt.*; 
7     
8     
9    public class Fractals { 
10       private int maxIter = 30; 
11       private static float xMin = -2f; 
12       private static float yMax = 1.5f; 
13       private static float xMax = 1f; 
14       private static float yMin = -1.5f; 
15       private short colorR[] = new short[maxIter]; 
16       private short colorG[] = new short[maxIter]; 
17       private short colorB[] = new short[maxIter]; 
18    
19       public Fractals() { 
20   //make some colors, that's all 
21           float deltaTheta = (float) (Math.PI / maxIter); 
22           for (int i = 0; i < maxIter; i++) { 
23               colorR[i] = (short) (Math.sin((i * deltaTheta)) * 255f); 
24               colorG[i] = 0; 
25               colorB[i] = (short) (i * 255 / maxIter); 
26           } 
27       } 
28    
29    
30       public int getColor(float pixelr, float pixeli) { 
31           float zr = 0; 
32           float zi = 0; 
33           float tmp = 0; 
34           int iter = 0; 
35           //z*z+c, in complex basically; 
36           do { 
37               iter++; 
38               tmp = zr; 
39               zr = zr * zr - zi * zi; 
40               zi = 2 * tmp * zi; 
41               zr = zr + pixelr; 
42               zi = zi + pixeli; 
43           } while (((zr * zr + zi * zi) <= 300) && (iter <= maxIter)); 
44           if (iter > maxIter) 
45               return -1; 
46           else 
47               return iter; 
48       } 
49       public static void testMandelbrot() { 
50           ClosableJFrame cjf = new ClosableJFrame("mandlebrot"){ 
51               public void paint(Graphics g){ 
52                   Dimension d = getSize(); 
53                   Image i = getMandelbrot(d.width,d.height); 
54                   g.drawImage(i,0,0,this); 
55               } 
56    
57           }; 
58           cjf.setSize(400,400); 
59           cjf.show(); 
60       } 
61       public static void main(String args[]){ 
62           testMandelbrot(); 
63       } 
64       public static Image getMandelbrot(int w, int h){ 
65           ShortImageBean sib = new ShortImageBean(w,h); 
66           Fractals f = new Fractals(); 
67           f.mandelbrot(sib.getR(), sib.getG(), sib.getB()); 
68           return sib.getImage(); 
69       } 
70       public void mandelbrot(short[][] r, short[][] g, short[][] b) { 
71           int height = r[0].length; 
72           int width = r.length; 
73           int Clr; 
74           float pixelr,pixeli; 
75           for (int y = 0; y < height; y++) 
76               for (int x = 0; x < width; x++) { 
77                   pixelr = xMin + (float) x / width * (xMax - xMin); 
78                   pixeli = yMin + (float) y / height * (yMax - yMin); 
79                   Clr = getColor(pixelr, pixeli); 
80                   if (Clr == -1) { 
81                       r[x][y] = 255; 
82                       g[x][y] = 128; 
83                       b[x][y] = 0; 
84                   } else { 
85                       r[x][y] = colorR[Clr % maxIter]; 
86                       g[x][y] = colorG[Clr % maxIter]; 
87                       b[x][y] = colorB[Clr % maxIter]; 
88                   } 
89               } 
90       } 
91    
92       public int getMaxIter() { 
93           return maxIter; 
94       } 
95    
96       public void setMaxIter(int maxIter) { 
97           this.maxIter = maxIter; 
98       } 
99    
100      public static float getxMin() { 
101          return xMin; 
102      } 
103   
104      public static void setxMin(float xMin) { 
105          Fractals.xMin = xMin; 
106      } 
107   
108      public static float getyMax() { 
109          return yMax; 
110      } 
111   
112      public static void setyMax(float yMax) { 
113          Fractals.yMax = yMax; 
114      } 
115   
116      public static float getxMax() { 
117          return xMax; 
118      } 
119   
120      public static void setxMax(float xMax) { 
121          Fractals.xMax = xMax; 
122      } 
123   
124      public static float getyMin() { 
125          return yMin; 
126      } 
127   
128      public static void setyMin(float yMin) { 
129          Fractals.yMin = yMin; 
130      } 
131   
132      public short[] getColorR() { 
133          return colorR; 
134      } 
135   
136      public void setColorR(short[] colorR) { 
137          this.colorR = colorR; 
138      } 
139   
140      public short[] getColorG() { 
141          return colorG; 
142      } 
143   
144      public void setColorG(short[] colorG) { 
145          this.colorG = colorG; 
146      } 
147   
148      public short[] getColorB() { 
149          return colorB; 
150      } 
151   
152      public void setColorB(short[] colorB) { 
153          this.colorB = colorB; 
154      } 
155  }