From 580e3826b55b2bd217df84cbd5b96f285775db8d Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Sun, 22 Nov 2020 10:30:50 +0100 Subject: [PATCH] Thread now implements runnable instead of extending Thread --- .../CalculationThread.java | 25 ++++++-- src/mandelbrotCalculator/MandelbrotSet.java | 7 ++- src/ui/Controller.java | 11 +++- src/ui/Main.java | 1 + src/ui/sample.css | 2 +- src/ui/sample.fxml | 60 +++++++++++++++---- 6 files changed, 84 insertions(+), 22 deletions(-) diff --git a/src/mandelbrotCalculator/CalculationThread.java b/src/mandelbrotCalculator/CalculationThread.java index b02b264..600085f 100644 --- a/src/mandelbrotCalculator/CalculationThread.java +++ b/src/mandelbrotCalculator/CalculationThread.java @@ -1,12 +1,17 @@ package mandelbrotCalculator; +import ui.Controller; +import util.Values; + import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; -public class CalculationThread extends Thread { +public class CalculationThread implements Runnable { + + private Thread thread; int threadNumber; int threadAmount; @@ -19,7 +24,16 @@ public class CalculationThread extends Thread { CNumber[][] samples; MandelbrotSet set; + Controller controller; + public void start(){ + if(thread == null){ + thread = new Thread(this, String.valueOf(threadNumber)); + thread.start(); + } + } + + @Override public void run() { long totalStartTime = System.currentTimeMillis(); @@ -54,18 +68,21 @@ public class CalculationThread extends Thread { long frameTime = System.currentTimeMillis() - startTime; System.out.println("Frame " + frameCounter + " finished in " + ((double) frameTime / 1000) + "s"); + controller.printOutput("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"); + controller.printOutput("--Thread " + threadNumber + " completed. Process took " + ((double) totalTime / 1000) + "s"); set.setFinished(threadNumber); } - public CalculationThread(MandelbrotSet set, int number, int threads, int frames, int widthC, int heightC, int iterationsC, int thresholdC, double[][] zoomValuesC) { + public CalculationThread(Controller controller, MandelbrotSet set, int number, int threads, int frames, int widthC, int heightC, int iterationsC, int thresholdC, double[][] zoomValuesC) { this.set = set; + this.controller = controller; this.threadNumber = number; this.threadAmount = threads; this.frameAmount = frames; @@ -78,7 +95,7 @@ public class CalculationThread extends Thread { /** * Creates an image from the calculated values - * @param image the image to be used + * @param image the image to be used * @param counter the frame number of the image * @param width width of the image * @param height height of the image @@ -105,7 +122,7 @@ public class CalculationThread extends Thread { } } try { - File f = new File("C:\\Users\\nilsh\\Desktop\\testordner/image" + counter + ".png"); + File f = new File(Values.SAVE_IMAGE_PATH + counter + ".png"); ImageIO.write(image, "png", f); } catch (IOException e) { e.printStackTrace(); diff --git a/src/mandelbrotCalculator/MandelbrotSet.java b/src/mandelbrotCalculator/MandelbrotSet.java index 7904214..e59363b 100644 --- a/src/mandelbrotCalculator/MandelbrotSet.java +++ b/src/mandelbrotCalculator/MandelbrotSet.java @@ -50,6 +50,7 @@ public class MandelbrotSet { height = (int) ((float) width * ratio); iterations = switch (quality){ + case -1 -> 10; case 0 -> 50; case 1 -> 100; case 2 -> 500; @@ -85,7 +86,7 @@ public class MandelbrotSet { //create the threads CalculationThread[] threads = new CalculationThread[threadAmount]; for (int i = 0; i < threadAmount; i++) { - threads[i] = new CalculationThread(this, i, threadAmount, frames, width, height, iterations, threshold, zoomValues); + threads[i] = new CalculationThread(controller, this, i, threadAmount, frames, width, height, iterations, threshold, zoomValues); threads[i].start(); } } @@ -138,8 +139,6 @@ public class MandelbrotSet { } } - System.err.println(latestFrame); - try { controller.nextImage(latestFrame); } catch (FileNotFoundException e) { @@ -164,11 +163,13 @@ public class MandelbrotSet { if(finished){ System.out.println("CALCULATION FINISHED"); + controller.printOutput("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"); + controller.printOutput("Calculated " + frames + " frame/s in " + completionTimeSec + "s"); } } diff --git a/src/ui/Controller.java b/src/ui/Controller.java index c104498..9477aa4 100644 --- a/src/ui/Controller.java +++ b/src/ui/Controller.java @@ -1,6 +1,6 @@ package ui; -import javafx.event.ActionEvent; +import javafx.scene.control.TextArea; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import mandelbrotCalculator.MandelbrotSet; @@ -13,6 +13,7 @@ import java.io.InputStream; public class Controller { public ImageView setPreview; + public TextArea output; public void nextImage(int frameNumber) throws FileNotFoundException { @@ -21,8 +22,12 @@ public class Controller { setPreview.setImage(image); } - public void startCalculation(ActionEvent actionEvent) { - MandelbrotSet m = new MandelbrotSet(2, 1, 3, 100, this); + public void startCalculation() { + MandelbrotSet m = new MandelbrotSet(2, -1, 2, 100, this); m.startMandelbrot(); } + + public void printOutput(String s){ + output.appendText(String.format("%n%s", s)); + } } diff --git a/src/ui/Main.java b/src/ui/Main.java index 69dd4fd..d3dc0b1 100644 --- a/src/ui/Main.java +++ b/src/ui/Main.java @@ -14,6 +14,7 @@ public class Main extends Application { Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); primaryStage.setTitle("Mandelbrot"); primaryStage.setScene(new Scene(root, 500, 300)); + primaryStage.setMaximized(true); primaryStage.show(); } diff --git a/src/ui/sample.css b/src/ui/sample.css index a85d89c..18341aa 100644 --- a/src/ui/sample.css +++ b/src/ui/sample.css @@ -1,5 +1,5 @@ .root { - -fx-background-color: #AAAABA; + -fx-background-color: #aaaaba; } .label { diff --git a/src/ui/sample.fxml b/src/ui/sample.fxml index 851a68b..8d22d90 100644 --- a/src/ui/sample.fxml +++ b/src/ui/sample.fxml @@ -3,15 +3,53 @@ + - -