Get the latest Java books
h t t p : / /w w w . j a v a c o f f e e b r e a k . c o m /
Looking for Java resources? Check out the Java Coffee Break directory!
"The Mandelbrot set? I know. They are on bookshelves in the libraries."
"Glamorous pictures! But I don't know how to draw them."
"How long does it take you to become a virtual artist, Bill?"
These are the typical questions people would probably ask about when
they first glance the fractal images below or even better ones like
those that can be found elsewhere. The first time when I myself read a
book on fractals, I had thought that heavy efforts must have been spent
on drawing those pictures, which had effectively made me timid until
Because of my current job (computer simulation for biomolecules), I am always concerned with computer graphics. I realized that if I were to write a graphics library, which is to be used to assemble biochemical systems on screen in my own style, I must know more about Java 2D and 3D APIs. So I looked up the Java documentation for the details about how graphics is rendered in Java. A week later I ended up with the AWT applet and Swing app both of which you can find on this page.
I am not going to present here all aspects of Java graphics. In what follows I will talk about only the classes necessary to display a fractal image. So let's get started.
One of the ways in which an image is described is assigning to the
pixels values that represents the graphical properties such as RGB
colors and transparency (alpha value). These properties can be stored in
So how to assign color to a pixel? Easy. Suppose you would like the
int alpha=0xff; int red=0x78; int green=0xc5; int blue=0xa1; pixels[j*width+i]=alpha<<24 | red<<16 | green<<8 | blue;
The bitwise left shifts send the color numbers and alpha value to the
corresponding bit positions, and the bitwise or operator forms the 32
bit integer. If you mistakenly define an
Render an ImageThe next step is to turn the data stored in
MemoryImageSource source = new MemoryImageSource(width,height,pixels,0,width);
This line of code constructs an
Many AWT components inherit the method
Image image = createImage(source);
If you have been a Java programmer for a while and you know a little
bit about fractals, to this point, you won't need to read the rest of my
article. You know how to render an
Explore the Mandelbrot Set
The Mandelbrot set is the domain of convergence of the series built
up by a recursion formula of complex number:
At this point we have to define two coordinate systems. One is the
one on the screen whose origin is at the upper left corner of the
window, the other is the complex plane relative to the Mandelbrot set. I
wrote two methods
I decided to allow the applet to be able to magnify the Mandelbrot set up to a scale of 10000000X. Due to self similarity, the pattern will likely be alike. If one doesn't set a limit to the magnification ratio the applet might crash when the size of the window on the Mandelbrot set becomes numerically too small.
After the applet is invoked, it will display the full Mandelbrot set
(the Mandelbrot Dinosaur), namely, the window
That's it. Isn't it easy?
I exhibit in the above mosaic a number of snapshots taken from this tiny applet. In principle, you can obtain as many fractal pictures as you want, every pattern you will get is likely different from the ones that have already been in existence (though they could look similar), and the price is only a few mouse clicks!
With such a handy applet, you don't need to navigate around the Internet to those fractal galleries which have tons of high-resolution pictures to jam your bandwidth. Just load and click the applet, it will guide you into the beautiful world of fractal. Digest Java also have generously published the code here (other Java applets running on the Internet don't provide this courtesy).
So you have seen how easy it is to draw fractals in your fascinating way. This is really nothing difficult. The charm and beauty don't come from the fractal pictures but the Java language which makes the deployment of a fractal generator on the Internet such a painless job.
A standalone app written in Java 2, which has a simple user interface as shown below, has been released (You can download it below. Currently you can only run it on a DOS console, or you can run it as an applet in browser. Sorry I haven't built a Java-Win32 hybrid program). The basic idea is to build a random fractal image generator which produces a background image to be adopted as an input to more sophisticated and professional graphics tools like Photoshop and Corel Photo Paint. Internet artists can then post-process a fractal image and add whatever they need to put on top of such a fractal background to create a desired effect.
Here are some screenshots of the app. I also install some useful
image processing functionalities, so that you don't need to rely on a
postprocessor like Photoshop for simple image manipulations. For
example, you can invert the image on screen
And, of course, you are able to shift the Mandelbrot window in four different directions: east, west, north and south. There is also a two-speed gear in control of the drift speed. The program can therefore be used as a screensaver if the size is enlarged to cover the whole screen.
About the author
Qian Xie is a physicist at the National Laboratory of Materials Simulation and Design, Tsinghua University, China. His research interests includes computer simulation technologies and computer-aided design in physics, chemistry, and molecular biology. In his spare time, he is learning the Java programming language and using it to write educational software. He can be reached via email at email@example.com, or his personal website.
You can also visit Digest Java's Java Molecular Dynamics, which is an educational tool for studying how a system of particles with soft-core interactions evolves. If you have no interest in physics, I will show you how collective motion of particles resembles the dynamics of a stock market.