Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-24-2008, 12:21 AM
Moderator
 
Join Date: Nov 2007
Posts: 1,637
Java Tip will become famous soon enoughJava Tip will become famous soon enough
How to draw Mandelbrot in Java
Code:
import java.awt.Color;import java.awt.Container; import java.awt.Graphics; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.awt.image.WritableRaster; import javax.swing.JFrame; import javax.swing.JPanel; public class MandelbrotTest { public static void main(String[] args) { JFrame frame = new MandelbrotFrame(); frame.show(); } } class MandelbrotFrame extends JFrame { public MandelbrotFrame() { setTitle("MandelbrotTest"); setSize(400, 400); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); Container contentPane = getContentPane(); contentPane.add(new MandelbrotPanel(), "Center"); } } class MandelbrotPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); generate(image); g.drawImage(image, 0, 0, null); } public void generate(BufferedImage image) { int width = image.getWidth(); int height = image.getHeight(); WritableRaster raster = image.getRaster(); ColorModel model = image.getColorModel(); Color fractalColor = Color.red; int argb = fractalColor.getRGB(); Object colorData = model.getDataElements(argb, null); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) { double a = XMIN + i * (XMAX - XMIN) / width; double b = YMIN + j * (YMAX - YMIN) / height; if (!escapesToInfinity(a, b)) raster.setDataElements(i, j, colorData); } } private boolean escapesToInfinity(double a, double b) { double x = 0.0; double y = 0.0; int iterations = 0; do { double xnew = x * x - y * y + a; double ynew = 2 * x * y + b; x = xnew; y = ynew; iterations++; if (iterations == MAX_ITERATIONS) return false; } while (x <= 2 && y <= 2); return true; } private static final double XMIN = -2; private static final double XMAX = 2; private static final double YMIN = -2; private static final double YMAX = 2; private static final int MAX_ITERATIONS = 16;
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Draw Text in Java Java Tip java.awt 0 06-24-2008 12:14 AM
How to Draw Arc in Java Java Tip java.awt 0 06-24-2008 12:12 AM
How to Draw a Rectangle in Java Java Tip java.awt 0 06-23-2008 12:09 AM
How to Draw a Polygon in Java Java Tip java.awt 0 06-23-2008 12:09 AM
how to draw in Java Heather AWT / Swing 2 07-12-2007 12:01 PM


All times are GMT +3. The time now is 01:58 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org