[英]How do you draw the Mandelbrot Set in Java using SWING/AWT?
我正在嘗試繪制 Mandelbrot 集合,集合中的點為黑色,其他所有點為白色。 在這個初始版本中,我不希望能夠放大,而只是創建一個 static 圖像。
我創建了一個 ComplexNumber class,如下所示,用於處理復數的平方和相加。
public class ComplexNumber {
private double real;
private double imaginary;
public ComplexNumber(double real, double imaginary){
this.real = real;
this.imaginary = imaginary;
}
public ComplexNumber times(ComplexNumber number){
double a = this.real*number.real;
double b = this.imaginary*number.real;
double c = this.real*number.imaginary;
double d = this.imaginary*number.imaginary*-1;
double newReal = a+d;
double newImaginary = b+c;
ComplexNumber newComplexNumber = new ComplexNumber(newReal, newImaginary);
return newComplexNumber;
}
public ComplexNumber add(ComplexNumber number){
double newReal = this.real+number.real;
double newImaginary = this.imaginary+number.imaginary;
return new ComplexNumber(newReal, newImaginary);
}
public double abs(){
return Math.hypot(this.real, this.imaginary);
}
public double getReal() {
return real;
}
public double getImaginary() {
return imaginary;
}
}
這是我渲染 GUI 並實際計算 Mandelbrot 集中點的代碼。
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class MandelBrotSet extends JComponent {
public static final int WIDTH = 800;
public static final int HEIGHT = 800;
public static final int ITERATIONS = 100;
public static final double startX = -2;
public static final double width = 4;
public static final double startY = 2;
public static final double height = 4;
public static final double dx = width/(WIDTH-1);
public static final double dy = height/(HEIGHT-1);
private BufferedImage buffer;
public MandelBrotSet() {
buffer = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
JFrame frame = new JFrame("Mandelbrot Set");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.getContentPane().add(this);
frame.pack();
frame.setVisible(true);
}
@Override
public void addNotify() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
}
@Override
public void paint(Graphics g) {
g.drawImage(buffer, 0, 0, null);
}
public void render(){
for (int x=0; x<WIDTH; x++){
for (int y=0; y<HEIGHT; y++){
int color = calculatePoint(x, y);
buffer.setRGB(x, y, color);
}
}
}
public int calculatePoint(int x, int y){
ComplexNumber number = convertToComplex(x, y);
ComplexNumber z = number;
int i;
for (i=0; i<ITERATIONS; i++){
z = z.times(z).add(number);
if (z.abs()>2.0){
break;
}
}
if (i==ITERATIONS) {
return 0x00000000;
}
else {
return 0xFFFFFFFF;
}
}
public static ComplexNumber convertToComplex(int x, int y){
double real = startX + x*dx;
double imaginary = 2 - y*dy;
return new ComplexNumber(real, imaginary);
}
public static void main(String[] args) {
MandelBrotSet mandy = new MandelBrotSet();
mandy.render();
}
}
運行此代碼后,我得到下面的圖像。 似乎可以瞥見曼德布羅布景,但隨后被大量黑色遮住了。 我究竟做錯了什么?
下面更新了解決方案。 謝謝您的幫助。
圖像的渲染比顯示MandelBrotSet
-Object 花費的時間更長,並且之后永遠不會更新。 您創建一個 MandelBrotSet 類型的MandelBrotSet
,然后立即調用它的渲染方法。 在創建該 object 期間,您將其放入您立即顯示的JFrame
中。 顯示幀時,渲染不完整,這就是您的ImageBuffer
尚未填充的原因(構建 mandelbrot-form 需要更長的時間)。
要解決此問題,您可以重新繪制相關組件和框架。 我對 awt 的重繪函數的理解不足以告訴你如何正確地做到這一點(也許其他人可以在那里提供幫助),但是將它添加到你的渲染方法的末尾應該會有所幫助:
revalidate();
repaint();
可以省略重新驗證或重新繪制。
會很酷,如果你用完成的圖像更新你的問題:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.