Created config class

This commit is contained in:
Alberto Venturini 2017-09-17 07:24:11 +02:00
parent 1c6427c0e5
commit 12f1744451
9 changed files with 190 additions and 32 deletions

View file

@ -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();

View file

@ -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);
}

View file

@ -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;
}
}

View file

@ -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++) {

View file

@ -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;
// }
// }
// }
//
//}

View file

@ -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,

View file

@ -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) {

View file

@ -0,0 +1,9 @@
package com.albertoventurini.juliaset.calculator;
import com.albertoventurini.juliaset.JuliaSetConfig;
public interface JuliaSetCalculator {
int[][] calculate(JuliaSetConfig config);
}

View file

@ -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 {