public class BasicFractal{ // The width and height of the window. private static final int WIDTH = 350; private static final int HEIGHT = 350; // The ComplexNumber to base the Julia Set off of. private static ComplexNumber c = new ComplexNumber(-0.223, 0.745); // Array of booleans stating which pixels make up the fractal. private boolean[][] values = null; // The bounds of the Complex Plane to graph. private double minX = -1.5; private double maxX = 1.5; private double minY = -1.5; private double maxY = 1.5; // The image to draw on. private BufferedImage image = null; // The maximum Magnitude of the ComplexNumer to be allowed in the Set. private double threshold = 1; // The number of times that the algorithm recurses. private int iterations = 50; /** * The meat of the program is in the constructor. * This handles the Frame, filling the array, painting, * and coloring. */ public BasicFractal(){ // Create a BufferedImage to paint on. image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB); // Fill the booean[][]. getValues(); // Go through the array and set the pixel color on the BufferedImage // according to the values in the array. for(int i=0;i<WIDTH;i++){ for(int j=0;j<HEIGHT;j++){ // If the point is in the Set, color it White, else, color it Black. if(values[i][j]) image.setRGB(i, j, Color.WHITE.getRGB()); if(!values[i][j]) image.setRGB(i, j, Color.BLACK.getRGB()); } } // Create a Frame to display the Fractal. JFrame f = new JFrame("Basic Fractal Example"){ // Override the paint method (for simplicity) @Override public void paint(java.awt.Graphics g){ g.drawImage(image,0,0,null); } }; // Set other Frame settings. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(WIDTH,HEIGHT); f.repaint(); f.setVisible(true); } /** * Fill the values boolean[][] with data. */ private void getValues(){ values = new boolean[WIDTH][HEIGHT]; // Go through each pixel. for(int i=0;i<WIDTH;i++){ for(int j=0;j<HEIGHT;j++){ /** * Each pixel represents a ComplexNumber. The pixel in the center * represents 0,0 on the Complex Plane. The following two lines * treat the window as a scaled version of the bounds set above * (See min and max vars above). They scale the numbers, and * shift everything around so it lies up right. */ double a = (double)i*(maxX-minX)/(double)WIDTH + minX; double b = (double)j*(maxY-minY)/(double)HEIGHT + minY; // fill the boolean array. values[i][j] = isInSet(new ComplexNumber(a,b)); } } } /** * Determine if the ComplexNumber cn fits in the Fractal pattern. * This is the basic quadratic julia set. The formula is: * f(z+1) = z^2+c * where z is a complex number, * and where c is a constant complex number. * @param cn The ComplexNumber to check. * @return true if it is in the set, else false. */ private boolean isInSet(ComplexNumber cn){ for(int i=0;i<iterations;i++){ // The basic Julia Set Algorithm. cn = cn.square().add(c); } // If the threshold^2 is larger than the magnitude, return true. return cn.magnitude()<threshold*threshold; } /** * The Method the execute this program * @param args Command Line args */ public static void main(String[] args){ new BasicFractal(); } } public class ComplexNumber{ private double a, b; // Create a Complex Number with the given real numbers. public ComplexNumber(double a, double b){ this.a = a; this.b = b; } // Method for squaring a ComplexNumber public ComplexNumber square(){ return new ComplexNumber(this.a*this.a - this.b*this.b, 2*this.a*this.b); } // Method for adding 2 complex numbers public ComplexNumber add(ComplexNumber cn){ return new ComplexNumber(this.a+cn.a, this.b+cn.b); } // Method for calculating magnitude^2 (how close the number is to infinity) public double magnitude(){ return a*a+b*b; } }