1 9 10 package org.nfunk.jepexamples; 11 12 import java.awt.*; 13 import java.util.*; 14 15 import org.nfunk.jep.*; 16 import org.nfunk.jep.type.*; 17 import org.nfunk.jep.evaluation.*; 18 19 22 public class FractalCanvas extends Canvas { 23 24 private int scaleX, scaleY; 25 26 private Dimension dimensions; 27 28 private int iterations, nEvals; 29 30 private Image buffer; 31 32 private boolean initializedBuffer, changedFunction, hasError; 33 34 private JEP myParser; 35 private ExpressionCompiler expressionCompiler; 36 private CommandEvaluator evaluator; 37 private CommandElement[] commands; 38 private SymbolTable symTab; 39 40 private java.awt.TextField exprField; 41 42 45 public FractalCanvas(String initialExpression, java.awt.TextField exprField_in) { 46 iterations = 20; 47 nEvals = 0; 48 scaleX = 100; 49 scaleY = 100; 50 dimensions = getSize(); 51 initializedBuffer = false; 52 changedFunction = true; 53 hasError = true; 54 exprField = exprField_in; 55 initParser(initialExpression); 56 expressionCompiler = new ExpressionCompiler(); 57 evaluator = new CommandEvaluator(); 58 symTab = myParser.getSymbolTable(); 59 } 60 61 64 private void initParser(String initialExpression) { 65 myParser = new JEP(); 67 68 myParser.addStandardFunctions(); 70 71 myParser.addStandardConstants(); 73 myParser.addComplex(); 74 75 myParser.addVariable("z",0,0); 77 myParser.addVariable("c",0,0); 78 79 setExpressionString(initialExpression); 80 } 81 82 85 public void setExpressionString(String newString) { 86 nEvals = 0; 87 88 myParser.parseExpression(newString); 90 91 hasError = myParser.hasError(); 93 if (hasError) 94 exprField.setForeground(Color.red); 95 else 96 exprField.setForeground(Color.black); 97 98 changedFunction = true; 99 } 100 101 public void setIterations(int iterations_in) { 102 iterations = iterations_in; 103 } 104 105 private void paintWhite(Graphics g) { 106 g.setColor(Color.white); 107 g.fillRect(0,0,dimensions.width,dimensions.height); 108 } 109 110 111 private void paintFractalSansJEP(Graphics g) { 112 System.out.println("paintFractalSansJEP()"); 113 114 116 System.out.println("done."); 117 } 118 119 private void paintRegion(Graphics g, int x, int y, 120 int width, int height, int depth, 121 int depth_max) { 122 double re, im, p, q, resq, imsq, imtemp; 123 int count; 124 125 if (depth == depth_max) { 126 p = (double)(x+width/2-230)/scaleX; 127 q = (double)(y+height/2-150)/scaleY; 128 count = 0; 129 re = 0; 130 im = 0; 131 resq = 0; 132 imsq = 0; 133 134 while ((count < iterations) && ((resq + imsq) < 4.0)) { 135 imtemp = 2 * re * im; 136 re = resq - imsq + p; 137 im = imtemp + q; 138 count++; 139 resq = re * re; 140 imsq = im * im; 141 nEvals++; 142 } 143 if (count != iterations) { 145 g.setColor(new Color(0, 0, (int)(255.0*(Math.sqrt(count)/Math.sqrt(iterations))))); 146 g.fillRect(x, y, width, height); 147 } 148 149 } else { 150 paintRegion(g, x, y, width/2, height - height/2, depth+1, depth_max); 151 paintRegion(g, x + width/2, y, width - width/2, height/2, depth+1, depth_max); 152 paintRegion(g, x, y + height/2, width/2, height - height/2, depth+1, depth_max); 153 paintRegion(g, x + width/2, y + height/2, width - width/2, height - height/2, depth+1, depth_max); 154 } 155 } 156 157 private void paintFractal(Graphics g) { 158 Complex z,c,temp; 159 int count; 160 161 c = myParser.addVariable("c", 0, 0); 162 z = myParser.addVariable("z", 0, 0); 163 164 for (int x = 0; x <= (dimensions.width-1); x++) { 165 for (int y = 0; y <= (dimensions.height-1); y++) { 166 count = 0; 167 c.set((double)(x-230)/scaleX, 168 (double)(y-150)/scaleY); 169 z.set(0,0); 170 171 while ((count < iterations) && (z.abs2() < 4.0)) { 172 z.set(myParser.getComplexValue()); 173 count++; 174 nEvals++; 175 } 176 177 if (count != iterations) { 178 g.setColor(new Color(0, 0, (int)(255.0*(Math.sqrt(count)/Math.sqrt(iterations))))); 179 g.fillRect(x, y, 1, 1); 180 } 181 } 182 } 183 } 184 185 private void paintFractalWithCompiler(Graphics g) { 186 Complex z,c,temp; 187 int count; 188 189 c = myParser.addVariable("c", 0, 0); 190 z = myParser.addVariable("z", 0, 0); 191 try { 192 commands = expressionCompiler.compile(myParser.getTopNode()); 193 } catch (ParseException e) { 194 System.out.println("Failed to compile expression"); 195 e.printStackTrace(); 196 } 197 198 for (int x = 0; x <= (dimensions.width-1); x++) { 199 for (int y = 0; y <= (dimensions.height-1); y++) { 200 count = 0; 201 c.set((double)(x-230)/scaleX, 202 (double)(y-150)/scaleY); 203 z.set(0,0); 204 205 while ((count < iterations) && (z.abs2() < 4.0)) { 206 try { 207 temp = (Complex)evaluator.evaluate(commands, symTab); 208 z.set(temp); 209 } catch (Exception e) { 210 e.printStackTrace(); 212 } 213 count++; 214 nEvals++; 215 } 216 217 if (count != iterations) { 218 g.setColor(new Color(0, 0, (int)(255.0*(Math.sqrt(count)/Math.sqrt(iterations))))); 219 g.fillRect(x, y, 1, 1); 220 } 221 } 222 } 223 } 224 225 private void paintNonJEPFractal(Graphics g) { 226 double re, im, p, q, resq, imsq, imtemp; 227 int count; 228 229 for (int x = 0; x <= (dimensions.width-1); x++) { 230 for (int y = 0; y <= (dimensions.height-1); y++) { 231 p = (double)(x-230)/scaleX; 232 q = (double)(y-150)/scaleY; 233 count = 0; 234 re = 0; 235 im = 0; 236 resq = 0; 237 imsq = 0; 238 239 while ( (count < iterations) && ((resq + imsq) < 4.0) ) { 240 imtemp = 2 * re * im; 241 re = resq - imsq + p; 242 im = imtemp + q; 243 resq = re * re; 244 imsq = im * im; 245 count++; 246 nEvals++; 247 } 248 if (count != iterations) { 250 g.setColor(new Color(0, 0, (int)(255.0*(Math.sqrt(count)/Math.sqrt(iterations))))); 251 g.fillRect(x, y, 1, 1); 252 } 253 } 254 } 255 } 256 257 public void paint(Graphics g) { 258 Date start, finish; 259 260 dimensions = getSize(); 261 paintWhite(g); 262 if (!hasError) { 263 System.out.println("Painting... "); 264 start = new Date(); 265 nEvals = 0; 266 paintFractalWithCompiler(g); 268 finish = new Date(); 270 System.out.print("done. sec/eval: "); 271 double seconds = ( finish.getTime() - start.getTime() ) / 1000.0; 272 System.out.println(seconds/nEvals); 273 } 274 294 } 295 } 296 | Popular Tags |