-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractalimageswithnavigation.java
340 lines (229 loc) · 8.73 KB
/
fractalimageswithnavigation.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package fractalExplorer;
/**
* Fractal images
@author Pranav Kadam
**/
import java.awt.*;
import java.util.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
//---------------------x-----------------------x-------------------------x----------------x-----------------------------x--------------------------x
public class FractalExplorer extends JFrame {
static final int WIDTH = 600;
static final int HEIGHT =600;
//instance variables for width and height
Canvas canvas; //creates canvas
BufferedImage fractalImage; //creates something to 'draw' on the canvas
static final int MAX_ITER=100;
static final double DEFAULT_ZOOM= 100.0;
static final double DEFAULT_TOP_LEFT_X = -3.0;
static final double DEFAULT_TOP_LEFT_Y = +3.0;
double zoomFactor = DEFAULT_ZOOM;
double topLeftX = DEFAULT_TOP_LEFT_X;
double topLeftY = DEFAULT_TOP_LEFT_Y;
//---------------------x-----------------------x-------------------------x----------------x-----------------------------x--------------------------x
public FractalExplorer(){
setInitialGUIProperties();
addCanvas();
canvas.addKeyStrokeEvents();
updateFractal();
}
//---------------------x-----------------------x-------------------------x----------------x-----------------------------x--------------------------x
public static void main(String[] args){
new FractalExplorer();
}
//---------------------x-----------------------x-------------------------x----------------x-----------------------------x--------------------------x
private void addCanvas(){
canvas = new Canvas();
fractalImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
canvas.setVisible(true);
this.add(canvas,BorderLayout.CENTER);
}
//---------------------x-----------------------x-------------------------x----------------x-----------------------------x--------------------------x
public void setInitialGUIProperties(){
this.setTitle("Fractal Explorer"); //sets Title for GUI
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //defines how the window is closed. We chose 'x' button option
this.setSize(WIDTH,HEIGHT);
this.setResizable(false); //cannot be resized
this.setLocationRelativeTo(null); //GUI is placed in the center of the screen
this.setVisible(true);
}
private double getXPos(double x){
//transforms x co-ordinate into a point or rather a value on our complex plane (canvas)
return x/zoomFactor + topLeftX;
}
private double getYPos(double y){
return y/zoomFactor - topLeftY; //negative because y-axis is flipped
}
//the above methods return the values of c
//---------------------x-----------------------x-------------------------x----------------x-----------------------------x--------------------------x
public void updateFractal(){
//going to go through every single pixel on the canvas and change the color
//loops through pixels
for(int x=0; x<WIDTH; x++){
for(int y=0; y<HEIGHT; y++){
double c_r=getXPos(x); //real values are on the x-axis, imaginary y-axis
double c_i=getYPos(y);
int iterCount = computeIterations(c_r, c_i);
//returns number of iterations
//the next thing we need to do is create a color based on the number of iterations with make color method
int pixelColor = makeColor(iterCount);
fractalImage.setRGB(x,y,pixelColor);
}
}
canvas.repaint();
}
//---------------------x-----------------------x-------------------------x----------------x-----------------------------x--------------------------x
private int makeColor(int iterCount){
int color = 0b011011100001100101101000;
int mask = 0b000000000000010101110111;
int shiftMag = iterCount / 13;
if(iterCount==MAX_ITER){
return Color.BLACK.getRGB();
//return Color.BLUE.getRGB();
}return color | (mask << shiftMag);
}
//---------------------x-----------------------x-------------------------x----------------x-----------------------------x--------------------------x
private int computeIterations(double c_r, double c_i){
/*
Let c=c_r +c_i
Let z=z_r +z_i;
z'=z*z + c
=(z_r +z_i)*(z_r +z_i) + c_r +c_i;
=z_r^2 + 2*z_r*z_i-z_1^2 + c_r +c_i
Thus, simplified
z_r'=z_r^2-z_i^2 +c_r
z_i'=2*z_r*z_i + c_i
What we need to check, is whether z' distance from the origin is less than or equal to a certain value
*/
double z_r=0.0;
double z_i=0.0;
int iterCount=0;
/*to calculate modulus (distance from origin) we use sqrt of(a^2+b^2)<=2 (w is the value we decided)
i.e a^2+b^2 <=4 (squaring both sides) */
while(z_r*z_r + z_i*z_i <= 4.0){
double z_r_tmp = z_r;
z_r = z_r*z_r - z_i*z_i + c_r;
z_i = 2*z_i*z_r_tmp + c_i; //tmp because the value of z_r on line 62 is not the value of z_r on line 63
//Point was inside the Mandelbrot set
if(iterCount >= MAX_ITER){
return MAX_ITER;
}
iterCount++;
}
//if the while loop breaks early, then we know that the complex point c is outside the mandelbrot set
// And we are interested in the number of iterations it did before it broke out of the while loop
return iterCount;
}
//---------------------x-----------------------x-------------------------x----------------x-----------------------------x--------------------------x
private void moveUp(){
double curHeight = HEIGHT/zoomFactor;
topLeftY+=curHeight / 16;
updateFractal();
}
private void moveDown(){
double curHeight = HEIGHT/zoomFactor;
topLeftY-=curHeight / 16;
updateFractal();
}
private void moveLeft(){
double curHeight = WIDTH/zoomFactor;
topLeftX-=curHeight / 16;
updateFractal();
}
private void moveRight(){
double curHeight = WIDTH/zoomFactor;
topLeftX+=curHeight / 16;
updateFractal();
}
//---------------------x-----------------------x-------------------------x----------------x-----------------------------x--------------------------x
private void adjustZoom(double newX, double newY, double newZoomFactor){
topLeftX += newX/zoomFactor;
topLeftY -= newY/zoomFactor;
zoomFactor=newZoomFactor;
topLeftX -= (WIDTH/2) / zoomFactor;
topLeftY += (HEIGHT/2) / zoomFactor;
updateFractal();
}
//---------------------x-----------------------x-------------------------x----------------x-----------------------------x--------------------------x
private class Canvas extends JPanel implements MouseListener {
public Canvas(){
addMouseListener(this);
}
public void addKeyStrokeEvents() {
KeyStroke wKey=KeyStroke.getKeyStroke(KeyEvent.VK_W, 0);
KeyStroke aKey=KeyStroke.getKeyStroke(KeyEvent.VK_A, 0);
KeyStroke sKey=KeyStroke.getKeyStroke(KeyEvent.VK_S, 0);
KeyStroke dKey=KeyStroke.getKeyStroke(KeyEvent.VK_D, 0);
Action wPressed= new AbstractAction(){
@Override public void actionPerformed(ActionEvent e){
moveUp();
}
};
Action aPressed= new AbstractAction(){
@Override public void actionPerformed(ActionEvent e){
moveRight();
}
};
Action sPressed= new AbstractAction(){
@Override public void actionPerformed(ActionEvent e){
moveDown();
}
};
Action dPressed= new AbstractAction(){
@Override public void actionPerformed(ActionEvent e){
moveLeft();
}
};
this.getInputMap().put(wKey, "w_key");
this.getInputMap().put(aKey, "a_key");
this.getInputMap().put(sKey, "s_key");
this.getInputMap().put(dKey, "d_key");
this.getActionMap().put("w_key", wPressed);
this.getActionMap().put("a_key", aPressed);
this.getActionMap().put("s_key", sPressed);
this.getActionMap().put("d_key", dPressed);
}
@Override
public Dimension getPreferredSize(){
return new Dimension(WIDTH, HEIGHT);
}
@Override
public void paintComponent(Graphics drawingObj){
drawingObj.drawImage( fractalImage, 0, 0, null);
//this method would be responsible for drawing image
}
@Override
public void mouseClicked(MouseEvent mouse) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent mouse) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent mouse) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent mouse) {
double x = (double) mouse.getX();
double y = (double) mouse.getY();
switch(mouse.getButton()){
//left
case MouseEvent.BUTTON1:
adjustZoom(x,y,zoomFactor*2);
break;
case MouseEvent.BUTTON3:
adjustZoom(x,y,zoomFactor/2);
break;
}
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent mouse) {
// TODO Auto-generated method stub
}
}
}