diff options
Diffstat (limited to 'Week15 Mandelbrot/src')
4 files changed, 129 insertions, 38 deletions
diff --git a/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/FractalModel.java b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/FractalModel.java index 03a8908..ab2446d 100644 --- a/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/FractalModel.java +++ b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/FractalModel.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; @@ -14,9 +34,9 @@ public class FractalModel extends Observable { private double start_x, start_y, end_x, end_y; public FractalModel() { - start_x = -1; + start_x = -1.46; + end_x = 0.54; start_y = -1; - end_x = 1; end_y = 1; } 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); + } + } } diff --git a/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotFractal.java b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotFractal.java index 1c608fd..43909fe 100644 --- a/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotFractal.java +++ b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/MandelbrotFractal.java @@ -39,12 +39,12 @@ public class MandelbrotFractal { */ private static final Map<Point,Result> mandelNumbers = new HashMap<>(); - public static int mandelNumber(double x, double y, int repetitions) { + public synchronized static int mandelNumber(double x, double y, int repetitions) { Point p = new Point(x, y); return mandelNumber(p, repetitions); } - public static int mandelNumber(Point p, int repetitions) { + public synchronized static int mandelNumber(Point p, int repetitions) { if (mandelNumbers.containsKey(p)) { Result result = mandelNumbers.get(p); if (repetitions < result.repetitions || @@ -61,7 +61,7 @@ public class MandelbrotFractal { } } - protected static Result calculateMandelNumber(Point p, int repetitions) { + protected synchronized static Result calculateMandelNumber(Point p, int repetitions) { Result start = new Result(); start.x = p.x; start.y = p.y; @@ -69,7 +69,7 @@ public class MandelbrotFractal { return start; } - protected static void calculateMandelNumber(Point p, int repetitions, Result start) { + protected synchronized static void calculateMandelNumber(Point p, int repetitions, Result start) { int n = start.repetitions; while (start.x * start.x + start.y * start.y <= 4 && n < repetitions) { diff --git a/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/Textfields.java b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/Textfields.java index 78098d1..b4eb6a7 100644 --- a/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/Textfields.java +++ b/Week15 Mandelbrot/src/com/camilstaps/mandelbrot/Textfields.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; @@ -24,13 +44,11 @@ public class Textfields extends JPanel implements Observer { private final String INITIAL_CENTER_X = "0", INITIAL_CENTER_Y = "0", - INITIAL_SCALE = "0.5", - INITIAL_REPETITIONS = "100"; + INITIAL_SCALE = "0.5"; private final JTextField field_centerX = new JTextField(INITIAL_CENTER_X, 6); private final JTextField field_centerY = new JTextField(INITIAL_CENTER_Y, 6); private final JTextField field_scale = new JTextField(INITIAL_SCALE, 6); - private final JTextField field_repetitions = new JTextField(INITIAL_REPETITIONS, 6); private final JButton button_redraw = new JButton("Redraw"); private final JButton button_reset = new JButton("Reset"); @@ -41,6 +59,8 @@ public class Textfields extends JPanel implements Observer { fractalModel.addObserver(this); setupControls(); + + update(fractalModel, null); } private void setupControls() { @@ -55,9 +75,6 @@ public class Textfields extends JPanel implements Observer { panel.add(new JLabel("Scale:")); panel.add(field_scale); - panel.add(new JLabel("Repetitions:")); - panel.add(field_repetitions); - button_redraw.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { @@ -74,7 +91,6 @@ public class Textfields extends JPanel implements Observer { field_centerX.setText(INITIAL_CENTER_X); field_centerY.setText(INITIAL_CENTER_Y); field_scale.setText(INITIAL_SCALE); - field_repetitions.setText(INITIAL_REPETITIONS); button_redraw.doClick(); } @@ -115,7 +131,7 @@ public class Textfields extends JPanel implements Observer { } @Override - public void update(Observable o, Object o1) { + public final void update(Observable o, Object o1) { field_centerX.setText(Float.toString((float) ((fractalModel.getEndX() - fractalModel.getStartX()) / 2 + fractalModel.getStartX()))); field_centerY.setText(Float.toString((float) |