From 8bc928ca45da2bc26b37fe0e5d5321dddd685871 Mon Sep 17 00:00:00 2001 From: Magnus Eik <“mageik@stud.ntnu.noâ€> Date: Thu, 4 Apr 2024 16:31:08 +0200 Subject: [PATCH] Add fractal choice control to radio buttons and a quit button --- .../stud/chaosgame/view/ChaosGameGUIView.java | 59 +++++++++++++++++-- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGUIView.java b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGUIView.java index a4d2647..361578f 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGUIView.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGUIView.java @@ -7,6 +7,7 @@ import edu.ntnu.stud.chaosgame.model.generators.ChaosGameDescriptionFactory; import javafx.animation.KeyFrame; import javafx.animation.Timeline; +import javafx.application.Platform; import javafx.scene.Scene; import javafx.scene.control.ToggleGroup; import javafx.scene.control.RadioButton; @@ -22,6 +23,7 @@ import javafx.scene.control.Button; import javafx.util.Duration; import java.io.IOException; +import java.util.concurrent.atomic.AtomicReference; public class ChaosGameGUIView { private int currentLine = 0; @@ -43,7 +45,7 @@ public class ChaosGameGUIView { imageView = new ImageView(); - width = 1100; + width = 1000; height = 1000; WritableImage writableImage = new WritableImage(width, height); @@ -57,9 +59,9 @@ public class ChaosGameGUIView { //TEMPORARY CODE to test Chaos Games in GUI ChaosGameDescriptionFactory factory = new ChaosGameDescriptionFactory(); - ChaosGameDescription description = factory.getDescriptions().get(1); - ChaosCanvas canvas = new ChaosCanvas(1000, 1000, description.getMinCoords(), description.getMaxCoords()); - game = new ChaosGame(description, canvas); + AtomicReference<ChaosGameDescription> description = new AtomicReference<>(factory.getDescriptions().get(0)); + ChaosCanvas canvas = new ChaosCanvas(1000, 1000, description.get().getMinCoords(), description.get().getMaxCoords()); + game = new ChaosGame(description.get(), canvas); Button startButton = new Button("Start"); startButton.setOnAction(event -> timeline.play()); @@ -94,8 +96,55 @@ public class ChaosGameGUIView { RadioButton juliaRadioButton = new RadioButton("Julia"); juliaRadioButton.setToggleGroup(group); + AtomicReference<ChaosCanvas> canvasRef = new AtomicReference<>(canvas); + sierpinskiRadioButton.setOnAction(event -> { + timeline.stop(); + canvasRef.get().clearCanvas(); + WritableImage newWritableImage = new WritableImage(width, height); + setPixelWriter(newWritableImage.getPixelWriter()); + setImageViewFromImage(newWritableImage); + canvas.clearCanvas(); + + description.set(factory.getDescriptions().get(0)); // Assuming the Sierpinski description is at index 0 + canvasRef.set(new ChaosCanvas(1000, 1000, description.get().getMinCoords(), description.get().getMaxCoords())); + game = new ChaosGame(description.get(), canvasRef.get()); + }); + + barnsleyRadioButton.setOnAction(event -> { + timeline.stop(); + canvasRef.get().clearCanvas(); + WritableImage newWritableImage = new WritableImage(width, height); + setPixelWriter(newWritableImage.getPixelWriter()); + setImageViewFromImage(newWritableImage); + canvas.clearCanvas(); + description.set(factory.getDescriptions().get(1)); // Assuming the Sierpinski description is at index 0 + canvasRef.set(new ChaosCanvas(1000, 1000, description.get().getMinCoords(), description.get().getMaxCoords())); + game = new ChaosGame(description.get(), canvasRef.get()); + }); + + juliaRadioButton.setOnAction(event -> { + timeline.stop(); + canvasRef.get().clearCanvas(); + WritableImage newWritableImage = new WritableImage(width, height); + setPixelWriter(newWritableImage.getPixelWriter()); + setImageViewFromImage(newWritableImage); + canvas.clearCanvas(); + description.set(factory.getDescriptions().get(2)); // Assuming the Sierpinski description is at index 0 + canvasRef.set(new ChaosCanvas(1000, 1000, description.get().getMinCoords(), description.get().getMaxCoords())); + game = new ChaosGame(description.get(), canvasRef.get()); + }); + + // Quit button + Button quitButton = new Button("Quit"); + quitButton.setOnAction(event -> Platform.exit()); + + // Add basic control buttons sideMenu.getChildren().addAll(startButton,stopButton,newButton,clearButton); + // Add fractal radio buttons sideMenu.getChildren().addAll(sierpinskiRadioButton, barnsleyRadioButton, juliaRadioButton); + // Add quit button + sideMenu.getChildren().add(quitButton); + BorderPane borderPane = new BorderPane(); @@ -117,7 +166,7 @@ public class ChaosGameGUIView { ChaosCanvas canvas = game.getCanvas(); - game.runSteps(10); + game.runSteps(100000); // Test implementation for drawing fractals int[][] betaArray = canvas.getCanvasArray(); -- GitLab