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
No related branches found
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;
import edu.ntnu.idatt2003.model.ChaosGame;
import edu.ntnu.idatt2003.model.ChaosGameDescription;
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.Complex;
import edu.ntnu.idatt2003.model.JuliaTransform;
import edu.ntnu.idatt2003.model.Transform2D;
import edu.ntnu.idatt2003.model.Vector2d;
import edu.ntnu.idatt2003.view.MainPageView;
import edu.ntnu.idatt2003.view.MainPageView.TransformationType;
import java.util.ArrayList;
import java.util.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
......@@ -43,7 +39,6 @@ public class MainPageController {
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 Logger LOGGER = Logger.getLogger(MainPageController.class.getName());
private static int stepsCounter;
static {
try {
......@@ -99,15 +94,10 @@ public class MainPageController {
* @param steps The number of steps to run the simulation.
*/
public void runSteps(int steps) {
game.runSteps(steps);
stepsCounter += steps;
game.runStepsAndUpdateTotal(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
* file exists and is formatted correctly.
......@@ -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() {
LOGGER.log(Level.INFO, "Loading game state.");
File file = new File(SERIALIZED_GAME_PATH);
......@@ -234,7 +230,6 @@ public class MainPageController {
new ChaosGameDescription(minCoords, maxCoords, transform);
chaosGameFileHandler
.writeToFile(newChaosGameDescription, TRANSFORMATIONS_PATH + transformationName + ".txt");
System.out.println(transformationName);
customTransformations.add(transformationName);
view.render();
}
......@@ -266,6 +261,7 @@ public class MainPageController {
list.add(new JuliaTransform(complex, -1));
ChaosGameDescription chaosGameDescription = new ChaosGameDescription(min, max, list);
game.setDescription(chaosGameDescription);
game.runStepsWithoutUpdatingTotal(game.getTotalSteps());
}
}
......
......@@ -20,6 +20,7 @@ public class ChaosGame implements Serializable {
private final int width;
private final int height;
private final List<ChaosGameObserver> observers;
private int totalSteps;
Random randomGenerator;
/**
......@@ -36,6 +37,7 @@ public class ChaosGame implements Serializable {
this.height = height;
setDescription(description);
randomGenerator = new Random();
this.totalSteps = 0;
}
/**
......@@ -48,6 +50,15 @@ public class ChaosGame implements Serializable {
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.
* Generates points on the canvas based on random chosen transformation.
......@@ -55,13 +66,17 @@ public class ChaosGame implements Serializable {
*
* @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) {
this.canvas.clear();
totalSteps = 0;
} else {
for (int i = 0; i < steps; i++) {
applyRandomTransformation();
}
if (addSteps) {
totalSteps += steps;
}
}
notifyObservers();
}
......
......@@ -550,7 +550,6 @@ public class MainPageView extends Scene implements ChaosGameObserver {
*/
private void updateValues(double x, double y) {
controller.changeJuliaTransformationDynamic(x, y);
controller.runSteps(controller.getSteps());
xField.setText(String.format("X: %.1f", x));
yField.setText(String.format("Y: %.1f", y));
}
......
......@@ -49,7 +49,7 @@ public class UI {
runIterations();
break;
case SHOW_CANVAS:
ShowCanvas();
showCanvas();
break;
default:
System.out.println("Invalid choice");
......@@ -159,14 +159,14 @@ public class UI {
}
textRenderer.enterSteps();
int steps = numberInput();
chaosGame.runSteps(steps);
chaosGame.runStepsAndUpdateTotal(steps);
}
/**
* Shows the canvas, and ensures that chaosGame is not null.
*/
public void ShowCanvas() {
public void showCanvas() {
if(chaosGame == null) {
System.out.println("chaosGame is null");
return;
......
......@@ -23,7 +23,7 @@ public class ChaosGameTest {
List.of(transform));
cg = new ChaosGame(description, 100, 100);
cg.runSteps(100);
cg.runStepsAndUpdateTotal(100);
}
@Nested
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment