Mandelbrot设置GUI java

时间:2014-03-11 19:24:43

标签: java mandelbrot

嗨,所以我写了这些代码来展示一个mandelbrot集,但每次我运行它时,它只显示黑色。我一直在试图看看我的错误在哪里但未能找到它们所以如果你们可以向我展示它们会真的有帮助。

以下是数字类的代码:

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);
    }

}

这就是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);
        }

    }
}

2 个答案:

答案 0 :(得分:1)

可能

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

x和width是int而不是double,尝试使用强制转换来加倍

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

您还应该使用Apache complex

你可以看看我用javaFX

here的另一个实现

答案 1 :(得分:0)

也许是因为整数除法。

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;
}

此处,(x/width)(y/height)始终返回0,因为它是整数除法,widthheight的值大于xy。因此,我使用double代替int

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

修复由于更改引起的任何问题,看看会发生什么。