Skip to content
Snippets Groups Projects
Commit be061b1f authored by Nicklas Persia Tufteland's avatar Nicklas Persia Tufteland
Browse files

Merge branch '26-fix-bug-where-altering-c-in-julia-with-mouse-lags' into 'dev'

Resolve "Fix bug where altering c in Julia with mouse lags"

Closes #26

See merge request !33
parents ac37398a ba6e0321
Branches
No related tags found
2 merge requests!41final delivery,!33Resolve "Fix bug where altering c in Julia with mouse lags"
Pipeline #288191 passed
...@@ -3,18 +3,14 @@ package edu.ntnu.idatt2003.controller; ...@@ -3,18 +3,14 @@ package edu.ntnu.idatt2003.controller;
import edu.ntnu.idatt2003.model.ChaosGame; import edu.ntnu.idatt2003.model.ChaosGame;
import edu.ntnu.idatt2003.model.ChaosGameDescription; import edu.ntnu.idatt2003.model.ChaosGameDescription;
import edu.ntnu.idatt2003.model.ChaosGameDescriptionFactory; import edu.ntnu.idatt2003.model.ChaosGameDescriptionFactory;
import edu.ntnu.idatt2003.model.ChaosGameDescriptionFactory.descriptionTypeEnum;
import edu.ntnu.idatt2003.model.ChaosGameFileHandler;
import edu.ntnu.idatt2003.model.ChaosGameFileHandler; import edu.ntnu.idatt2003.model.ChaosGameFileHandler;
import edu.ntnu.idatt2003.model.Complex; import edu.ntnu.idatt2003.model.Complex;
import edu.ntnu.idatt2003.model.JuliaTransform; import edu.ntnu.idatt2003.model.JuliaTransform;
import edu.ntnu.idatt2003.model.Transform2D; import edu.ntnu.idatt2003.model.Transform2D;
import edu.ntnu.idatt2003.model.Vector2d; import edu.ntnu.idatt2003.model.Vector2d;
import edu.ntnu.idatt2003.view.MainPageView; import edu.ntnu.idatt2003.view.MainPageView;
import edu.ntnu.idatt2003.view.MainPageView.TransformationType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
...@@ -43,7 +39,6 @@ public class MainPageController { ...@@ -43,7 +39,6 @@ public class MainPageController {
private static final String TRANSFORMATIONS_PATH = "src/main/resources/transformations/"; private static final String TRANSFORMATIONS_PATH = "src/main/resources/transformations/";
private static final String SERIALIZED_GAME_PATH = "src/main/resources/savedTransformation.ser"; private static final String SERIALIZED_GAME_PATH = "src/main/resources/savedTransformation.ser";
private static final Logger LOGGER = Logger.getLogger(MainPageController.class.getName()); private static final Logger LOGGER = Logger.getLogger(MainPageController.class.getName());
private static int stepsCounter;
static { static {
try { try {
...@@ -99,15 +94,10 @@ public class MainPageController { ...@@ -99,15 +94,10 @@ public class MainPageController {
* @param steps The number of steps to run the simulation. * @param steps The number of steps to run the simulation.
*/ */
public void runSteps(int steps) { public void runSteps(int steps) {
game.runSteps(steps); game.runStepsAndUpdateTotal(steps);
stepsCounter += steps;
LOGGER.log(Level.INFO, "Chaos game simulation ran {0} steps successfully.", steps); LOGGER.log(Level.INFO, "Chaos game simulation ran {0} steps successfully.", steps);
} }
public int getSteps() {
return stepsCounter;
}
/** /**
* Validates the file that is uploaded., and calls storeFile if the * Validates the file that is uploaded., and calls storeFile if the
* file exists and is formatted correctly. * file exists and is formatted correctly.
...@@ -177,6 +167,12 @@ public class MainPageController { ...@@ -177,6 +167,12 @@ public class MainPageController {
} }
} }
/**
* Load the game state from the serialized file to restore progress.
* If the file does not exist, a new game state is created.
*
* @return The loaded game state, or a new game state if the file does not exist.
*/
public ChaosGame loadGameState() { public ChaosGame loadGameState() {
LOGGER.log(Level.INFO, "Loading game state."); LOGGER.log(Level.INFO, "Loading game state.");
File file = new File(SERIALIZED_GAME_PATH); File file = new File(SERIALIZED_GAME_PATH);
...@@ -234,7 +230,6 @@ public class MainPageController { ...@@ -234,7 +230,6 @@ public class MainPageController {
new ChaosGameDescription(minCoords, maxCoords, transform); new ChaosGameDescription(minCoords, maxCoords, transform);
chaosGameFileHandler chaosGameFileHandler
.writeToFile(newChaosGameDescription, TRANSFORMATIONS_PATH + transformationName + ".txt"); .writeToFile(newChaosGameDescription, TRANSFORMATIONS_PATH + transformationName + ".txt");
System.out.println(transformationName);
customTransformations.add(transformationName); customTransformations.add(transformationName);
view.render(); view.render();
} }
...@@ -266,6 +261,7 @@ public class MainPageController { ...@@ -266,6 +261,7 @@ public class MainPageController {
list.add(new JuliaTransform(complex, -1)); list.add(new JuliaTransform(complex, -1));
ChaosGameDescription chaosGameDescription = new ChaosGameDescription(min, max, list); ChaosGameDescription chaosGameDescription = new ChaosGameDescription(min, max, list);
game.setDescription(chaosGameDescription); game.setDescription(chaosGameDescription);
game.runStepsWithoutUpdatingTotal(game.getTotalSteps());
} }
} }
......
...@@ -20,6 +20,7 @@ public class ChaosGame implements Serializable { ...@@ -20,6 +20,7 @@ public class ChaosGame implements Serializable {
private final int width; private final int width;
private final int height; private final int height;
private final List<ChaosGameObserver> observers; private final List<ChaosGameObserver> observers;
private int totalSteps;
Random randomGenerator; Random randomGenerator;
/** /**
...@@ -36,6 +37,7 @@ public class ChaosGame implements Serializable { ...@@ -36,6 +37,7 @@ public class ChaosGame implements Serializable {
this.height = height; this.height = height;
setDescription(description); setDescription(description);
randomGenerator = new Random(); randomGenerator = new Random();
this.totalSteps = 0;
} }
/** /**
...@@ -48,6 +50,15 @@ public class ChaosGame implements Serializable { ...@@ -48,6 +50,15 @@ public class ChaosGame implements Serializable {
return canvas; return canvas;
} }
public int getTotalSteps() {
return totalSteps;
}
public void runStepsAndUpdateTotal(int steps) {
runSteps(steps, true);
}
public void runStepsWithoutUpdatingTotal(int steps) {
runSteps(steps, false);
}
/** /**
* Runs the chaos game simulation for the specified number of steps. * Runs the chaos game simulation for the specified number of steps.
* Generates points on the canvas based on random chosen transformation. * Generates points on the canvas based on random chosen transformation.
...@@ -55,13 +66,17 @@ public class ChaosGame implements Serializable { ...@@ -55,13 +66,17 @@ public class ChaosGame implements Serializable {
* *
* @param steps The number of steps to run the simulation. * @param steps The number of steps to run the simulation.
*/ */
public void runSteps(int steps) { private void runSteps(int steps, boolean addSteps) {
if (steps < 0) { if (steps < 0) {
this.canvas.clear(); this.canvas.clear();
totalSteps = 0;
} else { } else {
for (int i = 0; i < steps; i++) { for (int i = 0; i < steps; i++) {
applyRandomTransformation(); applyRandomTransformation();
} }
if (addSteps) {
totalSteps += steps;
}
} }
notifyObservers(); notifyObservers();
} }
......
...@@ -550,7 +550,6 @@ public class MainPageView extends Scene implements ChaosGameObserver { ...@@ -550,7 +550,6 @@ public class MainPageView extends Scene implements ChaosGameObserver {
*/ */
private void updateValues(double x, double y) { private void updateValues(double x, double y) {
controller.changeJuliaTransformationDynamic(x, y); controller.changeJuliaTransformationDynamic(x, y);
controller.runSteps(controller.getSteps());
xField.setText(String.format("X: %.1f", x)); xField.setText(String.format("X: %.1f", x));
yField.setText(String.format("Y: %.1f", y)); yField.setText(String.format("Y: %.1f", y));
} }
......
...@@ -49,7 +49,7 @@ public class UI { ...@@ -49,7 +49,7 @@ public class UI {
runIterations(); runIterations();
break; break;
case SHOW_CANVAS: case SHOW_CANVAS:
ShowCanvas(); showCanvas();
break; break;
default: default:
System.out.println("Invalid choice"); System.out.println("Invalid choice");
...@@ -159,14 +159,14 @@ public class UI { ...@@ -159,14 +159,14 @@ public class UI {
} }
textRenderer.enterSteps(); textRenderer.enterSteps();
int steps = numberInput(); int steps = numberInput();
chaosGame.runSteps(steps); chaosGame.runStepsAndUpdateTotal(steps);
} }
/** /**
* Shows the canvas, and ensures that chaosGame is not null. * Shows the canvas, and ensures that chaosGame is not null.
*/ */
public void ShowCanvas() { public void showCanvas() {
if(chaosGame == null) { if(chaosGame == null) {
System.out.println("chaosGame is null"); System.out.println("chaosGame is null");
return; return;
......
...@@ -23,7 +23,7 @@ public class ChaosGameTest { ...@@ -23,7 +23,7 @@ public class ChaosGameTest {
List.of(transform)); List.of(transform));
cg = new ChaosGame(description, 100, 100); cg = new ChaosGame(description, 100, 100);
cg.runSteps(100); cg.runStepsAndUpdateTotal(100);
} }
@Nested @Nested
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment