-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFractal.java
156 lines (134 loc) · 5.65 KB
/
Fractal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* Author : Kisaru Liyanage
* Description : This is an abstract class used to create Fractals
* (should be extended by a sub class before using)
* Date : 04/09/2016
*/
import javax.swing.*;
import java.awt.*;
public abstract class Fractal extends JPanel{
private static final int WIDTH = 800;
private Region complexRegion;
protected int iterations;
protected int colorScheme[];
private Color colorsOfPoints[][] = new Color[WIDTH][WIDTH];
Fractal (double regionMinX, double regionMaxX, double regionMinY, double regionMaxY, int iterations) {
complexRegion = new Region(regionMinX, regionMaxX, regionMinY, regionMaxY);
this.iterations = iterations;
setColorScheme();
}
//this method computes the colors of all the points in the drawing canvas for the relevant set
protected void computeFractalColors() {
int numOfThreads = 4;
Thread threads[] = new ColorCalcThread[numOfThreads];
//calculating colors of points using threads
for (int i = 0; i < numOfThreads; i++) {
threads[i] = new ColorCalcThread(i, WIDTH / numOfThreads);
threads[i].start();
}
//joining all the threads (this is to make sure all the threads have finished their job before
//point colors are used by the drawing canvas to draw the fractal)
try {
for (int i = 0; i < numOfThreads; i++) {
threads[i].join();
}
} catch (Exception e) {
System.out.println("Error occurred when joining threads!");
}
}
//this is an inner class used to calculate colors of points using threads
private class ColorCalcThread extends Thread {
private int threadCount;
private int threadRange;
ColorCalcThread(int tCount, int tRange) {
threadCount = tCount;
threadRange = tRange;
}
@Override
public void run() {
Region drawingCanvas = new Region(0, WIDTH - 1, 0, WIDTH - 1);
Point point;
for (int i = threadCount * threadRange; i < (threadCount + 1) * threadRange; i++) {
for (int j = 0; j < WIDTH; j++) {
//mapping point of drawing canvas to complex plane and computing the color
//of the point
point = new Point(i, j);
point.map(drawingCanvas, complexRegion);
colorsOfPoints[i][j] = getPointColor(calculateDivergingN(point.toComplex()));
}
}
}
}
abstract int calculateDivergingN(Complex complexPoint);
abstract void setColorScheme();
//this method takes the iteration count at the scheme converges for a point, and returns the relevant color
//in the color scheme specified by setColorScheme
private Color getPointColor(int divergingN) {
Color color = null;
if (divergingN == iterations) { //point is in set
color = Color.black;
} else { //point is not in set. so assign a color in colorScheme based on the divergingN
for (int i = colorScheme.length; i > 0; i--) {
if (divergingN > (i - 1) * 5) {
color = new Color(colorScheme[i - 1]);
break;
}
}
}
return color;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//drawing the points on the canvas using the colors computed by computeFractalColors()
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < WIDTH; j++) {
g2.setColor(colorsOfPoints[i][j]);
//points have been mapped thinking that the bottom left corner of canvas is (0, 0)
//but in JPanel, origin is at top left corner
g2.drawLine(i, WIDTH - 1 - j, i, WIDTH - 1 - j);
}
}
}
public static void main(String[] args) {
Fractal fractal = null;
String set = args[0];
if (set.equals("Mandelbrot")) {
switch (args.length){
case 1:
fractal = new Mandelbrot();
break;
case 5:
fractal = new Mandelbrot(Double.parseDouble(args[1]), Double.parseDouble(args[2]), Double.parseDouble(args[3]), Double.parseDouble(args[4]));
break;
case 6:
fractal = new Mandelbrot(Double.parseDouble(args[1]), Double.parseDouble(args[2]), Double.parseDouble(args[3]), Double.parseDouble(args[4]), Integer.parseInt(args[5]));
break;
default:
System.out.println("Error: Inavlid input format!");
}
} else if (set.equals("Julia")) {
switch (args.length){
case 1:
fractal = new Julia();
break;
case 3:
fractal = new Julia(Double.parseDouble(args[1]), Double.parseDouble(args[2]));
break;
default:
System.out.println("Error: Inavlid input format!");
}
}
if (fractal != null) {
JFrame frame = new JFrame("Fractal");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setContentPane(fractal);
frame.setPreferredSize(new Dimension(WIDTH - 1, WIDTH - 1));
frame.setSize(WIDTH - 1, WIDTH - 1);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
}