diff --git a/src/main/java/edu/ntnu/idatt2003/controller/ButtonObserver.java b/src/main/java/edu/ntnu/idatt2003/controller/ButtonObserver.java index 55755a601ede16da0aebea3858a11291a02049de..8f5030c37857e328cd7aec8b6de233b19c84d733 100644 --- a/src/main/java/edu/ntnu/idatt2003/controller/ButtonObserver.java +++ b/src/main/java/edu/ntnu/idatt2003/controller/ButtonObserver.java @@ -73,6 +73,18 @@ public class ButtonObserver implements Observer{ fractalOperations.setFractalCustomAffine(file.getPath()); } break; + + case "btnSaveFractalToFile": + FileChooser fileChooserSave = new FileChooser(); + fileChooserSave.setTitle("Save Fractal To File"); + File fileSave = fileChooserSave.showSaveDialog(new Stage()); + if (fileSave != null) { + fractalOperations.saveFractalToFile(fileSave.getPath()); + } + break; + + default: + break; } } } \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idatt2003/view/components/Buttons.java b/src/main/java/edu/ntnu/idatt2003/view/components/Buttons.java index e59def285d6f1ca47a68097a9368ff91fcf38bec..e15d4d0af0a86946cfcaebaf3e92783f7ed9a4f1 100644 --- a/src/main/java/edu/ntnu/idatt2003/view/components/Buttons.java +++ b/src/main/java/edu/ntnu/idatt2003/view/components/Buttons.java @@ -14,6 +14,7 @@ public class Buttons extends Subject { private Button btnReset; private Button btnAddInput; private Button btnAddFractalFromFile; + private Button btnSaveFractalToFile; private Button btnExitApplication; public Buttons() { @@ -62,6 +63,12 @@ public class Buttons extends Subject { System.out.println("Exiting Application..."); notifyObservers("btnExitApplication"); }); + + btnSaveFractalToFile = new Button("Save Fractal To File"); + btnSaveFractalToFile.setOnAction(e -> { + System.out.println("Save Fractal To File"); + notifyObservers("btnSaveFractalToFile"); + }); } public Button getBtnAdd10() { @@ -88,5 +95,8 @@ public class Buttons extends Subject { public Button getBtnExitApplication() { return btnExitApplication; } + public Button getBtnSaveFractalToFile() { + return btnSaveFractalToFile; + } } diff --git a/src/main/java/edu/ntnu/idatt2003/view/components/FractalOperations.java b/src/main/java/edu/ntnu/idatt2003/view/components/FractalOperations.java index 7fb63a36d41cbec21531ddc7dd5229a501263ef4..8ee5c3f89bc4880c93896764daa603d8f03586ee 100644 --- a/src/main/java/edu/ntnu/idatt2003/view/components/FractalOperations.java +++ b/src/main/java/edu/ntnu/idatt2003/view/components/FractalOperations.java @@ -138,4 +138,13 @@ public class FractalOperations { ChaosGameDescription newChaosGame = new ChaosGameDescription(chaosGame.getChaosGameDescription().getTransforms(), minCoords, maxCoords); this.chaosGame = new ChaosGame(newChaosGame, 400, 400); } + + public void saveFractalToFile(String path) { +ChaosGameFileHandler fileHandler = new ChaosGameFileHandler(path); + try { + fileHandler.writeToFile(path, chaosGame.getChaosGameDescription()); + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/edu/ntnu/idatt2003/view/scenes/Scene.java b/src/main/java/edu/ntnu/idatt2003/view/scenes/Scene.java index 527318dff14932655e5d2714decb252b9932500b..11481eb2a90061a90f2464b6c5383ed5cd94de06 100644 --- a/src/main/java/edu/ntnu/idatt2003/view/scenes/Scene.java +++ b/src/main/java/edu/ntnu/idatt2003/view/scenes/Scene.java @@ -65,16 +65,19 @@ public class Scene { addCInputBox(); removeMatrixVectorInputBoxes(); removeFractalFromFileBox(); + addSaveFractalToFileButton(); } else if ("Sierpinski Triangle".equals(newValue)) { removeMatrixVectorInputBoxes(); removeCInputBox(); addMatrixVectorInputBoxes(); removeFractalFromFileBox(); + addSaveFractalToFileButton(); } else if ( "Barnsley Fern".equals(newValue)) { removeMatrixVectorInputBoxes(); removeCInputBox(); - addMatrixVectorInputBoxes(); removeFractalFromFileBox(); + addMatrixVectorInputBoxes(); + addSaveFractalToFileButton(); } else if ("Fractal From File".equals(newValue)) { removeCInputBox(); @@ -86,8 +89,10 @@ public class Scene { // Initial setup based on the provided fractal type if ("Julia Set".equals(fractalType)) { addCInputBox(); + addSaveFractalToFileButton(); } else if ("Sierpinski Triangle".equals(fractalType) || "Barnsley Fern".equals(fractalType)) { addMatrixVectorInputBoxes(); + addSaveFractalToFileButton(); } else if ("Fractal From File".equals(fractalType)) { addFractalFromFileBox(); } @@ -467,6 +472,13 @@ public class Scene { fractalOperations.updateMinAndMaxFractal(minCoords, maxCoords); } + public void addSaveFractalToFileButton(){ + Button saveFractalToFileButton = buttons.getBtnSaveFractalToFile(); + if (saveFractalToFileButton != null && !topBox.getChildren().contains(saveFractalToFileButton)) { + topBox.getChildren().add(saveFractalToFileButton); + } + } + public void setUpStage(Stage primaryStage) { primaryStage.setTitle("ChaosGame"); primaryStage.setScene(scene);