diff --git a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/enums/ButtonEnum.java b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/enums/ButtonEnum.java new file mode 100644 index 0000000000000000000000000000000000000000..1397f4f34aca02325455bb50bd69bf466e56aafa --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/enums/ButtonEnum.java @@ -0,0 +1,14 @@ +package edu.ntnu.idatt2003.mappevurderingprog2.enums; + +/** + * An enum representing the buttons in the application + */ +public enum ButtonEnum { + Transform, + ClearTransformation, + ZoomIn, + ZoomOut, + ResetZoom, + RotateTransformationLeft, + RotateTransformationRight +} diff --git a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/models/chaos/ChaosGame.java b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/models/chaos/ChaosGame.java index 30b19d51591a9121292116c97e102b4a92d1bf33..cc752532b5d0559372eb0b786de932501541f0db 100644 --- a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/models/chaos/ChaosGame.java +++ b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/models/chaos/ChaosGame.java @@ -7,6 +7,8 @@ import edu.ntnu.idatt2003.mappevurderingprog2.models.Matrix2x2; import edu.ntnu.idatt2003.mappevurderingprog2.models.Transform2D; import edu.ntnu.idatt2003.mappevurderingprog2.models.UserInterface; import edu.ntnu.idatt2003.mappevurderingprog2.models.Vector2D; +import edu.ntnu.idatt2003.mappevurderingprog2.views.Components.JuliaTransformationButton; + import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -19,8 +21,10 @@ public class ChaosGame { private ChaosCanvas canvas; private ChaosGameDescription description; + private ChaosGameDescription juliaTransformDescription; private Vector2D currentPoint; - public Random random; + private Random random; + public ChaosGame(ChaosGameDescription description, int width, int height) { this.description = description; @@ -55,4 +59,14 @@ public class ChaosGame { ChaosGameDescription description = new ChaosGameDescription(transforms, new Vector2D(0, 0), new Vector2D(1, 1)); return description; } + + public static ChaosGameDescription createJuliaChaosGameDescription() { + List<Transform2D> transforms = new ArrayList<>(); + // Add the JuliaTransform to the list of transforms + transforms.add(new JuliaTransform(new Complex(-0.74543, 0.11301), -1)); + ChaosGameDescription juliaTransformDescription = new ChaosGameDescription( + transforms, new Vector2D(-1.6, -1.0), new Vector2D(1.6, 1.0)); + return juliaTransformDescription; + } + } \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/utils/Colorpalette.java b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/utils/Colorpalette.java index 00fce62feed702579903cde8b36c9d6f36c359d3..2ef4b3c5017ab2fd4617c5eebde324830732d436 100644 --- a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/utils/Colorpalette.java +++ b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/utils/Colorpalette.java @@ -5,5 +5,5 @@ import javafx.scene.paint.Color; public final class Colorpalette{ public static final Color White = Color.web("#FFFFFF"); public static final Color Black = Color.web("#000000"); - public static final Color Primary = Color.web("#778899"); + public static final Color Primary = Color.web("#7971EA"); } \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/views/Components/AffineTransformationButton.java b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/views/Components/AffineTransformationButton.java new file mode 100644 index 0000000000000000000000000000000000000000..7e383cd7c09ac159737ed0cdd9f23825fb133c98 --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/views/Components/AffineTransformationButton.java @@ -0,0 +1,73 @@ +package edu.ntnu.idatt2003.mappevurderingprog2.views.Components; + +import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosCanvas; +import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGame; +import edu.ntnu.idatt2003.mappevurderingprog2.utils.Colorpalette; +import edu.ntnu.idatt2003.mappevurderingprog2.views.View; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.canvas.Canvas; + +import javafx.scene.control.Menu; +import javafx.scene.control.MenuItem; + +public class AffineTransformationButton extends Menu { + private final GraphicsContext gc; + public int[][] canvas; + private ChaosGame chaosGame; + private View view; + private Canvas affineTransformationCanvas; // Declare the Canvas object + + public AffineTransformationButton(ChaosGame chaosGame, View view, Canvas affineTransformationCanvas) { + super("Affine Transformation"); // Set the name of the Menu + this.chaosGame = chaosGame; + this.view = view; + this.affineTransformationCanvas = affineTransformationCanvas; // Initialize the Canvas object + this.gc = affineTransformationCanvas.getGraphicsContext2D(); // Initialize the GraphicsContext object + + MenuItem transformItem = new MenuItem("Transform"); + transformItem.setOnAction(event -> performTransformation()); + this.getItems().add(transformItem); + } + + private void performTransformation() { + // Run steps on the existing ChaosGame instance + chaosGame.runSteps(20000); + + // Get the ChaosCanvas and draw it on your application's canvas + ChaosCanvas chaosCanvas = chaosGame.getCanvas(); + redrawCanvas(chaosCanvas); // Make sure the View class has a redrawCanvas method that accepts a ChaosCanvas object + redrawCanvas(chaosCanvas); + } + + private void redrawCanvas(ChaosCanvas chaosCanvas) { + int[][] newCanvas = chaosCanvas.getCanvasArray(); + this.canvas = newCanvas; + // Get the width and height of the canvas + double canvasWidth = affineTransformationCanvas.getWidth() - 20; + double canvasHeight = affineTransformationCanvas.getHeight() - 20; + + // Calculate the size of each pixel + double pixelWidth = canvasWidth / canvas[0].length; + double pixelHeight = canvasHeight / canvas.length; + + // Loop through each pixel in the canvas array + for (int i = 0; i < canvas.length; i++) { + for (int j = 0; j < canvas[i].length; j++) { + // Calculate the coordinates of the pixel on the canvas + double x = j * pixelWidth + 3; + double y = i * pixelHeight + 3; + + // Set the fill color based on the pixel value + if (canvas[i][j] == 0) { + gc.setFill(Colorpalette.White); + } else { + gc.setFill(Colorpalette.Black); + } + // Fill a rectangle representing the pixel + gc.fillRect(x, y, pixelWidth, pixelHeight); + } + } + } + + +} \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/views/Components/JuliaTransformationButton.java b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/views/Components/JuliaTransformationButton.java new file mode 100644 index 0000000000000000000000000000000000000000..8bba50642d9d7b913ff2085ee5c412797bc8b122 --- /dev/null +++ b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/views/Components/JuliaTransformationButton.java @@ -0,0 +1,70 @@ +package edu.ntnu.idatt2003.mappevurderingprog2.views.Components; + +import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosCanvas; +import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGame; +import edu.ntnu.idatt2003.mappevurderingprog2.utils.Colorpalette; +import edu.ntnu.idatt2003.mappevurderingprog2.views.View; +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.control.Menu; +import javafx.scene.control.MenuItem; +public class JuliaTransformationButton extends Menu { + private final GraphicsContext gc; + public int[][] canvas; + private ChaosGame chaosGame; + private View view; + private Canvas juliaTransformationCanvas; + + public JuliaTransformationButton (ChaosGame chaosGame, View view, Canvas juliaTransformationCanvas) { + super("Julia Transformation"); + this.chaosGame = chaosGame; + this.view = view; + this.juliaTransformationCanvas = juliaTransformationCanvas; + this.gc = juliaTransformationCanvas.getGraphicsContext2D(); + + MenuItem transformItem = new MenuItem("Transform"); + transformItem.setOnAction(event -> performTransformation()); + this.getItems().add(transformItem); + } + + private void performTransformation() { + chaosGame.runSteps(20000); + + ChaosCanvas chaosCanvas = chaosGame.getCanvas(); + redrawCanvas(chaosCanvas); + redrawCanvas(chaosCanvas); + } + + private void redrawCanvas(ChaosCanvas chaosCanvas) { + int[][] newCanvas = chaosCanvas.getCanvasArray(); + this.canvas = newCanvas; + // Get the width and height of the canvas + double canvasWidth = juliaTransformationCanvas.getWidth() - 20; + double canvasHeight = juliaTransformationCanvas.getHeight() - 20; + + // Calculate the size of each pixel + double pixelWidth = canvasWidth / canvas[0].length; + double pixelHeight = canvasHeight / canvas.length; + + // Loop through each pixel in the canvas array + for (int i = 0; i < canvas.length; i++) { + for (int j = 0; j < canvas[i].length; j++) { + // Calculate the coordinates of the pixel on the canvas + double x = j * pixelWidth + 3; + double y = i * pixelHeight + 3; + + // Set the fill color based on the pixel value + if (canvas[i][j] == 0) { + gc.setFill(Colorpalette.White); + } else { + gc.setFill(Colorpalette.Black); + } + // Fill a rectangle representing the pixel + gc.fillRect(x, y, pixelWidth, pixelHeight); + } + } + } + + + } + diff --git a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/views/Components/MainBar.java b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/views/Components/MainBar.java index c9c30fa9d03e79fc0b7de2b83ad95b612dc1f189..a6ea269a8270e332a678c372b23dee018fcdf841 100644 --- a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/views/Components/MainBar.java +++ b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/views/Components/MainBar.java @@ -2,30 +2,26 @@ package edu.ntnu.idatt2003.mappevurderingprog2.views.Components; import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGame; import edu.ntnu.idatt2003.mappevurderingprog2.views.View; -import javafx.scene.control.Button; -import javafx.scene.control.ToolBar; +import javafx.scene.control.Menu; +import javafx.scene.control.MenuBar; +import javafx.scene.control.MenuItem; -// In your MainBar class -public class MainBar extends ToolBar { +public class MainBar extends MenuBar { private ChaosGame chaosGame; private View view; - public MainBar(ChaosGame chaosGame, View view){ + public MainBar(ChaosGame chaosGame, ChaosGame chaosGame1, View view) { this.chaosGame = chaosGame; this.view = view; - Button transformButton = new Button(); - transformButton.setText("Transform"); - transformButton.setOnAction(event -> performTransformation()); - getItems().add(transformButton); - } - - private void performTransformation() { - // Run steps on the existing ChaosGame instance - chaosGame.runSteps(10000); + AffineTransformationButton affineTransformationButton = new AffineTransformationButton( + chaosGame, view, view.getAffineTransformationCanvas()); + this.getMenus().add(affineTransformationButton); - // Get the ChaosCanvas and draw it on your application's canvas - view.redrawCanvas(chaosGame.getCanvas()); + JuliaTransformationButton juliaTransformationButton = new JuliaTransformationButton( + chaosGame1, view, view.getJuliaTransformationCanvas()); + this.getMenus().add(juliaTransformationButton); } +} + -} \ No newline at end of file diff --git a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/views/View.java b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/views/View.java index aafd28416bbe408bc06afbb056e24acd96509e3e..1a4e27bc43a3b4e91a54a88f55fdcabd81144002 100644 --- a/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/views/View.java +++ b/src/main/java/edu/ntnu/idatt2003/mappevurderingprog2/views/View.java @@ -4,71 +4,53 @@ import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGame; import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGameDescription; import edu.ntnu.idatt2003.mappevurderingprog2.utils.Colorpalette; import edu.ntnu.idatt2003.mappevurderingprog2.views.Components.MainBar; -import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosCanvas; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; -import javafx.scene.canvas.GraphicsContext; import javafx.scene.layout.Background; import javafx.scene.layout.BackgroundFill; import javafx.scene.layout.BorderPane; import javafx.scene.layout.CornerRadii; -// In your View class public class View extends BorderPane { - public int [][] canvas; private Canvas affineTransformationCanvas; - private GraphicsContext gc; + private Canvas juliaTransformationCanvas; + + public Canvas getAffineTransformationCanvas() { + return affineTransformationCanvas; + } + + public Canvas getJuliaTransformationCanvas() { + return juliaTransformationCanvas; + } + + public Scene createScene() { this.setBackground(new Background(new BackgroundFill(Colorpalette.Primary, CornerRadii.EMPTY, Insets.EMPTY))); ChaosGameDescription description = ChaosGame.createAffineChaosGameDescription(); + ChaosGameDescription juliaTransformDescription = ChaosGame.createJuliaChaosGameDescription(); + ChaosGame chaosGame = new ChaosGame(description, 600, 600); + ChaosGame chaosGame1 = new ChaosGame(juliaTransformDescription, 600, 600); affineTransformationCanvas = new Canvas(600, 400); + juliaTransformationCanvas = new Canvas(600, 400); - gc = affineTransformationCanvas.getGraphicsContext2D(); - MainBar mainBar = new MainBar(chaosGame, this); + MainBar mainBar = new MainBar(chaosGame, chaosGame1, this); this.setTop(mainBar); + this.setCenter(juliaTransformationCanvas); this.setCenter(affineTransformationCanvas); return new Scene(this, 900, 700); } - public void redrawCanvas(ChaosCanvas chaosCanvas) { - int[][] newCanvas = chaosCanvas.getCanvasArray(); - this.canvas = newCanvas; - // Get the width and height of the canvas - double canvasWidth = affineTransformationCanvas.getWidth() - 20; - double canvasHeight = affineTransformationCanvas.getHeight() - 20; - - // Calculate the size of each pixel - double pixelWidth = canvasWidth / canvas[0].length; - double pixelHeight = canvasHeight / canvas.length; - - // Loop through each pixel in the canvas array - for (int i = 0; i < canvas.length; i++) { - for (int j = 0; j < canvas[i].length; j++) { - // Calculate the coordinates of the pixel on the canvas - double x = j * pixelWidth + 3; - double y = i * pixelHeight + 3; - - // Set the fill color based on the pixel value - if (canvas[i][j] == 0) { - gc.setFill(Colorpalette.White); - } else { - gc.setFill(Colorpalette.Black); - } - // Fill a rectangle representing the pixel - gc.fillRect(x, y, pixelWidth, pixelHeight); - } - } - } +