From 97d87fb1babc6168c92693d0f63ae9cd86c1aeea Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Wed, 18 Nov 2020 19:17:15 +0100 Subject: [PATCH] Preview is now shown --- .../CalculationThread.java | 2 + src/mandelbrotCalculator/MandelbrotSet.java | 52 ++++++++++++++++--- src/ui/Controller.java | 10 ++-- src/ui/sample.fxml | 2 +- src/util/Values.java | 7 +++ 5 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 src/util/Values.java diff --git a/src/mandelbrotCalculator/CalculationThread.java b/src/mandelbrotCalculator/CalculationThread.java index 38ab80b..b02b264 100644 --- a/src/mandelbrotCalculator/CalculationThread.java +++ b/src/mandelbrotCalculator/CalculationThread.java @@ -50,6 +50,8 @@ public class CalculationThread extends Thread { createImage(image, frameCounter, width, height, values, iterations); + set.frameFinished(frameCounter); + long frameTime = System.currentTimeMillis() - startTime; System.out.println("Frame " + frameCounter + " finished in " + ((double) frameTime / 1000) + "s"); diff --git a/src/mandelbrotCalculator/MandelbrotSet.java b/src/mandelbrotCalculator/MandelbrotSet.java index cb1a463..7904214 100644 --- a/src/mandelbrotCalculator/MandelbrotSet.java +++ b/src/mandelbrotCalculator/MandelbrotSet.java @@ -1,5 +1,9 @@ package mandelbrotCalculator; +import ui.Controller; + +import java.io.FileNotFoundException; + public class MandelbrotSet { private double[][] interestingPoints = {{-0.75, 0}, {-0.77568377, 0.13646737}, {-1.74995768370609350360221450607069970727110579726252077930242837820286008082972804887218672784431700831100544507655659531379747541999999995, 0.00000000000000000278793706563379402178294753790944364927085054500163081379043930650189386849765202169477470552201325772332454726999999995}}; @@ -16,16 +20,19 @@ public class MandelbrotSet { private int width = 1920; private int threshold = 1000; private float ratio = 2 / 3f; - private int threadAmount = Runtime.getRuntime().availableProcessors(); + private int threadAmount = Runtime.getRuntime().availableProcessors() / 2; //only declared private int height; private int iterations; - private boolean[] complete; + private boolean[] completeThreads; + private boolean[] completeFrames; private long startTime = System.currentTimeMillis(); + private Controller controller; + /** * Create a new Mandelbrot set manager @@ -34,10 +41,11 @@ public class MandelbrotSet { * @param zoomSpeed By how much the zoom value is multiplied every frame * @param frames The number of frames to be calculated */ - public MandelbrotSet(int pointNumber, int quality, double zoomSpeed, int frames) { + public MandelbrotSet(int pointNumber, int quality, double zoomSpeed, int frames, Controller controller) { this.pointNumber = pointNumber; this.zoomSpeed = zoomSpeed; this.frames = frames; + this.controller = controller; height = (int) ((float) width * ratio); @@ -50,11 +58,12 @@ public class MandelbrotSet { default -> quality; }; - complete = new boolean[threadAmount]; + completeThreads = new boolean[threadAmount]; + completeFrames = new boolean[frames]; } - public MandelbrotSet(){ - this(2, 2, 1.2, 1); + public MandelbrotSet(Controller c){ + this(2, 2, 1.2, 1, c); } public void startMandelbrot() { @@ -115,11 +124,38 @@ public class MandelbrotSet { return zoomValues; } + /** + * Call when a new frame is finished + * @param frameNumber The number of the frame + */ + public void frameFinished(int frameNumber){ + completeFrames[frameNumber] = true; + + int latestFrame = 0; + for(int i = 0; i < completeFrames.length; i++){ + if(completeFrames[i]){ + latestFrame = i; + } + } + + System.err.println(latestFrame); + + try { + controller.nextImage(latestFrame); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + /** + * Call when a new Thread is finished + * @param threadNumber The Threadier + */ public void setFinished(int threadNumber){ - complete[threadNumber] = true; + completeThreads[threadNumber] = true; boolean finished = true; - for(boolean b : complete){ + for(boolean b : completeThreads){ if (!b) { finished = false; break; diff --git a/src/ui/Controller.java b/src/ui/Controller.java index c8f9749..c104498 100644 --- a/src/ui/Controller.java +++ b/src/ui/Controller.java @@ -4,6 +4,7 @@ import javafx.event.ActionEvent; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import mandelbrotCalculator.MandelbrotSet; +import util.Values; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -13,18 +14,15 @@ public class Controller { public ImageView setPreview; - int currentFrame; + public void nextImage(int frameNumber) throws FileNotFoundException { - public void showImage(ActionEvent actionEvent) throws FileNotFoundException { - - InputStream stream = new FileInputStream("C:\\Users\\nilsh\\IdeaProjects\\testStuff\\sequence/Sequence" + currentFrame + ".png"); + InputStream stream = new FileInputStream(Values.SAVE_IMAGE_PATH + frameNumber + ".png"); Image image = new Image(stream); setPreview.setImage(image); - currentFrame++; } public void startCalculation(ActionEvent actionEvent) { - MandelbrotSet m = new MandelbrotSet(2, 2, 3, 10); + MandelbrotSet m = new MandelbrotSet(2, 1, 3, 100, this); m.startMandelbrot(); } } diff --git a/src/ui/sample.fxml b/src/ui/sample.fxml index af166ca..851a68b 100644 --- a/src/ui/sample.fxml +++ b/src/ui/sample.fxml @@ -6,7 +6,7 @@