Skip to content
Snippets Groups Projects
Commit e700fec8 authored by Magnus Eik's avatar Magnus Eik
Browse files

Add "Write to File"-button functionality.

parent c5662332
No related branches found
No related tags found
No related merge requests found
......@@ -68,4 +68,6 @@ public class ChaosGameDescription {
public Vector2D getMaxCoords(){
return maxCoords;
}
}
......@@ -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) {
......
......@@ -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;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment