From 9d68fe15214f06c9b692779f10cd03cb60f0ce64 Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Sun, 22 Nov 2020 11:30:53 +0100 Subject: [PATCH] Parameters in GUI and stopping the program/calculation --- .../CalculationThread.java | 63 +++++++++---------- src/mandelbrotCalculator/MandelbrotSet.java | 35 ++++++----- src/ui/Controller.java | 33 ++++++++-- src/ui/sample.fxml | 38 ++++++++++- src/util/Values.java | 2 +- 5 files changed, 114 insertions(+), 57 deletions(-) diff --git a/src/mandelbrotCalculator/CalculationThread.java b/src/mandelbrotCalculator/CalculationThread.java index 42d40ef..3c5d2df 100644 --- a/src/mandelbrotCalculator/CalculationThread.java +++ b/src/mandelbrotCalculator/CalculationThread.java @@ -11,26 +11,26 @@ import java.io.IOException; public class CalculationThread implements Runnable { + private boolean running; private Thread thread; - int threadNumber; - int threadAmount; - int frameAmount; - int width; - int height; - int iterations; - int threshold; - double[][] zoomValues; - CNumber[][] samples; + private int threadNumber; + private int threadAmount; + private int frameAmount; + private int width; + private int height; + private int iterations; + private int threshold; + private double[][] zoomValues; + private CNumber[][] samples; - final MandelbrotSet set; - final Controller controller; + private final MandelbrotSet set; + private final Controller controller; public void start() { if (thread == null) { - synchronized (controller){ - controller.printOutput("Thread " + threadNumber + " started"); - } + controller.printOutput("Thread " + threadNumber + " started"); + thread = new Thread(this, String.valueOf(threadNumber)); thread.start(); } @@ -38,7 +38,7 @@ public class CalculationThread implements Runnable { @Override public void run() { - + running = true; long totalStartTime = System.currentTimeMillis(); samples = new CNumber[width][height]; @@ -60,37 +60,31 @@ public class CalculationThread implements Runnable { // calculate values double[][] values = new double[width][height]; // new array of booleans for the drawing for (int i = 0; i < width; i++) { - for (int j = 0; j < height; j++) { - values[i][j] = checkMandelbrot(samples[i][j], iterations, threshold); // check if the number is inside of th set + if (running) { + for (int j = 0; j < height; j++) { + values[i][j] = checkMandelbrot(samples[i][j], iterations, threshold); // check if the number is inside of th set + } + } else { + controller.printOutput("Thread " + threadNumber + " stopped"); + set.setFinished(threadNumber); + return; } } createImage(image, frameCounter, width, height, values, iterations); - - synchronized (set) { - set.frameFinished(frameCounter); - } + set.frameFinished(frameCounter); long frameTime = System.currentTimeMillis() - startTime; System.out.println("Frame " + frameCounter + " finished in " + ((double) frameTime / 1000) + "s"); - synchronized (controller) { - controller.printOutput("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"); - synchronized (controller) { - controller.printOutput("--Thread " + threadNumber + " completed. Process took " + ((double) totalTime / 1000) + "s"); - } - - synchronized (set) { - set.setFinished(threadNumber); - } - + controller.printOutput("--Thread " + threadNumber + " completed. Process took " + ((double) totalTime / 1000) + "s"); + set.setFinished(threadNumber); } public CalculationThread(Controller controller, MandelbrotSet set, int number, int threads, int frames, int widthC, int heightC, int iterationsC, int thresholdC, double[][] zoomValuesC) { @@ -173,4 +167,7 @@ public class CalculationThread implements Runnable { return (a << 24) | (r << 16) | (g << 8) | b; } + public void stop() { + running = false; + } } diff --git a/src/mandelbrotCalculator/MandelbrotSet.java b/src/mandelbrotCalculator/MandelbrotSet.java index 8395173..86d5a22 100644 --- a/src/mandelbrotCalculator/MandelbrotSet.java +++ b/src/mandelbrotCalculator/MandelbrotSet.java @@ -28,6 +28,7 @@ public class MandelbrotSet { private boolean[] completeThreads; private boolean[] completeFrames; + private CalculationThread[] threads; private long startTime = System.currentTimeMillis(); @@ -50,12 +51,12 @@ public class MandelbrotSet { height = (int) ((float) width * ratio); iterations = switch (quality){ - case -1 -> 10; - case 0 -> 50; - case 1 -> 100; - case 2 -> 500; - case 3 -> 1000; - case 4 -> 5000; + case 0 -> 10; + case 1 -> 50; + case 2 -> 100; + case 3 -> 500; + case 4 -> 1000; + case 5 -> 5000; default -> quality; }; @@ -84,7 +85,7 @@ public class MandelbrotSet { double[][] zoomValues = zoomValues(frames, width, height, forceCenterX, forceCenterY, zoomSpeed, zoom); //create the threads - CalculationThread[] threads = new CalculationThread[threadAmount]; + threads = new CalculationThread[threadAmount]; for (int i = 0; i < threadAmount; i++) { threads[i] = new CalculationThread(controller, this, i, threadAmount, frames, width, height, iterations, threshold, zoomValues); threads[i].start(); @@ -129,7 +130,7 @@ public class MandelbrotSet { * Call when a new frame is finished * @param frameNumber The number of the frame */ - public void frameFinished(int frameNumber){ + public synchronized void frameFinished(int frameNumber){ completeFrames[frameNumber] = true; int latestFrame = 0; @@ -150,7 +151,7 @@ public class MandelbrotSet { * Call when a new Thread is finished * @param threadNumber The Threadier */ - public void setFinished(int threadNumber){ + public synchronized void setFinished(int threadNumber){ completeThreads[threadNumber] = true; boolean finished = true; @@ -163,17 +164,19 @@ public class MandelbrotSet { if(finished){ System.out.println("CALCULATION FINISHED"); - synchronized (controller) { - 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 + controller.printOutput("CALCULATION FINISHED"); + long endTime = System.currentTimeMillis(); long completionTimeLong = endTime - startTime; double completionTimeSec = (double) completionTimeLong / 1000.0; System.out.println("Calculated " + frames + " frame/s in " + completionTimeSec + "s"); - synchronized (controller) { - controller.printOutput("Calculated " + frames + " frame/s in " + completionTimeSec + "s"); - } + controller.printOutput("Calculated " + frames + " frame/s in " + completionTimeSec + "s"); + } + } + + public void stop(){ + for(CalculationThread c : threads){ + c.stop(); } } diff --git a/src/ui/Controller.java b/src/ui/Controller.java index 9477aa4..fc13008 100644 --- a/src/ui/Controller.java +++ b/src/ui/Controller.java @@ -1,6 +1,8 @@ package ui; +import javafx.event.ActionEvent; import javafx.scene.control.TextArea; +import javafx.scene.control.TextField; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import mandelbrotCalculator.MandelbrotSet; @@ -14,6 +16,12 @@ public class Controller { public ImageView setPreview; public TextArea output; + public TextField pointNumberField; + public TextField qualityField; + public TextField zoomSpeedField; + public TextField frameAmountField; + + private MandelbrotSet m; public void nextImage(int frameNumber) throws FileNotFoundException { @@ -21,13 +29,30 @@ public class Controller { Image image = new Image(stream); setPreview.setImage(image); } - public void startCalculation() { - MandelbrotSet m = new MandelbrotSet(2, -1, 2, 100, this); - m.startMandelbrot(); + String pointNumber = pointNumberField.getText(); + String quality = qualityField.getText(); + String zoomSpeed = zoomSpeedField.getText(); + String frames = frameAmountField.getText(); + + try{ + m = new MandelbrotSet(Integer.parseInt(pointNumber), Integer.parseInt(quality), Double.parseDouble(zoomSpeed), Integer.parseInt(frames), this); + m.startMandelbrot(); + } catch (NumberFormatException ignored) { + } + } - public void printOutput(String s){ + public void stop(){ + m.stop(); + } + + public void exitProgram() { + System.exit(0); + } + + public synchronized void printOutput(String s){ output.appendText(String.format("%n%s", s)); } + } diff --git a/src/ui/sample.fxml b/src/ui/sample.fxml index 8d22d90..4c53cfd 100644 --- a/src/ui/sample.fxml +++ b/src/ui/sample.fxml @@ -5,7 +5,7 @@ - + @@ -35,10 +35,42 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + +