gpt4 book ai didi

java - Mandelbrot集GUI java

转载 作者:行者123 更新时间:2023-12-01 13:17:43 27 4
gpt4 key购买 nike

嗨,我编写了这些代码来显示曼德尔布罗集,但每次运行它时,它只显示黑色。我一直在尝试查看我的错误在哪里,但未能找到它们,因此如果你们能向我展示它们,那将会非常有帮助。

这是数字类的代码:

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

}
}

最佳答案

可能

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

x 和 width 是 int,不是 double,尝试强制转换为 double

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

您还应该使用Apache complex

你可以看看我做的另一个实现here使用javaFX

关于java - Mandelbrot集GUI java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22334652/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com