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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Camil Staps <info@camilstaps.nl>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.camilstaps.mandelbrot;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
/**
*
* @author camilstaps
*/
public class Grid extends JPanel implements Observer, MouseListener, MouseMotionListener {
private final FractalModel fractalModel;
private final BufferedImage image;
private final WritableRaster raster;
private final int width = 400, height = 400;
private int pixelCounter;
private static final int REPETITIONS_MAX = 1000, STEPS = 50;
private final int[] colors;
private final List<Updater> updaters = new ArrayList<>();
private int start_x, start_y, old_x, old_y;
private boolean dragging = false;
Graphics graphics;
private ProgressView progressView;
public Grid(FractalModel fractalModel) {
this.fractalModel = fractalModel;
fractalModel.addObserver(this);
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
raster = image.getRaster();
setPreferredSize(new Dimension(width, height));
colors = new int[REPETITIONS_MAX + 1];
for (int i = 0; i <=REPETITIONS_MAX; i++) {
colors[i] = Color.HSBtoRGB(
0.07f,
0.5f + 0.5f * (float) i / (float) REPETITIONS_MAX,
1);
}
addMouseListener(this);
addMouseMotionListener(this);
update(fractalModel, null);
}
public ProgressView getProgressView() {
if (progressView == null) {
progressView = new ProgressView();
}
return progressView;
}
@Override
public int getWidth() {
return width;
}
@Override
public int getHeight() {
return height;
}
@Override
public final synchronized void update(Observable o, Object o1) {
if (updaters != null) {
synchronized (updaters) {
for (Updater updater : updaters) {
updater.stop();
updater.cancel(true);
}
}
}
synchronized (updaters) {
updaters.clear();
Updater u1 = new Updater(0, width / 2, 0, height / 2);
u1.execute();
updaters.add(u1);
Updater u2 = new Updater(width / 2, width, 0, height / 2);
u2.execute();
updaters.add(u2);
Updater u3 = new Updater(0, width / 2, height / 2, height);
u3.execute();
updaters.add(u3);
Updater u4 = new Updater(width / 2, width, height / 2, height);
u4.execute();
updaters.add(u4);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
public synchronized void setPixel(int x, int y, int[] rgb) {
raster.setPixel(x, y, rgb);
pixelCounter++;
if (pixelCounter == width * height) {
pixelCounter = 0;
repaint();
}
}
protected double getRealX(int pixel_x) {
return ((double) pixel_x * (fractalModel.getEndX() - fractalModel.getStartX()) / (double) width) + fractalModel.getStartX();
}
protected double getRealY(int pixel_y) {
return ((double) pixel_y * (fractalModel.getEndY() - fractalModel.getStartY()) / (double) height) + fractalModel.getStartY();
}
@Override
public void mouseClicked(MouseEvent me) {
if ((me.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
zoomOut(me);
} else {
zoomIn(me);
}
}
@Override
public void mousePressed(MouseEvent me) {
old_x = start_x = me.getX();
old_y = start_y = me.getY();
}
@Override
public synchronized void mouseReleased(MouseEvent me) {
if (me.getX() != start_x || me.getY() != start_y) {
zoomBox(me);
}
dragging = false;
notifyAll();
}
/**
* Zoom in with factor 2
* @param me
*/
private void zoomIn(MouseEvent me) {
double offset_x = (fractalModel.getEndX() - fractalModel.getStartX()) / 4;
double offset_y = (fractalModel.getEndY() - fractalModel.getStartY()) / 4;
double newCenterX = getRealX(me.getX());
fractalModel.setStartX(newCenterX - offset_x);
fractalModel.setEndX(newCenterX + offset_x);
double newCenterY = getRealY(me.getY());
fractalModel.setStartY(newCenterY - offset_y);
fractalModel.setEndY(newCenterY + offset_y);
}
/**
* Zoom out with factor 2
* @param me
*/
private void zoomOut(MouseEvent me) {
double offset_x = fractalModel.getEndX() - fractalModel.getStartX();
double offset_y = fractalModel.getEndY() - fractalModel.getStartY();
double newCenterX = getRealX(me.getX());
fractalModel.setStartX(newCenterX - offset_x);
fractalModel.setEndX(newCenterX + offset_x);
double newCenterY = getRealY(me.getY());
fractalModel.setStartY(newCenterY - offset_y);
fractalModel.setEndY(newCenterY + offset_y);
}
/**
* Zoom to the selected box.
* Intentionally chose to let the user only select squares, otherwise it's too easy to mess up the scale
* @param me
*/
private void zoomBox(MouseEvent me) {
double newStartX = getRealX(start_x),
newEndX = getRealX(me.getX()),
newStartY = getRealY(start_y),
newEndY = getRealY(start_y + me.getX() - start_x);
fractalModel.setStartX(newStartX);
fractalModel.setEndX(newEndX);
fractalModel.setStartY(newStartY);
fractalModel.setEndY(newEndY);
}
@Override
public void mouseEntered(MouseEvent me) {}
@Override
public void mouseExited(MouseEvent me) {}
@Override
public void mouseDragged(MouseEvent me) {
Graphics g = getSafeGraphics();
if (g != null) {
if (dragging) {
eraseZoombox();
}
g.drawRect(start_x, start_y, me.getX() - start_x, me.getX() - start_x);
old_y = old_x = me.getX();
dragging = true;
}
}
/**
* Semi-singleton construction for graphics
* @return
*/
private Graphics getSafeGraphics() {
if (graphics == null) {
graphics = getGraphics();
graphics.setXORMode(Color.white);
}
return graphics;
}
/**
* Erase the old zoombox if it exists
*/
private void eraseZoombox() {
if (start_x < 0 || start_y < 0 || old_x < 0 || old_y < 0)
return;
getSafeGraphics().drawRect(start_x, start_y, old_x - start_x, old_y - start_x);
}
@Override
public void mouseMoved(MouseEvent me) {}
protected class Updater extends SwingWorker<Map<Point,Integer>, Map<Point,Integer>> {
private boolean doneProcessing = true, stop = false;
private final int start_x, end_x, start_y, end_y;
public Updater(int start_x, int end_x, int start_y, int end_y) {
super();
this.start_x = start_x;
this.end_x = end_x;
this.start_y = start_y;
this.end_y = end_y;
}
@Override
protected Map<Point, Integer> doInBackground() throws Exception {
Map<Point,Integer> results = new HashMap<>();
for (int repetitions = REPETITIONS_MAX / STEPS; repetitions <= REPETITIONS_MAX && !stop; repetitions += REPETITIONS_MAX / STEPS) {
for (int x = start_x; x < end_x && !stop; x++) {
for (int y = start_y; y < end_y && !stop; y++) {
Point p = new Point(x, y);
results.put(p, fractalModel.getMandelNumber(getRealX(x), getRealY(y), repetitions));
}
}
// Since we're always publishing the same object, there's no point in calling publish if process() wasn't called yet
if (doneProcessing && !stop) {
doneProcessing = false;
publish(results);
}
setProgress(repetitions * 100 / REPETITIONS_MAX);
}
return results;
}
@Override
protected void process(List<Map<Point,Integer>> results) {
if (progressView != null) {
progressView.setValue();
}
for (Map<Point,Integer> resultMap : results) {
if (stop || dragging)
break;
for (Entry<Point,Integer> result : resultMap.entrySet()) {
int rgbValue = colors[result.getValue()];
int[] rgb = { (rgbValue >> 16) & 0xff, (rgbValue >> 8) & 0xff, rgbValue & 0xff };
setPixel(result.getKey().x, result.getKey().y, rgb);
}
}
doneProcessing = true;
}
@Override
public void done() {
//updaters.remove(this);
}
public void stop() {
stop = true;
}
}
protected class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object another) {
if (another == null || another.getClass() != Point.class) {
return false;
}
Point that = (Point) another;
return that.x == x && that.y == y;
}
@Override
public int hashCode() {
return (x << 8) | y;
}
}
protected class ProgressView extends JProgressBar {
public ProgressView() {
setMaximum(100);
setMinimum(0);
setValue(0);
}
@Override
public final void setValue(int n) {
setVisible(n != 100);
super.setValue(n);
}
public void setValue() {
int value = 100;
for (Updater updater : updaters) {
if (updater.getProgress() < value) {
value = updater.getProgress();
}
}
setValue(value);
}
}
}
|