64 lines
2.1 KiB
Java
64 lines
2.1 KiB
Java
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.util.stream.IntStream;
|
|
|
|
public class JuliaSet extends JPanel {
|
|
private static final int MAX_ITERATIONS = 300;
|
|
private static final double ZOOM = 1;
|
|
private static final double CX = -0.7;
|
|
private static final double CY = 0.27015;
|
|
private static final double MOVE_X = 0;
|
|
private static final double MOVE_Y = 0;
|
|
|
|
public JuliaSet() {
|
|
setPreferredSize(new Dimension(800, 600));
|
|
setBackground(Color.white);
|
|
}
|
|
|
|
void drawJuliaSet(Graphics2D g) {
|
|
int w = getWidth();
|
|
int h = getHeight();
|
|
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
|
|
|
|
IntStream.range(0, w).parallel().forEach(x -> {
|
|
IntStream.range(0, h).parallel().forEach(y -> {
|
|
double zx = 1.5 * (x - w / 2) / (0.5 * ZOOM * w) + MOVE_X;
|
|
double zy = (y - h / 2) / (0.5 * ZOOM * h) + MOVE_Y;
|
|
float i = MAX_ITERATIONS;
|
|
while (zx * zx + zy * zy < 4 && i > 0) {
|
|
double tmp = zx * zx - zy * zy + CX;
|
|
zy = 2.0 * zx * zy + CY;
|
|
zx = tmp;
|
|
i--;
|
|
}
|
|
int c = Color.HSBtoRGB((MAX_ITERATIONS / i) % 1, 1, i > 0 ? 1 : 0);
|
|
image.setRGB(x, y, c);
|
|
});
|
|
});
|
|
g.drawImage(image, 0, 0, null);
|
|
}
|
|
|
|
@Override
|
|
public void paintComponent(Graphics gg) {
|
|
super.paintComponent(gg);
|
|
Graphics2D g = (Graphics2D) gg;
|
|
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
|
RenderingHints.VALUE_ANTIALIAS_ON);
|
|
drawJuliaSet(g);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
SwingUtilities.invokeLater(() -> {
|
|
JFrame f = new JFrame();
|
|
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
f.setTitle("Julia Set");
|
|
f.setResizable(false);
|
|
f.add(new JuliaSet(), BorderLayout.CENTER);
|
|
f.pack();
|
|
f.setLocationRelativeTo(null);
|
|
f.setVisible(true);
|
|
});
|
|
}
|
|
}
|