Draw a Mandelbrot

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

Draw a Mandelbrot

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

Draw a Mandelbrot

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 MandelbrotExample.java

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();
  frame.show();
  }
}
class DrawMandelbrot extends JFrame {
  public DrawMandelbrot() {
  setTitle("Mandelbrot Example");
  setSize(350300);
  Container contentPane = getContentPane();
  contentPane.add(new MandelbrotPanel(), "Center");
  }
}
class MandelbrotPanel extends JPanel {
  public void paintComponent(Graphics g) {
  super.paintComponent(g);
  BufferedImage bufferedImage = new BufferedImage(getWidth(), getHeight(),
  BufferedImage.TYPE_INT_ARGB);
  generate(bufferedImage);
  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;
  count++;
  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