aboutsummaryrefslogtreecommitdiff
path: root/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/Grid.java
diff options
context:
space:
mode:
authorCamil Staps2015-06-05 15:03:46 +0200
committerCamil Staps2015-06-05 15:03:46 +0200
commit6d63d3cea59f0a24ba92bdc5e2d1887284a657d7 (patch)
treedcfc15a8afaa3b01c8d7de9d93bbdcb2f31cd8a3 /Week15 Mandelbrot/src/com/camilstaps/mandelbrot/Grid.java
parentEverything works except for multiple swingworkers (diff)
Version with four swingworkers
Diffstat (limited to 'Week15 Mandelbrot/src/com/camilstaps/mandelbrot/Grid.java')
-rw-r--r--Week15 Mandelbrot/src/com/camilstaps/mandelbrot/Grid.java99
1 files changed, 77 insertions, 22 deletions
diff --git a/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/Grid.java b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/Grid.java
index bf21b43..3d05b14 100644
--- a/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/Grid.java
+++ b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/Grid.java
@@ -1,5 +1,25 @@
/*
- * Copyright (c) 2015 Camil Staps
+ * 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;
@@ -12,6 +32,7 @@ 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;
@@ -32,17 +53,15 @@ public class Grid extends JPanel implements Observer, MouseListener, MouseMotion
private final BufferedImage image;
private final WritableRaster raster;
- private final int width, height;
- private static final int DEFAULT_WIDTH = 400, DEFAULT_HEIGHT = 400;
+ private final int width = 400, height = 400;
private int pixelCounter;
- private static final int COUNTER_MAX = 1000;
private static final int REPETITIONS_MAX = 1000, STEPS = 50;
private final int[] colors;
- private Updater updater;
+ private final List<Updater> updaters = new ArrayList<>();
private int start_x, start_y, old_x, old_y;
private boolean dragging = false;
@@ -54,9 +73,6 @@ public class Grid extends JPanel implements Observer, MouseListener, MouseMotion
this.fractalModel = fractalModel;
fractalModel.addObserver(this);
- width = DEFAULT_WIDTH;
- height = DEFAULT_HEIGHT;
-
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
raster = image.getRaster();
@@ -94,13 +110,30 @@ public class Grid extends JPanel implements Observer, MouseListener, MouseMotion
}
@Override
- public synchronized void update(Observable o, Object o1) {
- if (updater != null) {
- updater.stop();
- updater.cancel(true);
+ 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);
}
- updater = new Updater();
- updater.execute();
}
@Override
@@ -109,10 +142,10 @@ public class Grid extends JPanel implements Observer, MouseListener, MouseMotion
g.drawImage(image, 0, 0, null);
}
- public void setPixel(int x, int y, int[] rgb) {
+ public synchronized void setPixel(int x, int y, int[] rgb) {
raster.setPixel(x, y, rgb);
pixelCounter++;
- if (pixelCounter > COUNTER_MAX) {
+ if (pixelCounter == width * height) {
pixelCounter = 0;
repaint();
}
@@ -248,13 +281,24 @@ public class Grid extends JPanel implements Observer, MouseListener, MouseMotion
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 = 0; x < width && !stop; x++) {
- for (int y = 0; y < height && !stop; y++) {
+ 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));
}
@@ -272,9 +316,9 @@ public class Grid extends JPanel implements Observer, MouseListener, MouseMotion
}
@Override
- protected synchronized void process(List<Map<Point,Integer>> results) {
+ protected void process(List<Map<Point,Integer>> results) {
if (progressView != null) {
- progressView.setValue(getProgress());
+ progressView.setValue();
}
for (Map<Point,Integer> resultMap : results) {
@@ -293,7 +337,7 @@ public class Grid extends JPanel implements Observer, MouseListener, MouseMotion
@Override
public void done() {
- updater = null;
+ //updaters.remove(this);
}
public void stop() {
@@ -329,14 +373,25 @@ public class Grid extends JPanel implements Observer, MouseListener, MouseMotion
public ProgressView() {
setMaximum(100);
setMinimum(0);
+ setValue(0);
}
@Override
- public void setValue(int n) {
+ 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);
+ }
+
}
}