Question

Hi so I've written these code to show a mandelbrot set but everytime I run it, it only shows black. I've been trying to see where my errors are but failed to find them so it would really helped if you guys can show them to me.

Here's the code for the number class:

public class Complex {
    double real;
    double imaginary;
    public Complex(){
        real = 0;
        imaginary = 0;
    }
    public Complex(double real, double imaginary){
        this.real = real;
        this.imaginary = imaginary;
    }

    public double getReal(){
        return real;
    }
    public double getImaginary(){
        return imaginary;
    }
    public void setReal(int a){
        real = a;
    }
    public void setImaginary(int b){
        imaginary = b;
    }
    public String toString(){
        if(real == 0){
            return imaginary + "i";
        }else if(imaginary == 0){
            return real + "";
        }else if(imaginary < 0){
            return real + " - " + (-imaginary) + "i";
        }else{
            return real + " + " + imaginary + "i";
        }
    }
    public Complex square(){
        double re = real*real - imaginary*imaginary;
        double im = real*imaginary + imaginary*real;
        return new Complex(re, im);

    }
    public double modulusSquared(){
        double x = real*real;
        double y = imaginary*imaginary;
        return x + y;
    }
    public Complex add(Complex d){
        double re = real + d.real;
        double im = imaginary + d.imaginary;
        return new Complex(re, im);
    }

}

And this is the GUI:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class main {

    public static void main(String[] args) {
        mandFrame frame = new mandFrame("Mandelbrot Set");
        frame.init();

    }

}
class mandFrame extends JFrame{
    Complex max = new Complex(2.0, 1.6);
    Complex min = new Complex(-2.0, -1.6);
    int iteration = 100;
    int width = 600;
    int height = 600;
    int depth = 255;
    Complex[][] dot = new Complex[width][height];
    Complex point;      //the user selected point


    public mandFrame(String title){
        super(title);
        for(int y = 0; y < height; y++){
            for(int x = 0; x < width; x++){
                dot[x][y] = toComplex(x, y);
            }
        }
    }
    void init(){
        JPanel panel1 = new JPanel();
        panel1.setLayout(new BorderLayout());
        JPanel tfPanel = new JPanel();
        tfPanel.setLayout(new FlowLayout());
        mandPanel mPanel = new mandPanel();         //show the Mandelbrot Set

        final JTextField range = new JTextField(20);        //show range of complex plane displayed
        range.setEditable(false);
        range.setText(min.real + ";" + max.real + "," + min.imaginary + ";" + max.imaginary);
        final JTextField change = new JTextField(20);       //for user to change range
        JButton set = new JButton("Set");
        set.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                String s = change.getText();
                String[] parse = s.split("[;,]");
                min.real = Double.parseDouble(parse[0]);
                max.real = Double.parseDouble(parse[1]);
                min.imaginary = Double.parseDouble(parse[2]);
                max.imaginary = Double.parseDouble(parse[3]);
                range.setText(min.real + ";" + max.real + "," + min.imaginary + ";" + max.imaginary);
            }
        });
        final JTextField iter = new JTextField(10);     //let user set number of iterations
        JButton ok = new JButton("OK");             //implement number of iterations set by user
        ok.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                iteration = Integer.valueOf(iter.getText());
            }
        });

        tfPanel.add(range);
        tfPanel.add(change);
        tfPanel.add(set);
        tfPanel.add(iter);
        tfPanel.add(ok);

        mPanel.addMouseListener(new MouseListener());
        mPanel.addMouseMotionListener(new MouseListener());
        panel1.add(tfPanel, BorderLayout.NORTH);
        panel1.add(mPanel, BorderLayout.CENTER);
        setContentPane(panel1);

        setSize(width, height);
        setResizable(false);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    class mandPanel extends JPanel{

        public void paintComponent(Graphics g){
            super.paintComponent(g);
            for(int y = 0; y < height; y++){
                for(int x = 0; x < width; x++){
                    int it = ma(dot[x][y]);
                    g.setColor(color(it));
                    g.fillRect(x, y, 1, 1);
                }
            }
        }
    }

    Complex toComplex(int x, int y){
        Complex z = new Complex();
        z.real = min.real + ((x/width) * (max.real - min.real));    //compute               the real part of the point
        z.imaginary = max.imaginary - ((y/height) * (max.imaginary - min.imaginary));   //compute the imaginary part of the point
        return z;

    }

    public int ma(Complex z0){
        Complex z = z0;
        int it = 0;
        while((int)z.modulusSquared() < 4 && it < iteration){
            z = z.square();
            z = z.add(z0);
            it = it + 1;
        }
        return it;
    }
    public Color color(int c){
        if(c == 255){
            return Color.BLACK;
        }else{
            return new Color(0, 0, (c*3) % 254);
        }

    }
}
Was it helpful?

Solution 2

Maybe it's because of integer division.

Complex toComplex(int x, int y)
{
    Complex z = new Complex();
    z.real = min.real + ((x/width) * (max.real - min.real));
    z.imaginary = max.imaginary - ((y/height) * (max.imaginary - min.imaginary));
    return z;
}

Here, (x/width) and (y/height) return 0 always, since it's integer division and width and height are values greater than x or y. So, I'd use double instead of int.

Complex toComplex(double x, double y)
{
    ...
}

Fix any problems that raise because of the change and see what happens.

OTHER TIPS

Probably

z.real = min.real + ((x/width) * (max.real - min.real));

x and width are int not double, try a cast to double

z.real = min.real + (( (double)x/(double)width) * (max.real - min.real));

You should also use Apache complex

you can have a look to another implementation that I did here with javaFX

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top