From f39b7b8483b1c50821e9c3bc18c52eeca5bebb99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Daleng?= <142524365+MrMarHVD@users.noreply.github.com> Date: Mon, 1 Apr 2024 14:59:37 +0200 Subject: [PATCH] Created ChaosGameDescriptionFactory and added 5 files containing data. Currently they containthe same data, this should be changed later. --- .../java/edu/ntnu/stud/chaosgame/Main.java | 22 ++++---- .../ntnu/stud/chaosgame/model/Matrix2x2.java | 10 ++-- .../model/{handling => game}/ChaosCanvas.java | 2 +- .../model/{handling => game}/ChaosGame.java | 3 +- .../game/ChaosGameDescription.java | 2 +- .../game/ChaosGameFileHandler.java | 2 +- .../ChaosGameDescriptionFactory.java | 51 +++++++++++++++++++ .../transformations/AffineTransform2D.java | 3 +- .../model/transformations/JuliaTransform.java | 3 +- .../resources/descriptions/factory/desc_1.txt | 6 +++ .../resources/descriptions/factory/desc_2.txt | 6 +++ .../resources/descriptions/factory/desc_3.txt | 6 +++ .../resources/descriptions/factory/desc_4.txt | 6 +++ .../resources/descriptions/factory/desc_5.txt | 6 +++ .../game/ChaosGameDescriptionTest.java | 1 + 15 files changed, 107 insertions(+), 22 deletions(-) rename src/main/java/edu/ntnu/stud/chaosgame/model/{handling => game}/ChaosCanvas.java (98%) rename src/main/java/edu/ntnu/stud/chaosgame/model/{handling => game}/ChaosGame.java (93%) rename src/main/java/edu/ntnu/stud/chaosgame/{ => model}/game/ChaosGameDescription.java (97%) rename src/main/java/edu/ntnu/stud/chaosgame/{ => model}/game/ChaosGameFileHandler.java (99%) create mode 100644 src/main/java/edu/ntnu/stud/chaosgame/model/generators/ChaosGameDescriptionFactory.java create mode 100644 src/main/resources/descriptions/factory/desc_1.txt create mode 100644 src/main/resources/descriptions/factory/desc_2.txt create mode 100644 src/main/resources/descriptions/factory/desc_3.txt create mode 100644 src/main/resources/descriptions/factory/desc_4.txt create mode 100644 src/main/resources/descriptions/factory/desc_5.txt diff --git a/src/main/java/edu/ntnu/stud/chaosgame/Main.java b/src/main/java/edu/ntnu/stud/chaosgame/Main.java index 652e4fa..a6e8cc0 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/Main.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/Main.java @@ -1,16 +1,11 @@ package edu.ntnu.stud.chaosgame; -import edu.ntnu.stud.chaosgame.game.ChaosGameDescription; -import edu.ntnu.stud.chaosgame.game.ChaosGameFileHandler; -import edu.ntnu.stud.chaosgame.model.Matrix2x2; -import edu.ntnu.stud.chaosgame.model.Vector2D; -import edu.ntnu.stud.chaosgame.model.handling.ChaosCanvas; -import edu.ntnu.stud.chaosgame.model.handling.ChaosGame; -import edu.ntnu.stud.chaosgame.model.transformations.AffineTransform2D; -import edu.ntnu.stud.chaosgame.model.transformations.Transform2D; +import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription; +import edu.ntnu.stud.chaosgame.model.game.ChaosGameFileHandler; +import edu.ntnu.stud.chaosgame.model.game.ChaosCanvas; +import edu.ntnu.stud.chaosgame.model.game.ChaosGame; +import edu.ntnu.stud.chaosgame.model.generators.ChaosGameDescriptionFactory; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import java.util.Objects; import java.util.Scanner; @@ -36,7 +31,12 @@ public class Main { game = new ChaosGame(description, canvas); } catch (Exception e) { - System.out.println(e.getMessage()); // TODO: Alter error handling + System.out.println(e.getMessage()); // TODO: Alter error game + } + + ChaosGameDescriptionFactory factory = new ChaosGameDescriptionFactory(); + for (ChaosGameDescription desc : factory.getDescriptions()) { + System.out.println(desc); } boolean exit = false; diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/Matrix2x2.java b/src/main/java/edu/ntnu/stud/chaosgame/model/Matrix2x2.java index a45d369..caab08e 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/Matrix2x2.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/Matrix2x2.java @@ -1,5 +1,7 @@ package edu.ntnu.stud.chaosgame.model; +import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription; + /** * Class representing a 2x2 matrix. */ @@ -50,7 +52,7 @@ public class Matrix2x2 { } /** - * Getter method for {@link edu.ntnu.stud.chaosgame.game.ChaosGameDescription} + * Getter method for {@link ChaosGameDescription} * @return a matrix component. */ public double getA00() { @@ -58,7 +60,7 @@ public class Matrix2x2 { } /** - * Getter method for {@link edu.ntnu.stud.chaosgame.game.ChaosGameDescription} + * Getter method for {@link ChaosGameDescription} * @return a matrix component. */ public double getA01() { @@ -66,7 +68,7 @@ public class Matrix2x2 { } /** - * Getter method for {@link edu.ntnu.stud.chaosgame.game.ChaosGameDescription} + * Getter method for {@link ChaosGameDescription} * @return a matrix component. */ public double getA10() { @@ -74,7 +76,7 @@ public class Matrix2x2 { } /** - * Getter method for {@link edu.ntnu.stud.chaosgame.game.ChaosGameDescription} + * Getter method for {@link ChaosGameDescription} * @return a matrix component. */ public double getA11() { diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/handling/ChaosCanvas.java b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosCanvas.java similarity index 98% rename from src/main/java/edu/ntnu/stud/chaosgame/model/handling/ChaosCanvas.java rename to src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosCanvas.java index 69dc7df..530564c 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/handling/ChaosCanvas.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosCanvas.java @@ -1,4 +1,4 @@ -package edu.ntnu.stud.chaosgame.model.handling; +package edu.ntnu.stud.chaosgame.model.game; import edu.ntnu.stud.chaosgame.model.Matrix2x2; import edu.ntnu.stud.chaosgame.model.Vector2D; diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/handling/ChaosGame.java b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGame.java similarity index 93% rename from src/main/java/edu/ntnu/stud/chaosgame/model/handling/ChaosGame.java rename to src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGame.java index 2c29dec..4ab7c9d 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/handling/ChaosGame.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGame.java @@ -1,6 +1,5 @@ -package edu.ntnu.stud.chaosgame.model.handling; +package edu.ntnu.stud.chaosgame.model.game; -import edu.ntnu.stud.chaosgame.game.ChaosGameDescription; import edu.ntnu.stud.chaosgame.model.Vector2D; import java.util.Random; diff --git a/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGameDescription.java b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameDescription.java similarity index 97% rename from src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGameDescription.java rename to src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameDescription.java index 9416272..519aa88 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGameDescription.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameDescription.java @@ -1,4 +1,4 @@ -package edu.ntnu.stud.chaosgame.game; +package edu.ntnu.stud.chaosgame.model.game; import edu.ntnu.stud.chaosgame.model.Vector2D; diff --git a/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandler.java b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameFileHandler.java similarity index 99% rename from src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandler.java rename to src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameFileHandler.java index 419eb9f..e28929b 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/game/ChaosGameFileHandler.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGameFileHandler.java @@ -1,4 +1,4 @@ -package edu.ntnu.stud.chaosgame.game; +package edu.ntnu.stud.chaosgame.model.game; import edu.ntnu.stud.chaosgame.model.Complex; import edu.ntnu.stud.chaosgame.model.Matrix2x2; diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/generators/ChaosGameDescriptionFactory.java b/src/main/java/edu/ntnu/stud/chaosgame/model/generators/ChaosGameDescriptionFactory.java new file mode 100644 index 0000000..78a7738 --- /dev/null +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/generators/ChaosGameDescriptionFactory.java @@ -0,0 +1,51 @@ +package edu.ntnu.stud.chaosgame.model.generators; + +import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription; +import edu.ntnu.stud.chaosgame.model.game.ChaosGameFileHandler; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Objects; + +/** + * Class that generates a set number of ChaosGameDescription instances using a set of + * files read by a ChaosGameFileHandler. + */ +public class ChaosGameDescriptionFactory { + + /** + * The list of all the descriptions generated from the data in the directory specified + * in constructor. + */ + private final ArrayList<ChaosGameDescription> descriptions; + + /** + * Generate ChaosGameDescription-instances based on data in txt-files found in the directory. + * The naming of the txt-files must follow the pattern "desc_X.txt" where X is an integer + * incremented by 1 for each file in the directory. + * + * @throws IOException if the method fails to read successfully from one of the data files. + */ + public ChaosGameDescriptionFactory() throws IOException { + /** + * The directory where the descriptions are located. + */ + String directory1 = "src/main/resources/descriptions/factory/"; + this.descriptions = new ArrayList<>(); + ChaosGameFileHandler handler = new ChaosGameFileHandler(); + File directory = new File(directory1); + int fileCount = Objects.requireNonNull(directory.list()).length; + + for (int i = 0; i < fileCount; i++) { + this.descriptions.add(handler.readFromFile(directory1 + "desc_" + (i + 1) + ".txt")); + } + } + + /** + * Get the list of descriptions. + * + * @return the list. + */ + public ArrayList<ChaosGameDescription> getDescriptions() { return this.descriptions; } + +} diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/AffineTransform2D.java b/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/AffineTransform2D.java index ea625ba..7f04ea7 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/AffineTransform2D.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/AffineTransform2D.java @@ -2,6 +2,7 @@ package edu.ntnu.stud.chaosgame.model.transformations; import edu.ntnu.stud.chaosgame.model.Matrix2x2; import edu.ntnu.stud.chaosgame.model.Vector2D; +import edu.ntnu.stud.chaosgame.model.game.ChaosGameFileHandler; /** * Represents affine transformations in a 2D-plane by extending the abstract @@ -43,7 +44,7 @@ public class AffineTransform2D extends Transform2D { } /** - * Getter method to use with {@link edu.ntnu.stud.chaosgame.game.ChaosGameFileHandler} + * Getter method to use with {@link ChaosGameFileHandler} * @return The matrix for the transformation */ public Matrix2x2 getMatrix(){return this.matrix;} diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/JuliaTransform.java b/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/JuliaTransform.java index 1022516..5f4dbd2 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/JuliaTransform.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/transformations/JuliaTransform.java @@ -2,6 +2,7 @@ package edu.ntnu.stud.chaosgame.model.transformations; import edu.ntnu.stud.chaosgame.model.Complex; import edu.ntnu.stud.chaosgame.model.Vector2D; +import edu.ntnu.stud.chaosgame.model.game.ChaosGameFileHandler; public class JuliaTransform extends Transform2D { @@ -54,7 +55,7 @@ public class JuliaTransform extends Transform2D { } /** - * Getter method to use with {@link edu.ntnu.stud.chaosgame.game.ChaosGameFileHandler} + * Getter method to use with {@link ChaosGameFileHandler} * @return The complex number used in the transformation. */ public Complex getC1(){return this.c1;} diff --git a/src/main/resources/descriptions/factory/desc_1.txt b/src/main/resources/descriptions/factory/desc_1.txt new file mode 100644 index 0000000..e98ca28 --- /dev/null +++ b/src/main/resources/descriptions/factory/desc_1.txt @@ -0,0 +1,6 @@ +Affine2D +0.0, 0.0 +1.0, 1.0 +0.5, 0.0, 0.0, 0.5, 0.0, 0.0 +0.5, 0.0, 0.0, 0.5, 0.25, 0.5 +0.5, 0.0, 0.0, 0.5, 0.5, 0.0 diff --git a/src/main/resources/descriptions/factory/desc_2.txt b/src/main/resources/descriptions/factory/desc_2.txt new file mode 100644 index 0000000..e98ca28 --- /dev/null +++ b/src/main/resources/descriptions/factory/desc_2.txt @@ -0,0 +1,6 @@ +Affine2D +0.0, 0.0 +1.0, 1.0 +0.5, 0.0, 0.0, 0.5, 0.0, 0.0 +0.5, 0.0, 0.0, 0.5, 0.25, 0.5 +0.5, 0.0, 0.0, 0.5, 0.5, 0.0 diff --git a/src/main/resources/descriptions/factory/desc_3.txt b/src/main/resources/descriptions/factory/desc_3.txt new file mode 100644 index 0000000..e98ca28 --- /dev/null +++ b/src/main/resources/descriptions/factory/desc_3.txt @@ -0,0 +1,6 @@ +Affine2D +0.0, 0.0 +1.0, 1.0 +0.5, 0.0, 0.0, 0.5, 0.0, 0.0 +0.5, 0.0, 0.0, 0.5, 0.25, 0.5 +0.5, 0.0, 0.0, 0.5, 0.5, 0.0 diff --git a/src/main/resources/descriptions/factory/desc_4.txt b/src/main/resources/descriptions/factory/desc_4.txt new file mode 100644 index 0000000..e98ca28 --- /dev/null +++ b/src/main/resources/descriptions/factory/desc_4.txt @@ -0,0 +1,6 @@ +Affine2D +0.0, 0.0 +1.0, 1.0 +0.5, 0.0, 0.0, 0.5, 0.0, 0.0 +0.5, 0.0, 0.0, 0.5, 0.25, 0.5 +0.5, 0.0, 0.0, 0.5, 0.5, 0.0 diff --git a/src/main/resources/descriptions/factory/desc_5.txt b/src/main/resources/descriptions/factory/desc_5.txt new file mode 100644 index 0000000..e98ca28 --- /dev/null +++ b/src/main/resources/descriptions/factory/desc_5.txt @@ -0,0 +1,6 @@ +Affine2D +0.0, 0.0 +1.0, 1.0 +0.5, 0.0, 0.0, 0.5, 0.0, 0.0 +0.5, 0.0, 0.0, 0.5, 0.25, 0.5 +0.5, 0.0, 0.0, 0.5, 0.5, 0.0 diff --git a/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameDescriptionTest.java b/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameDescriptionTest.java index 0d80ceb..15b98d0 100644 --- a/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameDescriptionTest.java +++ b/src/test/java/edu/ntnu/stud/chaosgame/game/ChaosGameDescriptionTest.java @@ -3,6 +3,7 @@ package edu.ntnu.stud.chaosgame.game; import edu.ntnu.stud.chaosgame.model.Complex; import edu.ntnu.stud.chaosgame.model.Matrix2x2; import edu.ntnu.stud.chaosgame.model.Vector2D; +import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription; import edu.ntnu.stud.chaosgame.model.transformations.AffineTransform2D; import edu.ntnu.stud.chaosgame.model.transformations.JuliaTransform; import edu.ntnu.stud.chaosgame.model.transformations.Transform2D; -- GitLab