How to resolve the algorithm Julia set step by step in the Java programming language
How to resolve the algorithm Julia set step by step in the Java programming language
Table of Contents
Problem Statement
Generate and draw a Julia set.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Julia set step by step in the Java programming language
Julia Set
The Julia set is a beautiful and complex fractal that can be generated using a simple mathematical formula. This Java program draws the Julia set using the Graphics
class and the BufferedImage
class.
How the Program Works
The program begins by creating a JuliaSet
object and adding it to a JFrame
. The JuliaSet
object extends the JPanel
class and overrides the paintComponent()
method. The paintComponent()
method is where the actual drawing of the Julia set takes place.
The drawJuliaSet()
method is called from within the paintComponent()
method. This method takes a Graphics2D
object as an argument and uses it to draw the Julia set.
The first step is to create a BufferedImage
object. The BufferedImage
object will be used to store the pixels of the Julia set.
Next, the program loops through each pixel in the BufferedImage
object. For each pixel, the program calculates the complex number c
that corresponds to that pixel. The complex number c
is then used to calculate the Julia set value for that pixel.
The Julia set value is a complex number that represents the number of iterations it takes for the complex number c
to escape from the Mandelbrot set. If the complex number c
does not escape from the Mandelbrot set, then the Julia set value is set to 0.
The program uses the Color.HSBtoRGB()
method to convert the Julia set value to a color. The color is then used to set the pixel in the BufferedImage
object.
Once all of the pixels in the BufferedImage
object have been set, the program draws the BufferedImage
object onto the Graphics2D
object.
Parallelization
The program parallelizes the drawing of the Julia set by using the IntStream.range()
and parallel()
methods. This means that the program can draw the Julia set much faster than it could if it were to draw the set one pixel at a time.
Conclusion
This Java program is a simple but effective way to draw the Julia set. The program uses the Graphics
class and the BufferedImage
class to create a beautiful and complex fractal image.
Source code in the java programming language
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
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);
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; 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);
});
}
}
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);
});
}
}
You may also check:How to resolve the algorithm Calendar step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Totient function step by step in the PL/M programming language
You may also check:How to resolve the algorithm Fork step by step in the Aikido programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the E programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the Argile programming language