Drawing the Mandelbrot Set
To make this interesting we want to actually draw pictures of the Mandelbrot Set. To do this we'll move the actual calculation into a thread in an applet and then draw the results into a bitmap. Here's the code:

import java.applet.Applet; import java.awt.*; public class Mandelbrot extends Applet { int xdim; int ydim; double xstart = -2.0; double ystart = -1.25; int Mandel[][]; double gap = 0.05; int max_iterations = 256; public void paint(Graphics g) { int i,j,k; Complex z, c; xdim = size().; ydim = size().; gap = 2.5/ydim; Mandel = new int[xdim][ydim]; for (i=0; i < xdim; i++) { for (j=0; j < ydim; j++) { c = new Complex(xstart + i*gap, ystart + j*gap); z = new Complex(0.0, 0.0); for (k = 0; z.Magnitude() < 2.0 && k < max_iterations; k++) { z = z.Times(z); z = z.Plus(c); } g.setColor(selectColor(k)); g.fillRect(i, j, 1, 1); } } } protected Color selectColor (int num_iterations) { if (num_iterations max_iterations) return Color.black; else if (num_iterations 9*max_iterations/10) return Color.darkGray; else if (num_iterations 8*max_iterations/10) return Color.gray; else if (num_iterations 7*max_iterations/10) return Color.magenta; else if (num_iterations 6*max_iterations/10) return Color.cyan; else if (num_iterations 5*max_iterations/10) return Color.blue; else if (num_iterations 4*max_iterations/10) return Color.green; else if (num_iterations 3*max_iterations/10) return Color.yellow; else if (num_iterations 2*max_iterations/10) return Color.orange; else if (num_iterations 1*max_iterations/10) return Color.red; else return Color.white; } } This program is minimal. It should really create an ImageProducer which draws the Mandelbrot set. There are also a lot of additions that could be made to the parameters to allow for zooming in on particular regions. In fact you could even implement this as a Canvas in an applet with various controls to select the area of interest. This will all be investigated in the Cafe Au Lait newsletter.

For more details on the Mandelbrot Set see the first chapter of A.K. Dewdney's The Armchair Universe .

Exercises
  1. Explore different starting points and gap sizes for the Mandelbrot set. To make this easier add user input to dynamically select the starting point and gap size.
  2. What happens if you allow the x and y gap size to be chosen independently?

 

 

Bhopal news
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

freelance web designer india ecommerce web developer | Ecommerce web design, software developer india | Web hosting India Windows hosting | India web hosting Windows hosting India | India software developer | web designer india