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 badd5c199e4e72a919b4300167b162c72facb999..9bb957cfd9819d9a2939d79b9844b4deecea47c6 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGui.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGui.java @@ -39,6 +39,8 @@ public class ChaosGameGui implements ChaosGameObserver { private Stage primaryStage; + private double aspectRatio; + private Canvas canvas; @@ -124,6 +126,8 @@ public class ChaosGameGui implements ChaosGameObserver { private GuiButtonController controller; + + public ChaosGameGui(Stage primaryStage) throws IOException { this.primaryStage = primaryStage; this.initializeComponents(); @@ -135,7 +139,17 @@ public ChaosGameGui(Stage primaryStage) throws IOException { primaryStage.setScene(scene); primaryStage.setOnShown(event -> this.imageView.requestFocus()); primaryStage.show(); -} + // Initialize aspect ratio based on initial dimensions + this.aspectRatio = (double) width / height; + + // Add listeners to handle window size changes + scene.widthProperty().addListener((observable, oldValue, newValue) -> { + resizeCanvas(); + }); + + scene.heightProperty().addListener((observable, oldValue, newValue) -> { + resizeCanvas(); + });} /** * Initialize the components of the GUI. */ @@ -220,6 +234,10 @@ public ChaosGameGui(Stage primaryStage) throws IOException { this.canvas = new Canvas(width, height); //this.imageView.setImage(canvas.snapshot(null, null)); + canvas.widthProperty().bind(imageView.fitWidthProperty()); + canvas.heightProperty().bind(imageView.fitHeightProperty()); + + this.clearImageView(); } @@ -429,6 +447,8 @@ public ChaosGameGui(Stage primaryStage) throws IOException { rightVBox.setFocusTraversable(false); borderPane.setFocusTraversable(false); + + } /** @@ -569,4 +589,22 @@ public ChaosGameGui(Stage primaryStage) throws IOException { public Window getStage() { return this.primaryStage; } + + private void resizeCanvas() { + double newWidth = scene.getWidth() - sideMenu.getWidth(); + double newHeight = scene.getHeight(); + + if (newWidth / newHeight > aspectRatio) { + newWidth = newHeight * aspectRatio; + } else { + newHeight = newWidth / aspectRatio; + } + + // Update imageView size to new calculated dimensions + imageView.setFitWidth(newWidth); + imageView.setFitHeight(newHeight); + + // Redraw the fractal to fit the new canvas size + controller.drawChaosGame(); + } }