-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJuliaSetProgram.java
More file actions
224 lines (185 loc) · 5.74 KB
/
JuliaSetProgram.java
File metadata and controls
224 lines (185 loc) · 5.74 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class JuliaSetProgram extends JPanel implements AdjustmentListener, ActionListener
{
JFrame frame;
int w, h, pixels;
double a, b, zoom, dx, dy;
JScrollBar zoomBar, aBar, bBar, iBar, hueBar, adjustX, adjustY;
JPanel scrollPanel, boxPanel, southPanel, southEastPanel, southWestPanel;
JLabel[] labels;
float maxIter, hueAdjust;
JToggleButton pixelation;
BufferedImage image;
public JuliaSetProgram()
{
frame = new JFrame("Julia Set Program");
frame.add(this);
frame.setSize(1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
labels = new JLabel[8];
for(int i = 0; i < 8; i++)
{
labels[i] = new JLabel();
}
w = 1000; h = 700;
a = -0.7;
b = 0.27015;
a = 0;
b = 0;
zoom = 1;
maxIter = 100;
hueAdjust = 1;
pixels = 1;
dx = 0;
dy = 0;
scrollPanel = new JPanel();
boxPanel = new JPanel();
scrollPanel.setLayout(new GridLayout(4, 2));
zoomBar = new JScrollBar(JScrollBar.HORIZONTAL, 50, 0, 1, 200);
aBar = new JScrollBar(JScrollBar.HORIZONTAL, 3000, 0, 0, 6000);
bBar = new JScrollBar(JScrollBar.HORIZONTAL, 3000, 0, 0, 6000);
iBar = new JScrollBar(JScrollBar.HORIZONTAL, 100, 0, 1, 1000);
hueBar = new JScrollBar(JScrollBar.HORIZONTAL, 1, 0, 0, 100);
adjustX = new JScrollBar(JScrollBar.HORIZONTAL, 50, 0, 0, 100);
adjustY = new JScrollBar(JScrollBar.HORIZONTAL, 50, 0, 0, 100);
pixelation = new JRadioButton();
zoomBar.addAdjustmentListener(this);
aBar.addAdjustmentListener(this);
bBar.addAdjustmentListener(this);
iBar.addAdjustmentListener(this);
hueBar.addAdjustmentListener(this);
adjustX.addAdjustmentListener(this);
adjustY.addAdjustmentListener(this);
pixelation.addActionListener(this);
scrollPanel.add(aBar);
scrollPanel.add(zoomBar);
scrollPanel.add(bBar);
scrollPanel.add(iBar);
scrollPanel.add(adjustX);
scrollPanel.add(hueBar);
scrollPanel.add(adjustY);
boxPanel.add(pixelation);
scrollPanel.add(boxPanel);
southPanel = new JPanel();
southWestPanel = new JPanel();
southWestPanel.setLayout(new GridLayout(4, 1));
labels[0].setText("A: "+a);
southWestPanel.add(labels[0]);
labels[1].setText("B: "+b);
southWestPanel.add(labels[1]);
labels[2].setText("DX: "+dx);
southWestPanel.add(labels[2]);
labels[3].setText("DY: "+dy);
southWestPanel.add(labels[3]);
southEastPanel = new JPanel();
southEastPanel.setLayout(new GridLayout(4, 1));
labels[4].setText("ZOOM: "+zoom);
southEastPanel.add(labels[4]);
labels[5].setText("ITERATIONS: "+maxIter);
southEastPanel.add(labels[5]);
labels[6].setText("HUE: "+hueAdjust);
southEastPanel.add(labels[6]);
labels[7].setText("PIXEL: "+pixelation.isSelected());
southEastPanel.add(labels[7]);
southPanel.setLayout(new BorderLayout());
southPanel.add(southWestPanel, BorderLayout.WEST);
southPanel.add(southEastPanel, BorderLayout.EAST);
southPanel.add(scrollPanel, BorderLayout.CENTER);
//southPanel.add(boxPanel, BorderLayout.EAST);
frame.add(southPanel, BorderLayout.SOUTH);
setBackground(Color.white);
frame.setVisible(true); // should be last thing
}
public BufferedImage generateImage()
{
double zx, zy;
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < w; x+=pixels)
{
for (int y = 0; y < h; y+=pixels)
{
zx = 1.5 * (x - w / 2) / (0.5 * zoom * w)+dx;
zy = (y - h / 2) / (0.5 * zoom * h)+dy;
float i = maxIter;
while(zx*zx+zy*zy < 6 && i > 0)
{
double diff = (zx*zx)-(zy*zy)+a; //plus complex number value
zy = 2*zx*zy+b;
zx = diff;
i--;
}
int c;
if(i>0)
c = Color.HSBtoRGB(hueAdjust*(maxIter / i) % 1, 1, 1);
else c = Color.HSBtoRGB((maxIter / i), 1, 0);
image.setRGB(x, y, c);
}
}
return image;
}
public void actionPerformed(ActionEvent e)
{
if(pixelation.isSelected())
pixels = 5;
else
pixels = 1;
repaint();
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
if(e.getSource() == zoomBar)
{
zoom = (0.99/49)*zoomBar.getValue();
dy = ((adjustY.getValue()*-1 / 100.0)+.5)*(1/zoom);
dx = ((adjustX.getValue()*-1 / 100.0)+.5)*(1/zoom);
//dx/=zoomBar.getValue();
}
if(e.getSource() == aBar)
{
a = (aBar.getValue()/1000.0) - 3.0;
}
if(e.getSource() == bBar)
{
b = (bBar.getValue()/1000.0) - 3.0;
}
if(e.getSource() == iBar)
{
maxIter = iBar.getValue();
}
if(e.getSource() == hueBar)
{
hueAdjust = hueBar.getValue()/100.0f;
}
if(e.getSource() == adjustX)
{
dx = ((adjustX.getValue()*-1 / 100.0)+.5)*(1/zoom);
}
if(e.getSource() == adjustY)
{
dy = ((adjustY.getValue()*-1 / 100.0)+.5)*(1/zoom);
}
labels[0].setText("A: "+(double)Math.round(a * 1000d) / 1000d);
labels[1].setText("B: "+(double)Math.round(b * 1000d) / 1000d);
labels[2].setText("DX: "+(double)Math.round(dx * 1000d) / 1000d);
labels[3].setText("DY: "+(double)Math.round(dy * 1000d) / 1000d);
labels[4].setText("ZOOM: "+(double)Math.round(zoom * 1000d) / 1000d);
labels[5].setText("ITERATIONS: "+maxIter);
labels[6].setText("HUE: "+hueAdjust);
labels[7].setText("PIXEL: "+pixelation.isSelected());
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g); //GIANT ERASER, Wipes out everything and redraws everything
//g.setColor(new Color(redValue, greenValue, blueValue));
//g.fillRect(0, 0, frame.getWidth(), frame.getHeight());
g.drawImage(generateImage(),0,0,null);
}
public static void main(String[]args)
{
JuliaSetProgram app = new JuliaSetProgram();
}
}