mirror of
https://github.com/Noratrieb/mandelbrotGUI.git
synced 2026-01-16 16:25:06 +01:00
Thread now implements runnable instead of extending Thread
This commit is contained in:
parent
97d87fb1ba
commit
580e3826b5
6 changed files with 84 additions and 22 deletions
|
|
@ -1,12 +1,17 @@
|
||||||
package mandelbrotCalculator;
|
package mandelbrotCalculator;
|
||||||
|
|
||||||
|
import ui.Controller;
|
||||||
|
import util.Values;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class CalculationThread extends Thread {
|
public class CalculationThread implements Runnable {
|
||||||
|
|
||||||
|
private Thread thread;
|
||||||
|
|
||||||
int threadNumber;
|
int threadNumber;
|
||||||
int threadAmount;
|
int threadAmount;
|
||||||
|
|
@ -19,7 +24,16 @@ public class CalculationThread extends Thread {
|
||||||
CNumber[][] samples;
|
CNumber[][] samples;
|
||||||
|
|
||||||
MandelbrotSet set;
|
MandelbrotSet set;
|
||||||
|
Controller controller;
|
||||||
|
|
||||||
|
public void start(){
|
||||||
|
if(thread == null){
|
||||||
|
thread = new Thread(this, String.valueOf(threadNumber));
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
long totalStartTime = System.currentTimeMillis();
|
long totalStartTime = System.currentTimeMillis();
|
||||||
|
|
@ -54,18 +68,21 @@ public class CalculationThread extends Thread {
|
||||||
|
|
||||||
long frameTime = System.currentTimeMillis() - startTime;
|
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");
|
||||||
|
controller.printOutput("Frame " + frameCounter + " finished in " + ((double) frameTime / 1000) + "s");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
long totalTime = System.currentTimeMillis() - totalStartTime;
|
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");
|
||||||
|
controller.printOutput("--Thread " + threadNumber + " completed. Process took " + ((double) totalTime / 1000) + "s");
|
||||||
|
|
||||||
set.setFinished(threadNumber);
|
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.set = set;
|
||||||
|
this.controller = controller;
|
||||||
this.threadNumber = number;
|
this.threadNumber = number;
|
||||||
this.threadAmount = threads;
|
this.threadAmount = threads;
|
||||||
this.frameAmount = frames;
|
this.frameAmount = frames;
|
||||||
|
|
@ -105,7 +122,7 @@ public class CalculationThread extends Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
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);
|
ImageIO.write(image, "png", f);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,7 @@ public class MandelbrotSet {
|
||||||
height = (int) ((float) width * ratio);
|
height = (int) ((float) width * ratio);
|
||||||
|
|
||||||
iterations = switch (quality){
|
iterations = switch (quality){
|
||||||
|
case -1 -> 10;
|
||||||
case 0 -> 50;
|
case 0 -> 50;
|
||||||
case 1 -> 100;
|
case 1 -> 100;
|
||||||
case 2 -> 500;
|
case 2 -> 500;
|
||||||
|
|
@ -85,7 +86,7 @@ public class MandelbrotSet {
|
||||||
//create the threads
|
//create the threads
|
||||||
CalculationThread[] threads = new CalculationThread[threadAmount];
|
CalculationThread[] threads = new CalculationThread[threadAmount];
|
||||||
for (int i = 0; i < threadAmount; i++) {
|
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();
|
threads[i].start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -138,8 +139,6 @@ public class MandelbrotSet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.err.println(latestFrame);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
controller.nextImage(latestFrame);
|
controller.nextImage(latestFrame);
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
|
|
@ -164,11 +163,13 @@ public class MandelbrotSet {
|
||||||
|
|
||||||
if(finished){
|
if(finished){
|
||||||
System.out.println("CALCULATION 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
|
// 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 endTime = System.currentTimeMillis();
|
||||||
long completionTimeLong = endTime - startTime;
|
long completionTimeLong = endTime - startTime;
|
||||||
double completionTimeSec = (double) completionTimeLong / 1000.0;
|
double completionTimeSec = (double) completionTimeLong / 1000.0;
|
||||||
System.out.println("Calculated " + frames + " frame/s in " + completionTimeSec + "s");
|
System.out.println("Calculated " + frames + " frame/s in " + completionTimeSec + "s");
|
||||||
|
controller.printOutput("Calculated " + frames + " frame/s in " + completionTimeSec + "s");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package ui;
|
package ui;
|
||||||
|
|
||||||
import javafx.event.ActionEvent;
|
import javafx.scene.control.TextArea;
|
||||||
import javafx.scene.image.Image;
|
import javafx.scene.image.Image;
|
||||||
import javafx.scene.image.ImageView;
|
import javafx.scene.image.ImageView;
|
||||||
import mandelbrotCalculator.MandelbrotSet;
|
import mandelbrotCalculator.MandelbrotSet;
|
||||||
|
|
@ -13,6 +13,7 @@ import java.io.InputStream;
|
||||||
public class Controller {
|
public class Controller {
|
||||||
|
|
||||||
public ImageView setPreview;
|
public ImageView setPreview;
|
||||||
|
public TextArea output;
|
||||||
|
|
||||||
public void nextImage(int frameNumber) throws FileNotFoundException {
|
public void nextImage(int frameNumber) throws FileNotFoundException {
|
||||||
|
|
||||||
|
|
@ -21,8 +22,12 @@ public class Controller {
|
||||||
setPreview.setImage(image);
|
setPreview.setImage(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startCalculation(ActionEvent actionEvent) {
|
public void startCalculation() {
|
||||||
MandelbrotSet m = new MandelbrotSet(2, 1, 3, 100, this);
|
MandelbrotSet m = new MandelbrotSet(2, -1, 2, 100, this);
|
||||||
m.startMandelbrot();
|
m.startMandelbrot();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void printOutput(String s){
|
||||||
|
output.appendText(String.format("%n%s", s));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ public class Main extends Application {
|
||||||
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
|
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
|
||||||
primaryStage.setTitle("Mandelbrot");
|
primaryStage.setTitle("Mandelbrot");
|
||||||
primaryStage.setScene(new Scene(root, 500, 300));
|
primaryStage.setScene(new Scene(root, 500, 300));
|
||||||
|
primaryStage.setMaximized(true);
|
||||||
primaryStage.show();
|
primaryStage.show();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
.root {
|
.root {
|
||||||
-fx-background-color: #AAAABA;
|
-fx-background-color: #aaaaba;
|
||||||
}
|
}
|
||||||
|
|
||||||
.label {
|
.label {
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,53 @@
|
||||||
<?import javafx.scene.control.*?>
|
<?import javafx.scene.control.*?>
|
||||||
<?import javafx.scene.image.*?>
|
<?import javafx.scene.image.*?>
|
||||||
<?import javafx.scene.layout.*?>
|
<?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">
|
<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">
|
||||||
<Button onAction="#startCalculation" prefHeight="25.0" prefWidth="110.0" text="Start" GridPane.rowIndex="1" />
|
<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" />
|
<ImageView fx:id="setPreview" fitWidth="500" preserveRatio="true" />
|
||||||
<columnConstraints>
|
<Button onAction="#startCalculation" prefHeight="25.0" prefWidth="110.0" text="Start" />
|
||||||
<ColumnConstraints />
|
</children>
|
||||||
</columnConstraints>
|
</VBox>
|
||||||
<rowConstraints>
|
<VBox alignment="TOP_RIGHT">
|
||||||
<RowConstraints />
|
<children>
|
||||||
<RowConstraints />
|
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Output:" />
|
||||||
</rowConstraints>
|
<TextArea fx:id="output" editable="false" />
|
||||||
</GridPane>
|
</children>
|
||||||
|
</VBox>
|
||||||
|
</children>
|
||||||
|
</HBox>
|
||||||
|
</children>
|
||||||
|
</VBox>
|
||||||
|
</children>
|
||||||
|
</AnchorPane>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue