如何在Mandelbrot上进行流体变焦?

时间:2015-11-08 15:36:50

标签: java swing

我正在尝试在我的Mandelbrot设备上实现自动缩放,但它看起来非常滞后,我无法弄清楚如何解决它。 plotPoints函数绘制Mandelbrot,并在构造函数中每10毫秒调用一次。这是我的代码:

import java.awt.Graphics;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

public class MendelbrotMain extends JFrame {

    private final int MAX_ITER = 570;
    private double ZOOM = 150;
    private BufferedImage I;
    private double zx, zy, j, k, tmp;

    public MendelbrotMain() {
        super("Mandelbrot Set");
        setBounds(100, 100, 800, 600);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        I = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
        plotPoints();
        Timer t = new Timer(10, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                plotPoints();
                ZOOM+=100;
            }
        });
        t.setRepeats(true);
        t.start();
    }

    public void plotPoints(){
        for (int y = 0; y < getHeight(); y++) {
            for (int x = 0; x < getWidth(); x++) {
                zx = zy = 0;
                int iter = MAX_ITER;
                j = (x - 400) / ZOOM;
                k = (y - 300) / ZOOM;
                while (zx * zx + zy * zy < 4 && iter > 0) {
                    tmp = zx * zx - zy * zy + j;
                    zy = 2.0 * zx * zy + k;
                    zx = tmp;
                    iter--;
                }
                I.setRGB(x, y, iter | (iter << 8));
            }
        }
        validate();
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(I, 0, 0, this);
    }

    public static void main(String[] args) {
        new MendelbrotMain().setVisible(true);
    }
}

0 个答案:

没有答案