From 655ca67637f52dfacc60c968a15913157956013d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Daleng?= <142524365+MrMarHVD@users.noreply.github.com> Date: Tue, 2 Apr 2024 16:29:54 +0200 Subject: [PATCH] Converted ChaosGameObserver to an interface. --- .../controller/ChaosGameObserver.java | 14 +++++++--- .../stud/chaosgame/model/game/ChaosGame.java | 27 +++++++++++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/main/java/edu/ntnu/stud/chaosgame/controller/ChaosGameObserver.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/ChaosGameObserver.java index d558acb..c0cb4a3 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/controller/ChaosGameObserver.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/ChaosGameObserver.java @@ -1,12 +1,18 @@ package edu.ntnu.stud.chaosgame.controller; import edu.ntnu.stud.chaosgame.model.game.ChaosCanvas; +import edu.ntnu.stud.chaosgame.model.game.ChaosGame; import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription; /** - * Observer class for monitoring changes to the active - * ChaosGameDescription {@link ChaosGameDescription} or the canvas {@link ChaosCanvas} + * Observer interface for monitoring changes to the active + * */ -public class ChaosGameObserver { -// TODO: Create class +public interface ChaosGameObserver { +// TODO: Create interface + + /** + * Perform update + */ + void update(ChaosGame game); } diff --git a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGame.java b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGame.java index 26d2f5f..154a5e0 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGame.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/model/game/ChaosGame.java @@ -1,6 +1,9 @@ package edu.ntnu.stud.chaosgame.model.game; +import edu.ntnu.stud.chaosgame.controller.ChaosGameObserver; import edu.ntnu.stud.chaosgame.model.data.Vector2D; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Random; /** @@ -19,9 +22,9 @@ public class ChaosGame { private ChaosGameDescription description; /** - * The description for this ChaosGame + * Observers monitoring this ChaosGame. */ - // private ChaosGameDescription description; + private ArrayList<ChaosGameObserver> observers; /** * The current point in the chaos game. @@ -38,6 +41,11 @@ public class ChaosGame { */ private int numOfTransforms; + /** + * Basic parameterised constructor. + * @param description the ChaosGameDescription. + * @param canvas the ChaosCanvas. + */ public ChaosGame(ChaosGameDescription description, ChaosCanvas canvas) { this.canvas = canvas; this.description = description; @@ -46,6 +54,21 @@ public class ChaosGame { this.currentPoint = new Vector2D(0, 0); } + /** + * Parameterised constructor with observers + * @param description the ChaosGameDescription. + * @param canvas the ChaosCanvas. + * @param observers the ChaosGameObservers to be added. + */ + public ChaosGame(ChaosGameDescription description, ChaosCanvas canvas, ChaosGameObserver... observers) { + this.canvas = canvas; + this.description = description; + this.random = new Random(); + this.numOfTransforms = description.getTransforms().size(); + this.currentPoint = new Vector2D(0, 0); + this.observers.addAll(Arrays.asList(observers)); + } + /** * Get the canvas of this chaos game. * @return the canvas. -- GitLab