1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323:
| /* * @(#)fractal.java - Fractal Program Mandelbrot Set * www.eckhard-roessel.de * Copyright (c) Eckhard Roessel. All Rights Reserved. * 06/30/2000 - 03/29/2000 */ import java.awt.*; import java.applet.*; import java.awt.event.*; /** * @version 1.1 * @author Eckhard Roessel * @modified 03/29/2001 * @modified 08/16/2006 tweaked by Duncan Mullier 8/2006 */ public class Fractal extends Applet implements MouseListener, MouseMotionListener { class HSB {//djm added, it makes it simpler to have this code in here than in the C# public float rChan,gChan,bChan; public HSB() { rChan=gChan=bChan=0; } public void fromHSB(float h,float s,float b) { float red = b; float green = b; float blue = b; if (s != 0) { float max = b; float dif = b * s / 255f; float min = b - dif;
float h2 = h * 360f / 255f;
if (h2 < 60f) { red = max; green = h2 * dif / 60f + min; blue = min; } else if (h2 < 120f) { red = -(h2 - 120f) * dif / 60f + min; green = max; blue = min; } else if (h2 < 180f) { red = min; green = max; blue = (h2 - 120f) * dif / 60f + min; } else if (h2 < 240f) { red = min; green = -(h2 - 240f) * dif / 60f + min; blue = max; } else if (h2 < 300f) { red = (h2 - 240f) * dif / 60f + min; green = min; blue = max; } else if (h2 <= 360f) { red = max; green = min; blue = -(h2 - 360f) * dif / 60 + min; } else { red = 0; green = 0; blue = 0; } }
rChan = Math.round(Math.min(Math.max(red, 0f), 255)); gChan = Math.round(Math.min(Math.max(green, 0), 255)); bChan = Math.round(Math.min(Math.max(blue, 0), 255)); } } private final int MAX = 256; // max iterations private final double SX = -2.025; // start value real private final double SY = -1.125; // start value imaginary private final double EX = 0.6; // end value real private final double EY = 1.125; // end value imaginary private static int x1, y1, xs, ys, xe, ye; private static double xstart, ystart, xende, yende, xzoom, yzoom; private static boolean action, rectangle, finished; private static float xy; private Image picture; private Graphics g1; private Cursor c1, c2; private HSB HSBcol=new HSB(); public void init() // all instances will be prepared { //HSBcol = new HSB(); setSize(640,480); finished = false; addMouseListener(this); addMouseMotionListener(this); c1 = new Cursor(Cursor.WAIT_CURSOR); c2 = new Cursor(Cursor.CROSSHAIR_CURSOR); x1 = getSize().width; y1 = getSize().height; xy = (float)x1 / (float)y1; picture = createImage(x1, y1); g1 = picture.getGraphics(); finished = true; }
public void destroy() // delete all instances { if (finished) { removeMouseListener(this); removeMouseMotionListener(this); picture = null; g1 = null; c1 = null; c2 = null; System.gc(); // garbage collection } }
public void start() { action = false; rectangle = false; initvalues(); xzoom = (xende - xstart) / (double)x1; yzoom = (yende - ystart) / (double)y1; mandelbrot(); }
public void stop() { } public void paint(Graphics g) { update(g); } public void update(Graphics g) { g.drawImage(picture, 0, 0, this); if (rectangle) { g.setColor(Color.white); if (xs < xe) { if (ys < ye) g.drawRect(xs, ys, (xe - xs), (ye - ys)); else g.drawRect(xs, ye, (xe - xs), (ys - ye)); } else { if (ys < ye) g.drawRect(xe, ys, (xs - xe), (ye - ys)); else g.drawRect(xe, ye, (xs - xe), (ys - ye)); } } } private void mandelbrot() // calculate all points { int x, y; float h, b, alt = 0.0f; action = false; setCursor(c1); showStatus("Mandelbrot-Set will be produced - please wait..."); for (x = 0; x < x1; x+=2) for (y = 0; y < y1; y++) { h = pointcolour(xstart + xzoom * (double)x, ystart + yzoom * (double)y); // color value if (h != alt) { b = 1.0f - h * h; // brightnes ///djm added ///HSBcol.fromHSB(h,0.8f,b); //convert hsb to rgb then make a Java Color ///Color col = new Color(0,HSBcol.rChan,HSBcol.gChan,HSBcol.bChan); ///g1.setColor(col); //djm end //djm added to convert to RGB from HSB g1.setColor(Color.getHSBColor(h, 0.8f, b)); //djm test Color col = Color.getHSBColor(h,0.8f,b); int red = col.getRed(); int green = col.getGreen(); int blue = col.getBlue(); //djm alt = h; } g1.drawLine(x, y, x + 1, y); } showStatus("Mandelbrot-Set ready - please select zoom area with pressed mouse."); setCursor(c2); action = true; } private float pointcolour(double xwert, double ywert) // color value from 0.0 to 1.0 by iterations { double r = 0.0, i = 0.0, m = 0.0; int j = 0; while ((j < MAX) && (m < 4.0)) { j++; m = r * r - i * i; i = 2.0 * r * i + ywert; r = m + xwert; } return (float)j / (float)MAX; } private void initvalues() // reset start values { xstart = SX; ystart = SY; xende = EX; yende = EY; if ((float)((xende - xstart) / (yende - ystart)) != xy ) xstart = xende - (yende - ystart) * (double)xy; }
public void mousePressed(MouseEvent e) { e.consume(); if (action) { xs = e.getX(); ys = e.getY(); } } public void mouseReleased(MouseEvent e) { int z, w; e.consume(); if (action) { xe = e.getX(); ye = e.getY(); if (xs > xe) { z = xs; xs = xe; xe = z; } if (ys > ye) { z = ys; ys = ye; ye = z; } w = (xe - xs); z = (ye - ys); if ((w < 2) && (z < 2)) initvalues(); else { if (((float)w > (float)z * xy)) ye = (int)((float)ys + (float)w / xy); else xe = (int)((float)xs + (float)z * xy); xende = xstart + xzoom * (double)xe; yende = ystart + yzoom * (double)ye; xstart += xzoom * (double)xs; ystart += yzoom * (double)ys; } xzoom = (xende - xstart) / (double)x1; yzoom = (yende - ystart) / (double)y1; mandelbrot(); rectangle = false; repaint(); } }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { } public void mouseDragged(MouseEvent e) { e.consume(); if (action) { xe = e.getX(); ye = e.getY(); rectangle = true; repaint(); } } public void mouseMoved(MouseEvent e) { }
public String getAppletInfo() { return "fractal.class - Mandelbrot Set a Java Applet by Eckhard Roessel 2000-2001"; } } |