From 12f17444510db4ed926a7abfc222d6b1506485a8 Mon Sep 17 00:00:00 2001 From: Alberto Venturini Date: Sun, 17 Sep 2017 07:24:11 +0200 Subject: [PATCH] Created config class --- src/com/albertoventurini/MainClass.java | 11 ++- .../juliaset/JuliaSetCalculator.java | 13 --- .../juliaset/JuliaSetConfig.java | 98 +++++++++++++++++++ .../juliaset/JuliaSetPanel.java | 10 +- .../juliaset/JuliaSetUtils.java | 31 ++++++ .../ExecutorServiceJuliaSetCalculator.java | 20 ++-- .../ForkJoinJuliaSetCalculator.java | 15 ++- .../calculator/JuliaSetCalculator.java | 9 ++ .../ThreadedJuliaSetCalculator.java | 15 ++- 9 files changed, 190 insertions(+), 32 deletions(-) delete mode 100644 src/com/albertoventurini/juliaset/JuliaSetCalculator.java create mode 100644 src/com/albertoventurini/juliaset/JuliaSetConfig.java create mode 100644 src/com/albertoventurini/juliaset/JuliaSetUtils.java rename src/com/albertoventurini/juliaset/{ => calculator}/ExecutorServiceJuliaSetCalculator.java (80%) rename src/com/albertoventurini/juliaset/{ => calculator}/ForkJoinJuliaSetCalculator.java (82%) create mode 100644 src/com/albertoventurini/juliaset/calculator/JuliaSetCalculator.java rename src/com/albertoventurini/juliaset/{ => calculator}/ThreadedJuliaSetCalculator.java (80%) diff --git a/src/com/albertoventurini/MainClass.java b/src/com/albertoventurini/MainClass.java index b878e2f..90bed1e 100644 --- a/src/com/albertoventurini/MainClass.java +++ b/src/com/albertoventurini/MainClass.java @@ -1,8 +1,9 @@ package com.albertoventurini; -import com.albertoventurini.juliaset.JuliaSetCalculator; +import com.albertoventurini.juliaset.JuliaSetConfig; +import com.albertoventurini.juliaset.calculator.JuliaSetCalculator; import com.albertoventurini.juliaset.JuliaSetPanel; -import com.albertoventurini.juliaset.ThreadedJuliaSetCalculator; +import com.albertoventurini.juliaset.calculator.ThreadedJuliaSetCalculator; import javax.swing.*; import java.awt.*; @@ -13,8 +14,10 @@ public class MainClass { int width = 1600; int height = 1000; - JuliaSetCalculator juliaSetCalculator = new ThreadedJuliaSetCalculator(width, height); - JuliaSetPanel juliaSetPanel = new JuliaSetPanel(juliaSetCalculator, width, height); + JuliaSetCalculator calculator = new ThreadedJuliaSetCalculator(width, height); + JuliaSetConfig config = new JuliaSetConfig(width, height, 1000, 1.0, + -0.7, 0.27015, 0.0, 0.0); + JuliaSetPanel juliaSetPanel = new JuliaSetPanel(calculator, config); SwingUtilities.invokeLater(() -> { JFrame f = new JFrame(); diff --git a/src/com/albertoventurini/juliaset/JuliaSetCalculator.java b/src/com/albertoventurini/juliaset/JuliaSetCalculator.java deleted file mode 100644 index 29c451e..0000000 --- a/src/com/albertoventurini/juliaset/JuliaSetCalculator.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.albertoventurini.juliaset; - -public interface JuliaSetCalculator { - - int[][] calculate( - int maxIterations, - double zoom, - double cx, - double cy, - double moveX, - double moveY); - -} diff --git a/src/com/albertoventurini/juliaset/JuliaSetConfig.java b/src/com/albertoventurini/juliaset/JuliaSetConfig.java new file mode 100644 index 0000000..5a06821 --- /dev/null +++ b/src/com/albertoventurini/juliaset/JuliaSetConfig.java @@ -0,0 +1,98 @@ +package com.albertoventurini.juliaset; + +public class JuliaSetConfig { + + private int width; + private int height; + private int maxIterations; + private double zoom; + private double cx; + private double cy; + private double moveX; + private double moveY; + + public JuliaSetConfig( + final int width, + final int height, + final int maxIterations, + final double zoom, + final double cx, + final double cy, + final double moveX, + final double moveY + ) { + + this.width = width; + this.height = height; + this.maxIterations = maxIterations; + this.zoom = zoom; + this.cx = cx; + this.cy = cy; + this.moveX = moveX; + this.moveY = moveY; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public int getMaxIterations() { + return maxIterations; + } + + public void setMaxIterations(int maxIterations) { + this.maxIterations = maxIterations; + } + + public double getZoom() { + return zoom; + } + + public void setZoom(double zoom) { + this.zoom = zoom; + } + + public double getCx() { + return cx; + } + + public void setCx(double cx) { + this.cx = cx; + } + + public double getCy() { + return cy; + } + + public void setCy(double cy) { + this.cy = cy; + } + + public double getMoveX() { + return moveX; + } + + public void setMoveX(double moveX) { + this.moveX = moveX; + } + + public double getMoveY() { + return moveY; + } + + public void setMoveY(double moveY) { + this.moveY = moveY; + } +} diff --git a/src/com/albertoventurini/juliaset/JuliaSetPanel.java b/src/com/albertoventurini/juliaset/JuliaSetPanel.java index c1b1360..08ec3df 100644 --- a/src/com/albertoventurini/juliaset/JuliaSetPanel.java +++ b/src/com/albertoventurini/juliaset/JuliaSetPanel.java @@ -1,5 +1,7 @@ package com.albertoventurini.juliaset; +import com.albertoventurini.juliaset.calculator.JuliaSetCalculator; + import javax.swing.*; import java.awt.*; import java.awt.image.BufferedImage; @@ -10,10 +12,12 @@ public class JuliaSetPanel extends JPanel { private double cY, cX; private JuliaSetCalculator juliaSetCalculator; + private JuliaSetConfig config; - public JuliaSetPanel(final JuliaSetCalculator juliaSetCalculator, final int width, final int height) { + public JuliaSetPanel(final JuliaSetCalculator juliaSetCalculator, final JuliaSetConfig config) { this.juliaSetCalculator = juliaSetCalculator; - setPreferredSize(new Dimension(width, height)); + this.config = config; + setPreferredSize(new Dimension(config.getWidth(), config.getHeight())); setBackground(Color.white); } @@ -26,7 +30,7 @@ public class JuliaSetPanel extends JPanel { cY = 0.27015; double moveX = 0, moveY = 0; - int[][] iterations = juliaSetCalculator.calculate(maxIter, zoom, cX, cY, moveX, moveY); + int[][] iterations = juliaSetCalculator.calculate(config); for(int x = 0; x < width; x++) { for(int y = 0; y < height; y++) { diff --git a/src/com/albertoventurini/juliaset/JuliaSetUtils.java b/src/com/albertoventurini/juliaset/JuliaSetUtils.java new file mode 100644 index 0000000..2dee108 --- /dev/null +++ b/src/com/albertoventurini/juliaset/JuliaSetUtils.java @@ -0,0 +1,31 @@ +//package com.albertoventurini.juliaset; +// +//public class JuliaSetUtils { +// +// public static int[] calculateChunk( +// final JuliaSetConfig config, +// final int startInclusive, +// final int endExclusive) { +// +// double zx, zy; +// +// int width = config.getWidth(); +// int height = config.getHeight(); +// +// for(int x = startInclusive; x < endExclusive; x++) { +// for (int y = 0; y < height; y++) { +// zx = 1.5 * (x - width / 2) / (0.5 * zoom * width) + moveX; +// zy = (y - height / 2) / (0.5 * zoom * height) + moveY; +// int i = maxIterations; +// while (zx * zx + zy * zy < 4 && i > 0) { +// double tmp = zx * zx - zy * zy + cx; +// zy = 2.0 * zx * zy + cy; +// zx = tmp; +// i--; +// } +// iterations[x][y] = i; +// } +// } +// } +// +//} diff --git a/src/com/albertoventurini/juliaset/ExecutorServiceJuliaSetCalculator.java b/src/com/albertoventurini/juliaset/calculator/ExecutorServiceJuliaSetCalculator.java similarity index 80% rename from src/com/albertoventurini/juliaset/ExecutorServiceJuliaSetCalculator.java rename to src/com/albertoventurini/juliaset/calculator/ExecutorServiceJuliaSetCalculator.java index 917de65..4e6b87a 100644 --- a/src/com/albertoventurini/juliaset/ExecutorServiceJuliaSetCalculator.java +++ b/src/com/albertoventurini/juliaset/calculator/ExecutorServiceJuliaSetCalculator.java @@ -1,4 +1,6 @@ -package com.albertoventurini.juliaset; +package com.albertoventurini.juliaset.calculator; + +import com.albertoventurini.juliaset.JuliaSetConfig; import java.util.concurrent.*; @@ -14,13 +16,15 @@ public class ExecutorServiceJuliaSetCalculator implements JuliaSetCalculator { iterations = new int[width][height]; } - public int[][] calculate( - final int maxIterations, - final double zoom, - final double cx, - final double cy, - final double moveX, - final double moveY) { + public int[][] calculate(final JuliaSetConfig config) { + + int[][] result = new int[config.getWidth()][config.getHeight()]; + double zoom = config.getZoom(); + double moveX = config.getMoveX(); + double moveY = config.getMoveY(); + int maxIterations = config.getMaxIterations(); + double cx = config.getCx(); + double cy = config.getCy(); /** * Each task calculates one row. I.e. if we have 'width*height' iterations, diff --git a/src/com/albertoventurini/juliaset/ForkJoinJuliaSetCalculator.java b/src/com/albertoventurini/juliaset/calculator/ForkJoinJuliaSetCalculator.java similarity index 82% rename from src/com/albertoventurini/juliaset/ForkJoinJuliaSetCalculator.java rename to src/com/albertoventurini/juliaset/calculator/ForkJoinJuliaSetCalculator.java index aae1b40..2ec1e53 100644 --- a/src/com/albertoventurini/juliaset/ForkJoinJuliaSetCalculator.java +++ b/src/com/albertoventurini/juliaset/calculator/ForkJoinJuliaSetCalculator.java @@ -1,4 +1,6 @@ -package com.albertoventurini.juliaset; +package com.albertoventurini.juliaset.calculator; + +import com.albertoventurini.juliaset.JuliaSetConfig; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveAction; @@ -16,13 +18,22 @@ public class ForkJoinJuliaSetCalculator implements JuliaSetCalculator { } @Override - public int[][] calculate(int maxIterations, double zoom, double cx, double cy, double moveX, double moveY) { + public int[][] calculate(final JuliaSetConfig config) { + + int[][] result = new int[config.getWidth()][config.getHeight()]; + double zoom = config.getZoom(); + double moveX = config.getMoveX(); + double moveY = config.getMoveY(); + int maxIterations = config.getMaxIterations(); + double cx = config.getCx(); + double cy = config.getCy(); class JuliaTask extends RecursiveAction { private int startInclusive; private int endExclusive; + // When the problem size is too small to be divided into subproblems private final int THRESHOLD = 10; JuliaTask(int startInclusive, int endExclusive) { diff --git a/src/com/albertoventurini/juliaset/calculator/JuliaSetCalculator.java b/src/com/albertoventurini/juliaset/calculator/JuliaSetCalculator.java new file mode 100644 index 0000000..6496ab7 --- /dev/null +++ b/src/com/albertoventurini/juliaset/calculator/JuliaSetCalculator.java @@ -0,0 +1,9 @@ +package com.albertoventurini.juliaset.calculator; + +import com.albertoventurini.juliaset.JuliaSetConfig; + +public interface JuliaSetCalculator { + + int[][] calculate(JuliaSetConfig config); + +} diff --git a/src/com/albertoventurini/juliaset/ThreadedJuliaSetCalculator.java b/src/com/albertoventurini/juliaset/calculator/ThreadedJuliaSetCalculator.java similarity index 80% rename from src/com/albertoventurini/juliaset/ThreadedJuliaSetCalculator.java rename to src/com/albertoventurini/juliaset/calculator/ThreadedJuliaSetCalculator.java index 65dc18f..c47fb70 100644 --- a/src/com/albertoventurini/juliaset/ThreadedJuliaSetCalculator.java +++ b/src/com/albertoventurini/juliaset/calculator/ThreadedJuliaSetCalculator.java @@ -1,4 +1,7 @@ -package com.albertoventurini.juliaset; +package com.albertoventurini.juliaset.calculator; + +import com.albertoventurini.juliaset.JuliaSetConfig; +import com.albertoventurini.juliaset.calculator.JuliaSetCalculator; public class ThreadedJuliaSetCalculator implements JuliaSetCalculator { @@ -13,7 +16,15 @@ public class ThreadedJuliaSetCalculator implements JuliaSetCalculator { } @Override - public int[][] calculate(int maxIterations, double zoom, double cx, double cy, double moveX, double moveY) { + public int[][] calculate(final JuliaSetConfig config) { + + int[][] result = new int[config.getWidth()][config.getHeight()]; + double zoom = config.getZoom(); + double moveX = config.getMoveX(); + double moveY = config.getMoveY(); + int maxIterations = config.getMaxIterations(); + double cx = config.getCx(); + double cy = config.getCy(); class JuliaTask implements Runnable {