diff --git a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameDescription.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameDescription.java index 77c1b484bc9578883391a3644816947871180bbd..70ad369ab9314a34346be046fd379b91a45cfd8f 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameDescription.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGameDescription.java @@ -68,4 +68,6 @@ public class ChaosGameDescription { public Vector2D getMaxCoords(){ return maxCoords; } + + } diff --git a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/GuiButtonController.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/GuiButtonController.java index 74c66aeefd3bf38cf10745e4511e65b48123f9dd..4c786633766b3cc2e89ed94a1f8904c8dc44da00 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/GuiButtonController.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/GuiButtonController.java @@ -6,10 +6,15 @@ import edu.ntnu.stud.chaosgame.view.ChaosGameGui; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Platform; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; import javafx.scene.canvas.GraphicsContext; import javafx.scene.image.WritableImage; import javafx.util.Duration; +import javafx.stage.FileChooser; +import java.io.File; +import java.io.IOException; import java.util.Objects; public class GuiButtonController { @@ -18,6 +23,7 @@ public class GuiButtonController { private Timeline timeline; private ChaosCanvas chaosCanvas; private ChaosGameDescriptionFactory factory; + private ChaosGameFileHandler fileHandler; public GuiButtonController(ChaosGame game, ChaosGameGui gui) { @@ -29,6 +35,49 @@ public class GuiButtonController { this.chaosCanvas = new ChaosCanvas(1000, 1000, this.factory.getDescriptions().get(0).getMinCoords(), this.factory.getDescriptions().get(0).getMaxCoords()); initializeMainButtons(); + this.fileHandler = new ChaosGameFileHandler(); + initializeWriteToFileButtonHandler(); + + } + + private void initializeWriteToFileButtonHandler() { + gui.getWriteToFileButton().setOnAction(new EventHandler<ActionEvent>() { + @Override + public void handle(ActionEvent event) { + // Create a FileChooser object + FileChooser fileChooser = new FileChooser(); + + // Set the title of the FileChooser dialog + fileChooser.setTitle("Save File"); + + // Set the initial directory of the FileChooser + fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); + + // Set the extension filter + fileChooser.getExtensionFilters().addAll( + new FileChooser.ExtensionFilter("Text Files", "*.txt"), + new FileChooser.ExtensionFilter("All Files", "*.*") + ); + + // Show the save file dialog and get the selected file + File file = fileChooser.showSaveDialog(gui.getStage()); + + if (file != null) { + try { + // Create a ChaosGameDescription object + ChaosGameDescription description = game.getDescription(); + + // Call the writeToFile method + System.out.println("About to write to file..."); + fileHandler.writeToFile(description, file.getPath()); + System.out.println("Finished writing to file."); + } catch (IOException e) { + // Handle the exception + e.printStackTrace(); + } + } + } + }); } public void runGameSteps(int steps) { diff --git a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGui.java b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGui.java index e1f49486c938e5b83018d0767ca6f4cb30fde73c..81f6f60ee8309f7c4eb4cf1dfe76e2f8da0124ab 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGui.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGui.java @@ -27,6 +27,7 @@ import javafx.scene.layout.Region; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.stage.Stage; +import javafx.stage.Window; import javafx.util.Duration; import java.io.IOException; @@ -34,6 +35,8 @@ import java.io.IOException; public class ChaosGameGui implements ChaosGameObserver { private int currentLine = 0; + private Stage primaryStage; + private Canvas canvas; @@ -120,7 +123,7 @@ public class ChaosGameGui implements ChaosGameObserver { public ChaosGameGui(Stage primaryStage) throws IOException { - + this.primaryStage = primaryStage; this.initializeComponents(); this.initializeGameComponents(); this.controller = new GuiButtonController(game, this); // Initialize controller here @@ -530,5 +533,11 @@ public ChaosGameGui(Stage primaryStage) throws IOException { } + public ButtonBase getWriteToFileButton() { + return this.writeFractalToFileButton; + } + public Window getStage() { + return this.primaryStage; + } }