From eb71992b087b70fa70b4cbe121d3c52d035e6c3a Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Wed, 18 Nov 2020 16:43:04 +0100 Subject: [PATCH] added finish message --- .../CalculationThread.java | 18 +++---- src/mandelbrotCalculator/MandelbrotSet.java | 47 ++++++++++++------- src/ui/Controller.java | 2 +- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/mandelbrotCalculator/CalculationThread.java b/src/mandelbrotCalculator/CalculationThread.java index ea9163d..38ab80b 100644 --- a/src/mandelbrotCalculator/CalculationThread.java +++ b/src/mandelbrotCalculator/CalculationThread.java @@ -18,6 +18,8 @@ public class CalculationThread extends Thread { double[][] zoomValues; CNumber[][] samples; + MandelbrotSet set; + public void run() { long totalStartTime = System.currentTimeMillis(); @@ -49,16 +51,19 @@ public class CalculationThread extends Thread { createImage(image, frameCounter, width, height, values, iterations); long frameTime = System.currentTimeMillis() - startTime; - System.out.println("------------------------Frame " + frameCounter + " finished in " + ((double) frameTime / 1000) + "s------------------------"); + System.out.println("Frame " + frameCounter + " finished in " + ((double) frameTime / 1000) + "s"); } long totalTime = System.currentTimeMillis() - totalStartTime; - System.out.println("Thread " + threadNumber + " completed. Process took " + ((double) totalTime / 1000) + "s"); + System.out.println("--Thread " + threadNumber + " completed. Process took " + ((double) totalTime / 1000) + "s"); + + set.setFinished(threadNumber); } - public CalculationThread(int number, int threads, int frames, int widthC, int heightC, int iterationsC, int thresholdC, double[][] zoomValuesC) { + public CalculationThread(MandelbrotSet set, int number, int threads, int frames, int widthC, int heightC, int iterationsC, int thresholdC, double[][] zoomValuesC) { + this.set = set; this.threadNumber = number; this.threadAmount = threads; this.frameAmount = frames; @@ -80,8 +85,6 @@ public class CalculationThread extends Thread { */ void createImage(BufferedImage image, int counter, int width, int height, double[][] values, int iterations) { - //System.out.println("Frame: " + counter + " | Started creating image..."); - int p0 = getColorAsInt(0, 0, 0, 0); int t0 = -1; @@ -100,16 +103,15 @@ public class CalculationThread extends Thread { } } try { - File f = new File("C:\\Users\\nilsh\\Desktop\\testordner/sterbi" + counter + ".png"); + File f = new File("C:\\Users\\nilsh\\Desktop\\testordner/image" + counter + ".png"); ImageIO.write(image, "png", f); - System.out.println(f.getAbsolutePath()); } catch (IOException e) { e.printStackTrace(); } } /** - * Checks wheter the number is in the Mandelbrot set + * Checks whether the number is in the Mandelbrot set * @param number The Complex Number to be checked * @param iterations The amount of iterations the program should do * @param threshold The threshold for a number not being in the set diff --git a/src/mandelbrotCalculator/MandelbrotSet.java b/src/mandelbrotCalculator/MandelbrotSet.java index fb0886d..cb1a463 100644 --- a/src/mandelbrotCalculator/MandelbrotSet.java +++ b/src/mandelbrotCalculator/MandelbrotSet.java @@ -1,11 +1,5 @@ package mandelbrotCalculator; -import javax.imageio.ImageIO; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.io.File; -import java.io.IOException; - public class MandelbrotSet { private double[][] interestingPoints = {{-0.75, 0}, {-0.77568377, 0.13646737}, {-1.74995768370609350360221450607069970727110579726252077930242837820286008082972804887218672784431700831100544507655659531379747541999999995, 0.00000000000000000278793706563379402178294753790944364927085054500163081379043930650189386849765202169477470552201325772332454726999999995}}; @@ -22,12 +16,16 @@ public class MandelbrotSet { private int width = 1920; private int threshold = 1000; private float ratio = 2 / 3f; - private int threadAmount = 10; + private int threadAmount = Runtime.getRuntime().availableProcessors(); //only declared private int height; private int iterations; + private boolean[] complete; + + private long startTime = System.currentTimeMillis(); + /** * Create a new Mandelbrot set manager @@ -51,6 +49,8 @@ public class MandelbrotSet { case 4 -> 5000; default -> quality; }; + + complete = new boolean[threadAmount]; } public MandelbrotSet(){ @@ -59,6 +59,8 @@ public class MandelbrotSet { public void startMandelbrot() { + System.out.println("Started calculating " + frames + " frame/s"); + double forceCenterX; double forceCenterY; if(pointNumber == -1){ @@ -69,23 +71,14 @@ public class MandelbrotSet { forceCenterY = interestingPoints[pointNumber][1]; } - // TIME - long startTime = System.currentTimeMillis(); - double[][] zoomValues = zoomValues(frames, width, height, forceCenterX, forceCenterY, zoomSpeed, zoom); //create the threads CalculationThread[] threads = new CalculationThread[threadAmount]; for (int i = 0; i < threadAmount; i++) { - threads[i] = new CalculationThread(i, threadAmount, frames, width, height, iterations, threshold, zoomValues); + threads[i] = new CalculationThread(this, i, threadAmount, frames, width, height, iterations, threshold, zoomValues); threads[i].start(); } - - // TIME should probably not be here and serves no practical purpose but that doesn't stop me from keeping it here - long endTime = System.currentTimeMillis(); - long completionTimeLong = endTime - startTime; - double completionTimeSec = (double) completionTimeLong / 1000.0; - System.out.println("Prepared " + frames + " frame/s in " + completionTimeSec + "s"); } @@ -122,6 +115,26 @@ public class MandelbrotSet { return zoomValues; } + public void setFinished(int threadNumber){ + complete[threadNumber] = true; + + boolean finished = true; + for(boolean b : complete){ + if (!b) { + finished = false; + break; + } + } + + if(finished){ + System.out.println("CALCULATION FINISHED"); + // TIME should probably not be here and serves no practical purpose but that doesn't stop me from keeping it here + long endTime = System.currentTimeMillis(); + long completionTimeLong = endTime - startTime; + double completionTimeSec = (double) completionTimeLong / 1000.0; + System.out.println("Calculated " + frames + " frame/s in " + completionTimeSec + "s"); + } + } //SETTER public void setZoom(double zoom) { diff --git a/src/ui/Controller.java b/src/ui/Controller.java index 393b9c9..c8f9749 100644 --- a/src/ui/Controller.java +++ b/src/ui/Controller.java @@ -24,7 +24,7 @@ public class Controller { } public void startCalculation(ActionEvent actionEvent) { - MandelbrotSet m = new MandelbrotSet(2, 3, 3, 10); + MandelbrotSet m = new MandelbrotSet(2, 2, 3, 10); m.startMandelbrot(); } }