diff --git a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/models/UserInterface.java b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/models/UserInterface.java new file mode 100644 index 0000000000000000000000000000000000000000..710a07c757411ee532fb1eccf768a2654398489e --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/models/UserInterface.java @@ -0,0 +1,96 @@ +package edu.ntnu.idatt2003.mappevurderingprog2.models; + +import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGame; +import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGameDescription; +import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGameFileHandler; +import java.util.Scanner; + +public class UserInterface { + private ChaosGameDescription description; + private ChaosGame game; + private final ChaosGameFileHandler fileHandler = new ChaosGameFileHandler(); + private final Scanner scanner = new Scanner(System.in); + public void start() { + System.out.println("CLI Application for Chaos Game"); + while (true) { + System.out.println("\n1. Read description from file"); + System.out.println("2. Write description to file"); + System.out.println("3. Run a number of iterations"); + System.out.println("4. Print ASCII fractal to console"); + System.out.println("5. Exit"); + System.out.print("Enter choice: "); + int choice = scanner.nextInt(); + scanner.nextLine(); + + switch (choice) { + case 1: + readDescriptionFromFile(); + break; + case 2: + writeDescriptionToFile(); + break; + case 3: + runIterations(); + break; + case 4: + printFractal(); + break; + case 5: + System.out.println("Exiting..."); + return; + default: + System.out.println("Invalid choice. Please try again."); + } + } + } + + private void readDescriptionFromFile() { + System.out.print("Enter the name of the text file (e.g., JuliaTransformation.txt): "); + String fileName = scanner.nextLine(); + String filePath = "src/main/resources/transformations/" + fileName; + + try { + description = fileHandler.readTransformationsFromFile(filePath); + System.out.println("Description read successfully from: " + filePath); + } catch (Exception e) { + System.out.println("Error reading file: " + e.getMessage()); + } + } + + private void writeDescriptionToFile() { + if (description == null) { + System.out.println("No description to write. Please read a description first."); + return; + } + System.out.print("Enter the name of the text file to write to (e.g., JuliaTransformation.txt): "); + String fileName = scanner.nextLine(); + String filePath = "src/main/resources/transformations/" + fileName; + + try { + fileHandler.writeTransformationsToFile(description, filePath); + System.out.println("Description written successfully to: " + filePath); + } catch (Exception e) { + System.out.println("Error writing to file: " + e.getMessage()); + } + } + + private void runIterations() { + if (description == null) { + System.out.println("No description loaded. Please read a description first."); + return; + } + System.out.print("Enter the number of iterations to run: "); + int iterations = scanner.nextInt(); + game = new ChaosGame(description, 50, 50); + game.runSteps(iterations); + System.out.println("Iterations completed."); + } + + private void printFractal() { + if (game == null || game.getCanvas() == null) { + System.out.println("No game to print. Please run iterations first."); + return; + } + game.getCanvas().printCanvas(); + } +} \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/models/chaos/ChaosGame.java b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/models/chaos/ChaosGame.java index 201c9d2c9247fde7a0631c768089488d34c83443..705aa751f92292e0d31b570287df427517780307 100644 --- a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/models/chaos/ChaosGame.java +++ b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/models/chaos/ChaosGame.java @@ -5,6 +5,7 @@ import edu.ntnu.idatt2003.mappevurderingprog2.models.Complex; import edu.ntnu.idatt2003.mappevurderingprog2.models.JuliaTransform; import edu.ntnu.idatt2003.mappevurderingprog2.models.Matrix2x2; import edu.ntnu.idatt2003.mappevurderingprog2.models.Transform2D; +import edu.ntnu.idatt2003.mappevurderingprog2.models.UserInterface; import edu.ntnu.idatt2003.mappevurderingprog2.models.Vector2D; import java.util.ArrayList; import java.util.List; @@ -41,19 +42,9 @@ public class ChaosGame { } } - public static void main(String[] args) { - try { - String filePath = "src/main/resources/transformations/JuliaTransformation.txt"; - ChaosGameDescription descriptionToWrite = createJuliaChaosGameDescription(); - ChaosGameFileHandler fileHandler = new ChaosGameFileHandler(); - fileHandler.writeTransformationsToFile(descriptionToWrite, filePath); - ChaosGameDescription descriptionRead = fileHandler.readTransformationsFromFile(filePath); - ChaosGame game = new ChaosGame(descriptionRead, 200, 200); - game.runSteps(1000000); - game.getCanvas().printCanvas(); - } catch (Exception e) { - e.printStackTrace(); - } + public static void main(String[] args) throws Exception { + UserInterface ui = new UserInterface(); + ui.start(); } private static ChaosGameDescription createAffineChaosGameDescription() { diff --git a/src/main/resources/transformations/BarnsleyFern.txt b/src/main/resources/transformations/BarnsleyFern.txt new file mode 100644 index 0000000000000000000000000000000000000000..647a7a0b44406f0aacef5788d039dcc120639504 --- /dev/null +++ b/src/main/resources/transformations/BarnsleyFern.txt @@ -0,0 +1,7 @@ +Affine2D +0.0, 0.0 +1.0, 1.0 +0.000000, 0.000000, 0.000000, 0.160000, 0.0, 0.0 +0.850000, 0.040000, -0.040000, 0.850000, 0.0, 1.6 +0.200000, -0.260000, 0.230000, 0.220000, 0.0, 1.6 +-0.150000, 0.280000, 0.260000, 0.240000, 0.0, 0.44 \ No newline at end of file diff --git a/src/main/resources/transformations/AffineTransformation.txt b/src/main/resources/transformations/SierpinskiTriangle.txt similarity index 100% rename from src/main/resources/transformations/AffineTransformation.txt rename to src/main/resources/transformations/SierpinskiTriangle.txt