Thread now implements runnable instead of extending Thread

This commit is contained in:
nora 2020-11-22 10:30:50 +01:00
parent 97d87fb1ba
commit 580e3826b5
6 changed files with 84 additions and 22 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -1,5 +1,5 @@
.root {
-fx-background-color: #AAAABA;
-fx-background-color: #aaaaba;
}
.label {

View file

@ -3,15 +3,53 @@
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<GridPane alignment="center" hgap="10.0" prefHeight="143.0" prefWidth="107.0" stylesheets="/ui/sample.css" vgap="10" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ui.Controller">
<Button onAction="#startCalculation" prefHeight="25.0" prefWidth="110.0" text="Start" GridPane.rowIndex="1" />
<ImageView fx:id="setPreview" fitWidth="500" preserveRatio="true" />
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
<RowConstraints />
</rowConstraints>
</GridPane>
<AnchorPane prefHeight="225.0" prefWidth="769.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ui.Controller">
<children>
<VBox AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Mandelbrot Calculator">
<font>
<Font size="36.0" />
</font>
</Text>
<HBox prefHeight="435.0" prefWidth="769.0">
<children>
<VBox prefHeight="200.0" prefWidth="100.0">
<children>
<ImageView fx:id="setPreview" fitWidth="500" preserveRatio="true" />
<Button onAction="#startCalculation" prefHeight="25.0" prefWidth="110.0" text="Start" />
</children>
</VBox>
<VBox alignment="TOP_RIGHT">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Output:" />
<TextArea fx:id="output" editable="false" />
</children>
</VBox>
</children>
</HBox>
</children>
</VBox>
</children>
</AnchorPane>