Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Draw a Mandelbrot

A Mandelbrot is a collection of points in the plane whose boundary forms a fractal.


Draw a Mandelbrot


In this section, you will studied how to draw a Mandelbrot.

A Mandelbrot is a collection of points in the plane whose boundary forms a fractal. It is having a complicated structure, which does not simplify at any given magnification. We are providing you an example to draw a Mandelbrot.

In the given example, the class WritableRaster extends Raster to provide pixel writing capabilities. To translate a pixel value to color components (for example, red, green, and blue) and an alpha component, we have used the class ColorModel. It is necessary to convert pixel value into color and alpha components to render an image to the screen.

The method bufferedImage.getRaster() returns the pixel values. The method bufferedImage.getColorModel() returns the color components and an alpha component being translated from the pixel values. The method getDataElement(c,null) of class ColorModel returns a pixel value with an array of unnormalized color/alpha components. The method setDataElements(j,k,object) of class WritableRaster  sets the data for the pixel values.

Following code draws an image of Mandelbrot:

g.drawImage(bufferedImage, 0, 0, null)

Here is the code of

import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;

public class MandelbrotExample {
  public static void main(String[] args) {
  JFrame frame= new DrawMandelbrot();;
class DrawMandelbrot extends JFrame {
  public DrawMandelbrot() {
  setTitle("Mandelbrot Example");
  Container contentPane = getContentPane();
  contentPane.add(new MandelbrotPanel(), "Center");
class MandelbrotPanel extends JPanel {
  public void paintComponent(Graphics g) {
  BufferedImage bufferedImage = new BufferedImage(getWidth(), getHeight(),
  g.drawImage(bufferedImage, 00null);
public void generate(BufferedImage bufferedImage) {
  int w = bufferedImage.getWidth();
  int h = bufferedImage.getHeight();
  WritableRaster writableRaster = bufferedImage.getRaster();
  ColorModel colorModel = bufferedImage.getColorModel();
  Color color = Color.cyan;
  int c = color.getRGB();
  Object object = colorModel.getDataElements(c, null);

  for (int j = 0; j < w; j++)
  for (int k = 0; k < h; k++) {
  double p = Pmin + j * (Pmax - Pmin) / w;
  double q = Qmin + k * (Qmax - Qmin) / h;
  if (!escapes(p, q))
  writableRaster.setDataElements(j, k, object);
  private boolean escapes(double p, double q) {
  double x = 0.4;
  double y = 0.4;
  int count = 0;
  do {
  double X = x * x - y * y + p;
  double Y = * x * y + q;
  x = X;
  y = Y;
  if (count == MAX_COUNTS)
  return false;
  while (x <= && y <= 2);
  return true;
  double Pmin = -2;
  double Pmax = 2;
  double Qmin = -2;
  double Qmax = 2;
  int MAX_COUNTS = 16;

Output will be displayed as:

Download Source Code


Share on Google+Share on Google+

Draw a Mandelbrot

Posted on: October 11, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Draw a Mandelbrot  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image