mandelbrot.MainDraw.java Source code

Java tutorial

Introduction

Here is the source code for mandelbrot.MainDraw.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mandelbrot;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.apache.commons.math3.complex.*;
import processing.core.*;
import javax.swing.*;

/**
 *
 * @author balagardash
 */
public class MainDraw extends PApplet {

    //static JFrame frame;
    //static JButton right, left, up, down;
    //static JSlider slider;
    //static JSlider vertical;

    public static void main(String[] args) {
        PApplet.main("mandelbrot.MainDraw");
        //GUIhandler guIhandler = new GUIhandler();
    }

    public float minX, maxX, minY, maxY, step;

    @Override
    public void settings() {
        size(200, 200, P2D);
        //fullScreen(P2D);
    }

    @Override
    public void setup() {
        background(255);
        //noLoop();
        minX = 2;
        maxX = 2;
        minY = 2;
        maxY = 2;
        step = (float) 0.2;
    }

    @Override
    public void draw() {
        /*GSlider gs = new GSlider(this, 
            (float) 0.0, 
            (float)0.0, 
            (float)100.0, 
            (float) 10.0, 
            (float) 50);*/

        //TODO use int value from iteration to color the 
        /*
        minX = (float) (2);//(0.5*getZoomX()));
        maxX = (float) (2);//(0.5*getZoomX()));
        minY = (float) (2);//(0.5*getZoomY()));
        maxY = (float) (2);//(0.5*getZoomY()));
        */
        System.out.println(step);
        translate(width / 2, height / 2);
        for (int i = -width / 2; i < width / 2; i++) {
            for (int j = -height / 2; j < height / 2; j++) {
                //translate(width/2, height/2);
                float x = PApplet.map(i, -width / 2, width / 2, -minX, maxX);
                float y = PApplet.map(j, -height / 2, height / 2, -minY, maxY);
                int check = iterate(new Complex(x, y));
                colorMode(HSB);
                check = (int) map(check, 0, 400, 150, 0);
                stroke(check, 180, 150);
                point(i, j);
            }
        }
        //System.out.println(val);
    }

    //TODO add GUI for zooming
    //This one does not work properly
    @Override
    public void keyPressed() {
        switch (key) {
        case 'i':
            step += 0.1 * step;
            break;
        case 'k':
            step -= 0.1 * step;
            break;
        case 'p':
            minX += (float) step;
            minY += (float) step;
            maxX += (float) step;
            maxY += (float) step;
            break;
        case 'o':
            minX -= (float) step;
            minY -= (float) step;
            maxX -= (float) step;
            maxY -= (float) step;
            break;
        case 'w':
            minY += (float) step;
            maxY -= (float) step;
            break;
        case 's':
            minY -= (float) step;
            maxY += (float) step;
            break;
        case 'd':
            minX -= (float) step;
            maxX += (float) step;
            break;
        case 'a':
            minX += (float) step;
            maxX -= (float) step;
            break;
        default:
            break;
        }
    }

    public static Complex mandelFunction(Complex z, Complex c) {
        Complex zz = z.multiply(z);
        return (c.add(zz));
    }

    //TODO chane it to static int which will return number
    //of iterations it took to converge    
    public static int iterate(Complex c) {
        Complex z = new Complex(0, 0);
        int i;
        for (i = 0; i < 800; i++) {
            //Complex pz = z;
            z = mandelFunction(z, c);
            if (z.abs() > 2) {
                return i;
            }
        }
        return i;
    }

}