Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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 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

                         

» View all related tutorials
Related Tags: c class orm form io method sed format 2d get ip translation return instance transform show for transformation to draw

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.