使用BufferedImage绘制Mandelbrot集,只获得纯色

时间:2016-04-05 00:58:27

标签: java bufferedimage paintcomponent fractals mandelbrot

我正在编写一个Java程序来为我的入门编程类显示Mandelbrot集。我相信我已经正确设置了所有数学,但是当我尝试绘制分形时,我最终得到的是纯色。我已经测试了数学,看起来它应该正常工作。我搜索了一个多小时,但我找不到任何有帮助的东西。下面是我的复数类和实际创建Mandelbrot集: 复数

public class ComplexNum {
//Instance Fields
private double realNum; //the real number portion of the complex number
private double imgNum; //the imaginary number portion of the complex number

//NOTE TO SELF: i = sqrt(-1); i^2 = -1; i^3 = -i; i^4 = 1; then the cycle starts over.

//Constructor
/**Creates a complex number of form x+yi, where x and y are both of type double; x represents the real number piece of the
 * complex number, while y represents the imaginary piece.
 * @param realPart -- the double value which is the real piece of the complex number
 * (Precondition: realPart is a real number of type double)
 * @param imaginaryPart -- the double value which represents the imaginary piece of the complex number
 * (Precondition: imaginaryPart is a real number of type double)
 */
public ComplexNum(double realPart, double imaginaryPart){
    realNum = realPart;
    imgNum = imaginaryPart;
}

/**Add two complex numbers by taking the sum of their real and imaginary pieces.
 * (Postcondition: returns the sum of two complex numbers)
 * @param comNum -- the complex number that is to be added together with this one
 * (Precondition: both the complex number you are calling this method on and comNum must have been initialized)
 * @return the sum of two complex numbers
 */
public ComplexNum add(ComplexNum comNum){
    return new ComplexNum(realNum+comNum.getRealPart(), imgNum+comNum.getImgPart());
}

/**Square the complex number and returns the result.
 * (Precondition: the complex number must have been initialized)
 * @return the squared value of the complex number
 */
public ComplexNum squareComplex(){
    double realPiece = realNum*realNum; //this is a normal number
    double imaginaryPiece = (realNum*imgNum)+(imgNum*realNum); //each section in parenthesis has an i attached to it, allowing both sections to be added together
    double iSquaredPiece = imgNum*imgNum; //this now has an i^2

    //The form that the complex number currently: a + b(i) + c(i^2), where b is actually x(i)+y(i) simplified.
    //since i^2 is -1, the iSquaredPiece is actually a real number. Multiply the value by -1, then add it to a,
    //and the true real number piece of the complex number is created.
    realPiece = realPiece + (iSquaredPiece*-1);

    return new ComplexNum(realPiece, imaginaryPiece);
}

/**Allows the real piece of a complex number to be extracted.
 * (Precondition: the complex number must have been initialized)
 * @return the value of the real number piece of the complex number
 */
public double getRealPart(){
    return realNum;
}

/**Allows the imaginary piece of a complex number to be extracted.
 * (Precondition: the complex number must have been initialized)
 * @return the value of the imaginary number piece of the complex number
 */
public double getImgPart(){
    return imgNum;
}

曼德尔布罗

public class MandelbrotGenerator {
//Constants
/**The maximum number of times the Mandelbrot calculations will be run on a specific point. If the real and imaginary pieces
 * from each calculation don't exceed 2 within the maximum number of iterations, they are part of the Mandelbrot set.
 */
public static final int MAX_ITERATIONS = 30; //The maximum number of times the calculations will be run on a specific point.
private final double MIN_X = -2.0; //The minimum value of x when graphing the Mandelbrot set
private final double MAX_Y = 2.0; //The maximum value of y when graphing the Mandelbrot set
private final double MANDEL_X_RANGE = 4.0; //The range of x values from -2 to 2 when graphing the Mandelbrot set
private final double MANDEL_Y_RANGE = 4.0; //The range of y values from -2 to 2 when graphing the Mandelbrot set

//Instance Fields
private ComplexNum z; //In the Mandelbrot equation of Z_(n+1)=Z_n^2+C, this is the value of Z_n^2
private ComplexNum c; //In the Mandelbrot equation of Z_(n+1)=Z_n^2+C, this is the value of C
private ComplexNum currentCalc; //In the Mandelbrot equation of Z_(n+1)=Z_n^2+C, this is the value of Z_(n+1)
private int numIterations; //The current number of iterations

//Constructor
/**Create a MandelbrotGenerator object.
 */
public MandelbrotGenerator(){
    z = new ComplexNum(0,0);
    c = new ComplexNum(0,0);
    currentCalc = new ComplexNum(0,0);
    numIterations = 0;
}

//Methods
/**Carry out the Mandelbrot calculation on the point at the (x,y) coordinates specified by the parameters. The return value specifies
 * whether or not this point is within the Mandelbrot set, which is determined by whether or not the values of the real and imaginary
 * pieces of currentCalc, or Z_(n+1) from the Mandelbrot equation, both reach or exceed the value of 2 within a number of iterations
 * less than or equal to MAX_ITERATIONS.
 * (Postcondition: the program will return an int value which can be used to determine whether the input point is within the Mandelbrot set)
 * @param xVal -- the double value of the desired x coordinate
 * (Precondition: xVal is a real number)
 * @param yVal -- the double value of the desired y coordinate
 * (Precondition: yVal is a real number)
 * @return returns the number of iterations needed to meet or exceed the 2 threshold, or the value of MAX_ITERATIONS if the threshold is never met
 */
public int calculateMandelbrot(double xVal, double yVal, double panelWidth, double panelHeight){
    double xCord = convertToMandelX(xVal, panelWidth);
    double yCord = convertToMandelY(yVal, panelHeight);
    c = new ComplexNum(xCord,-yCord);
    for(int iterations = 0; iterations <= MAX_ITERATIONS && Math.abs(currentCalc.getRealPart())+Math.abs(currentCalc.getImgPart())<=4.0; iterations ++){
        numIterations = iterations;
        z = currentCalc;
        currentCalc = z.squareComplex().add(c);

    }
    return numIterations;

}
//I haven't properly commented the two methods below yet, but these
//are used to convert the coordinates of the pixel I'm testing into
//a point on the coordinate plane with x from -2 to 2 and y from
//-2i to 2i, which the Mandelbrot set is within.
//xPixLoc and yPixLoc are the (x,y) coordinates of the pixels from the
//frame, and maxXVal and maxYVal are the (x,y) dimensions of the frame,
//400 in my case. 
public double convertToMandelX(double xPixLoc, double maxXVal){
    double xCoordinate = MIN_X + ((xPixLoc/maxXVal)*MANDEL_X_RANGE);
    return xCoordinate;
}

public double convertToMandelY(double yPixLoc, double maxYVal){
    double yCoordinate = MAX_Y -((yPixLoc/maxYVal)*MANDEL_Y_RANGE);
    return yCoordinate;
}

我已经完成了一些JUnit测试,上述两个类似乎都能正常工作。我的测试中可能存在导致疏忽的缺陷,但我无法区分。在我看来,我的问题是我实际创建的图像,这是下面的类:

VisualComponent(我试图让它首先只使用两种颜色)

public class VisualComponent extends JComponent{
private static final long serialVersionUID = 1L;

//Constants
public static final int DEFAULT_ZOOM_CHANGE = 10;

//Instance Fields
int pnlWidth, pnlHeight; //The width and height of the panel the image will be painted into
BufferedImage fractalImg;
boolean updateImage;

//Constructor
public VisualComponent(int panelWidth, int panelHeight){
    pnlWidth=panelWidth;
    pnlHeight=panelHeight;
    fractalImg = new BufferedImage(panelWidth, panelHeight, BufferedImage.TYPE_INT_RGB);
    updateImage = true;
    //also initialize a default color pallet
}

//Methods
public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    if(updateImage){
        generateMandelbrot();
        updateImage=false;
    }
    g2.drawImage(fractalImg,0,0,this);

}

public void generateMandelbrot(){
    MandelbrotGenerator genImg = new MandelbrotGenerator();
    int iterations=0;
    for(int x=0; x<pnlWidth;x++){
        for(int y=0; y<pnlHeight;y++){
            iterations = genImg.calculateMandelbrot((double)x, (double)y, pnlWidth, pnlHeight);
            System.out.print(iterations);
            if(iterations == MandelbrotGenerator.MAX_ITERATIONS){
                fractalImg.setRGB(x, y, Color.BLACK.getRGB());
            } else {
                fractalImg.setRGB(x, y, Color.WHITE.getRGB());
            }
        }
    }
}

这是我的主要方法:

public class MainTester {
public static void main(String[] args){
    JFrame frame=new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,400);
    frame.setResizable(false);
    VisualComponent comp = new VisualComponent(400,400);
    frame.add(comp);
    frame.setVisible(true);
}
}

我真的很难过。似乎正在发生的事情是,当我调用calculateMandelbrot()时,返回始终是相同的。然而在我的测试中,我发现事实并非如此。我没有多少使用BufferedImages的经验,所以也许我使用它的方式存在缺陷?

由于我已经提到过这么多,所以这里也是我使用的测试代码。我知道这不是正确的形式,或者至少不是我教授所教导的形式,但我真的只是专注于试图找到问题。

public class ComplexNumTest {

@Test
public void testToString() {
    ComplexNum num = new ComplexNum(5,7);
    String res = num.toString();
    assertEquals("failed toString()", "5.0+7.0i", res);
}

@Test
public void testAdd(){
    ComplexNum num = new ComplexNum(5,7);
    ComplexNum num2 = new ComplexNum(5,3);
    ComplexNum num3 = num.add(num2);
    String res = num3.toString();
    assertEquals("failed add()", "10.0+10.0i", res);
    ComplexNum num4 = new ComplexNum(5,-7);
    ComplexNum num5 = new ComplexNum(-3,4);
    ComplexNum num6 = num4.add(num5);
    String res2 = num6.toString();
    assertEquals("failed add()", "2.0+-3.0i", res2);
}

@Test
public void testSquareComplex(){
    ComplexNum num = new ComplexNum(2,2);
    ComplexNum num2 = num.squareComplex();
    String res = num2.toString();
    assertEquals("failed squareComplex()", "0.0+8.0i", res);
    ComplexNum num3 = new ComplexNum(2,-2);
    ComplexNum num4 = num3.squareComplex();
    String res2 = num4.toString();
    assertEquals("failed squareComplex()", "0.0+-8.0i", res2);
    ComplexNum num5 = new ComplexNum(-1,0.5);
    ComplexNum num6 = num5.squareComplex();
    String res3 = num6.toString();
    assertEquals("failed squareComplex()", "0.75+-1.0i", res3);
}

@Test
public void testCalculations(){
    ComplexNum z = new ComplexNum(0,0);
    ComplexNum y = new ComplexNum(-1,0.5);
    ComplexNum a = z.squareComplex().add(y);
    String res = a.toString();
    assertEquals("failed calculations", "-1.0+0.5i", res);
    z = a;
    a = z.squareComplex().add(y);
    res = a.toString();
    assertEquals("failed squareComplex()", "-0.25+-0.5i", res);
}

@Test
public void getNums(){
    ComplexNum z = new ComplexNum(1,3);
    ComplexNum a = new ComplexNum(2,4);
    double y = z.getRealPart()+a.getRealPart();
    String num=y+"";
    assertEquals("failed getRealPart()", "3.0", num);
    y = z.getImgPart()+a.getImgPart();
    num=y+"";
    assertEquals("failed getRealPart()", "7.0", num);
}

@Test
public void testConvertToMandel(){
    MandelbrotGenerator a = new MandelbrotGenerator();
    double check = a.convertToMandelX(200, 400);
    String res = check+"";
    assertEquals("fail", "0.0", res);
    check = a.calculateMandelbrot(200, 200, 400, 400);
    res=check+"";
    assertEquals("fail", "30.0", res);
    boolean working=false;
    if(check==MandelbrotGenerator.MAX_ITERATIONS){
        working=true;
    }
    assertEquals("fail",true,working);
}
}

我希望这不是太多的代码可以一次性抛出这里。非常感谢你的帮助!

2 个答案:

答案 0 :(得分:2)

你的问题是你没有重新初始化z点,所以计算结果如你所说:

public int calculateMandelbrot(double xVal, double yVal, double panelWidth, double panelHeight){
    z = new ComplexNum(0,0);
    c = new ComplexNum(0,0);
    currentCalc = new ComplexNum(0,0);
    double xCord = convertToMandelX(xVal, panelWidth);
    double yCord = convertToMandelY(yVal, panelHeight);
    c = new ComplexNum(xCord,-yCord);
    for(int iterations = 0; iterations <= MAX_ITERATIONS && Math.abs(currentCalc.getRealPart())+ Math.abs(currentCalc.getImgPart())<=4; iterations ++){
        numIterations = iterations;
        z = currentCalc;
        currentCalc = z.squareComplex().add(c);
//   System.out.println(currentCalc.getRealPart()+" "+currentCalc.getImgPart());

    }
    return numIterations;

}

答案 1 :(得分:1)

我设法让这个工作。正如Hovercraft Full of Eels建议的那样,我从paintComponent()方法中删除了计算。相反,我让它们在main方法中完成,将值存储到Weather Vane建议的2D数组中,并修改我的VisualComponent类以在调用构造函数时将数组作为参数接收。计算本身也有缺陷,我对它们的初步信心是错误的。我对如何构造逃逸条件有一个误解,因为我没有意识到我应该对实部和虚部进行平方,然后将它们加起来并与4进行比较。我也不需要考虑它们的绝对值全部(因为平方值将确保它是正数)。最后,我没有在每次调用方法时初始化复数,这是gpasch指出的错误。这背后有一个原因,但事后看来我完全是愚蠢的,并认为会有多个MandelbrotGenerator对象,每个对象只会调用一次。是的,我非常困惑。我的工作代码如下:

Mandelbrot(我完全重组了进行计算的方法):

    //Constants
/**The maximum number of times the Mandelbrot calculations will be run on a specific point. If the real and imaginary pieces
 * from each calculation don't exceed 2 within the maximum number of iterations, they are part of the Mandelbrot set.
 */
public static final int MAX_ITERATIONS = 30; //The maximum number of times the calculations will be run on a specific point.
private final double MIN_X = -2.0; //The minimum value of x when graphing the Mandelbrot set
private final double MAX_Y = 2.0; //The maximum value of y when graphing the Mandelbrot set
private final double MANDEL_X_RANGE = 4.0; //The range of x values from -2 to 2 when graphing the Mandelbrot set
private final double MANDEL_Y_RANGE = 4.0; //The range of y values from -2 to 2 when graphing the Mandelbrot set

//Instance Fields
private ComplexNum z; //In the Mandelbrot equation of Z_(n+1)=Z_n^2+C, this is the value of Z_n^2
private ComplexNum c; //In the Mandelbrot equation of Z_(n+1)=Z_n^2+C, this is the value of C
private ComplexNum currentCalc; //In the Mandelbrot equation of Z_(n+1)=Z_n^2+C, this is the value of Z_(n+1)
private int numIterations; //The current number of iterations

//Constructor
/**Create a MandelbrotGenerator object.
 */
public MandelbrotGenerator(){
    z = new ComplexNum(0,0);
    c = new ComplexNum(0,0);
    currentCalc = new ComplexNum(0,0);
    numIterations = 0;
}

//Methods
/**Carry out the Mandelbrot calculation on the point at the (x,y) coordinates specified by the parameters. The return value specifies
 * whether or not this point is within the Mandelbrot set, which is determined by whether or not the values of the real and imaginary
 * pieces of currentCalc, or Z_(n+1) from the Mandelbrot equation, both reach or exceed the value of 2 within a number of iterations
 * less than or equal to MAX_ITERATIONS.
 * (Postcondition: the program will return an int value which can be used to determine whether the input point is within the Mandelbrot set)
 * @param xVal -- the double value of the desired x coordinate
 * (Precondition: xVal is a real number)
 * @param yVal -- the double value of the desired y coordinate
 * (Precondition: yVal is a real number)
 * @return returns the number of iterations needed to meet or exceed the 2 threshold, or the value of MAX_ITERATIONS if the threshold is never met
 */
public int calculateMandelbrot(double xVal, double yVal, double panelWidth, double panelHeight){
    double xCord = convertToMandelX(xVal, panelWidth);
    double yCord = convertToMandelY(yVal, panelHeight);
    c = new ComplexNum(xCord,-yCord);
    z = new ComplexNum(0,0);
    currentCalc = new ComplexNum(0,0);
    numIterations=0;
    while(numIterations<=MAX_ITERATIONS && Math.pow(currentCalc.getRealPart(),2)+Math.pow(currentCalc.getImgPart(),2)<=4.0){
        numIterations++;
        z = currentCalc;
        currentCalc = z.squareComplex();
        currentCalc = currentCalc.add(c);
    }
    return numIterations;
}

public double convertToMandelX(double xPixLoc, double maxXVal){
    double xCoordinate = MIN_X + ((xPixLoc/maxXVal)*MANDEL_X_RANGE);
    return xCoordinate;
}

public double convertToMandelY(double yPixLoc, double maxYVal){
    double yCoordinate = MAX_Y -((yPixLoc/maxYVal)*MANDEL_Y_RANGE);
    return yCoordinate;
}

主要

public class MainTester {
public static void main(String[] args){
    JFrame frame=new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,400);
    frame.setResizable(false);
    MandelbrotGenerator genImg = new MandelbrotGenerator();
    int[][] list = new int[400][400];
    int iterations=0;
    for(int x=0; x<400;x++){
        for(int y=0; y<400;y++){
            iterations = genImg.calculateMandelbrot((double)x, (double)y, 400, 400);
            list[x][y]=iterations;
            //System.out.println(list[x][y]);
        }
    }
    VisualComponent comp = new VisualComponent(400,400, list);
    frame.add(comp);
    frame.setVisible(true);
}
}

VisualComponent(我当前的颜色选择是任意的,只是我自己的实验)

public class VisualComponent extends JComponent{
private static final long serialVersionUID = 1L;

//Constants
public static final int DEFAULT_ZOOM_CHANGE = 10;

//Instance Fields
int pnlWidth, pnlHeight; //The width and height of the panel the image will be painted into
BufferedImage fractalImg;
boolean updateImage;
int[][] fList;

//Constructor
public VisualComponent(int panelWidth, int panelHeight, int[][] list){
    pnlWidth=panelWidth;
    pnlHeight=panelHeight;
    fractalImg = new BufferedImage(panelWidth, panelHeight, BufferedImage.TYPE_INT_ARGB);
    updateImage = true;
    fList=list;
    //also initialize a default color pallet
}

//Methods
public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    Color pixColor;
    for(int x = 0; x<400; x++){
        for(int y=0; y<400; y++){
            if(fList[x][y] >= MandelbrotGenerator.MAX_ITERATIONS){
                fractalImg.setRGB(x, y, Color.BLACK.getRGB());
            } else {
                if(fList[x][y]<=2){
                    pixColor= new Color((42+fList[x][y])%255,0,(80+fList[x][y])%255);
                }else if(fList[x][y]<=3){
                    pixColor= new Color((48+fList[x][y])%255,0,(90+fList[x][y])%255);
                }else {
                    pixColor=new Color((50+fList[x][y])%255,0,(100+fList[x][y])%255);
                }
                fractalImg.setRGB(x, y, pixColor.getRGB());
            }
        }
    }
    g2.drawImage(fractalImg,0,0,this);
}

我的复数类没有任何变化。显然我仍然需要让程序除了生成基本图像之外还要做一些事情,但是现在我已经把这一切都理顺了,我想我能够解决这个问题。再次感谢Hovercraft Full of Eels和Weather Vane的有用评论!

编辑:我在上面发布的代码中发现,有些实例我使用的是400而不是一个保持框架大小的变量。我已经解决了这个问题,只是想确保我意识到疏忽。 Here's an image of my result