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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
/*
* 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;
/**
* The Grid is both a View and a Controller: it shows a graphical representation
* of the FractalModel, and allows the user to control that FractalModel by
* clicking (zoom in), shift-clicking (zoom out) and dragging (box zoom).
* @author Camil Staps
*/
public class Grid extends JPanel implements Observer, MouseListener, MouseMotionListener {
/**
* Hard maximum amount of repetitions and the amount of steps to take to get
* to that maximum
*/
private static final int REPETITIONS_MAX = 1000, STEPS = 50;
/**
* Width and height of the grid
*/
private static final int WIDTH = 400, HEIGHT = 400;
/**
* List of colours for visualisation
*/
private final int[] colors;
/**
* The FractalModel to visualise
*/
private final FractalModel fractalModel;
/**
* Used for visualisation
*/
private final BufferedImage image;
private final WritableRaster raster;
/**
* Count pixels changed to check if we need to repaint
*/
private int pixelCounter;
/**
* Updating the view is done by SwingWorkers. The solution provides support
* for multiple SwingWorkers; Updaters; which are held in this list.
*/
private final List<Updater> updaters = new ArrayList<>();
/**
* Used for handling mouse events and zooming
*/
private int start_x, start_y, old_x, old_y;
private boolean dragging = false;
Graphics graphics;
/**
* A progress bar
*/
private ProgressView progressView;
/**
* Whether or not to use multiple SwingWorkers.
* Using multiple SwingWorkers is *not* faster. The application is already
* faster by using memory in the MandelbrotFractal class. Using multiple
* SwingWorkers does not aid in speed.
*/
private boolean useMultipleSwingWorkers = false;
/**
* Create a new Grid to view and control a FractalModel
* @param fractalModel
*/
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);
}
/**
* Get the progress bar (create it if it doesn't exist yet)
* @return
*/
public ProgressView getProgressView() {
if (progressView == null) {
progressView = new ProgressView();
}
return progressView;
}
@Override
public int getWidth() {
return WIDTH;
}
@Override
public int getHeight() {
return HEIGHT;
}
/**
* Change whether we use multiple SwingWorkers or not. This change will
* take effect when the FractalModel is updated.
* @param value
*/
public void setUseMultipleSwingWorkers(boolean value) {
useMultipleSwingWorkers = value;
}
/**
* Stop the previous update and start rendering all over again.
* @param o
* @param o1
*/
@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();
if (useMultipleSwingWorkers) {
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);
} else {
Updater u = new Updater(0, WIDTH, 0, HEIGHT);
u.execute();
updaters.add(u);
}
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
/**
* Set a pixel on the grid
* @param x
* @param y
* @param rgb
*/
public synchronized void setPixel(int x, int y, int[] rgb) {
raster.setPixel(x, y, rgb);
pixelCounter++;
if (pixelCounter == WIDTH * HEIGHT) {
pixelCounter = 0;
repaint();
}
}
/**
* Get the 'mathematical' x coordinate that belongs to a pixel's x coordinate
* @param pixel_x
* @return
*/
protected double getRealX(int pixel_x) {
return ((double) pixel_x * (fractalModel.getEndX() - fractalModel.getStartX())
/ (double) WIDTH) + fractalModel.getStartX();
}
/**
* Get the 'mathematical' y coordinate that belongs to a pixel's y coordinate
* @param pixel_y
* @return
*/
protected double getRealY(int pixel_y) {
return ((double) pixel_y * (fractalModel.getEndY() - fractalModel.getStartY())
/ (double) HEIGHT) + fractalModel.getStartY();
}
/**
* Click to zoom in; Shift-click to zoom out
* @param me
*/
@Override
public void mouseClicked(MouseEvent me) {
if ((me.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
zoomOut(me);
} else {
zoomIn(me);
}
}
/**
* Keep track of mouse data for the box zoom
* @param me
*/
@Override
public void mousePressed(MouseEvent me) {
old_x = start_x = me.getX();
old_y = start_y = me.getY();
}
/**
* If this was a box zoom, ... well, perform a box zoom.
* @param me
*/
@Override
public synchronized void mouseReleased(MouseEvent me) {
if (me.getX() != start_x || me.getY() != start_y) {
zoomBox(me);
}
dragging = false;
}
/**
* 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) {}
/**
* Draw the zoombox on dragging
* @param 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) {}
/**
* A SwingWorker to update the graphics
*/
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;
/**
* Update only the given rectangle
* @param start_x
* @param end_x
* @param start_y
* @param 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;
}
/**
* Calculate all points in the rectangle using the FractalModel
* @return the resulting points
*/
@Override
protected Map<Point, Integer> doInBackground() {
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;
}
/**
* Process a list of points, and update the progress bar
* @param 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;
}
/**
* Tell the updater to stop
*/
public void stop() {
stop = true;
}
}
/**
* A point on the grid
*/
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;
}
}
/**
* A special ProgressBar which is hidden when full, and takes the average
* of the progresses of all Updaters as its value
*/
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);
}
/**
* Calculate the value as the average of the progresses of all Updaters
*/
public void setValue() {
int sum = 0, count = 0;
for (Updater updater : updaters) {
sum += updater.getProgress();
count++;
}
setValue(sum / count);
}
}
}
|