Skip to content
Snippets Groups Projects
Commit b45e774b authored by Vetle Solheim Hodne's avatar Vetle Solheim Hodne
Browse files

Implemented observer design pattern for ChaosGame.

parent 58434e66
No related branches found
No related tags found
1 merge request!14Add open from file button in ChaosGame
Pipeline #271036 failed
...@@ -6,7 +6,10 @@ import javafx.scene.control.Button; ...@@ -6,7 +6,10 @@ import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import javafx.stage.Stage; import javafx.stage.Stage;
import org.example.chaosgame.controller.MainController;
import org.example.chaosgame.model.chaos.ChaosGame;
import org.example.chaosgame.model.chaos.ChaosGameDescription; import org.example.chaosgame.model.chaos.ChaosGameDescription;
import org.example.chaosgame.model.chaos.ChaosGameDescriptionFactory;
import org.example.chaosgame.model.chaos.ChaosGameFileHandler; import org.example.chaosgame.model.chaos.ChaosGameFileHandler;
import org.example.chaosgame.view.ChaosPage; import org.example.chaosgame.view.ChaosPage;
import org.example.chaosgame.view.ExplorePage; import org.example.chaosgame.view.ExplorePage;
...@@ -34,7 +37,6 @@ public class MainApp extends Application { ...@@ -34,7 +37,6 @@ public class MainApp extends Application {
}); });
Button exploreButton = menuView.getExploreButton(); Button exploreButton = menuView.getExploreButton();
exploreButton.setOnAction(e-> { exploreButton.setOnAction(e-> {
borderPane.setCenter(explorePage.getExploreContent()); borderPane.setCenter(explorePage.getExploreContent());
}); });
HBox menuBar = menuView.getMenuBar(); HBox menuBar = menuView.getMenuBar();
......
package org.example.chaosgame.controller;
public interface ChaosGameSubject {
void registerObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers();
}
package org.example.chaosgame.controller; package org.example.chaosgame.controller;
public class MainController { import javafx.scene.layout.StackPane;
import org.example.chaosgame.model.chaos.ChaosGame;
import org.example.chaosgame.view.ChaosPage;
public class MainController implements Observer{
private final ChaosGame chaosGame;
private final ChaosPage chaosPage;
public MainController(ChaosGame chaosGame) {
this.chaosGame = chaosGame;
this.chaosPage = new ChaosPage();
chaosGame.registerObserver(this);
}
@Override
public void update() {
chaosPage.updateCanvas();
}
} }
package org.example.chaosgame.controller;
public interface Observer {
void update();
}
package org.example.chaosgame.model.chaos; package org.example.chaosgame.model.chaos;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import org.example.chaosgame.controller.ChaosGameSubject;
import org.example.chaosgame.controller.Observer;
import org.example.chaosgame.model.linalg.Vector2D; import org.example.chaosgame.model.linalg.Vector2D;
/** /**
...@@ -13,14 +17,15 @@ import org.example.chaosgame.model.linalg.Vector2D; ...@@ -13,14 +17,15 @@ import org.example.chaosgame.model.linalg.Vector2D;
* The new point is then drawn on the canvas. * The new point is then drawn on the canvas.
* This process is repeated a selected amount of steps. * This process is repeated a selected amount of steps.
*/ */
public class ChaosGame { public class ChaosGame implements ChaosGameSubject {
private final ChaosCanvas canvas; private final ChaosCanvas canvas;
private final ChaosGameDescription description; private final ChaosGameDescription description;
private Vector2D currentPoint = new Vector2D(0.0, 0.0); private Vector2D currentPoint = new Vector2D(0.0, 0.0);
public final Random random = new Random(); private final Random random = new Random();
private List<Observer> observers;
/** /**
* Constructor for ChaosGame. * Constructor for ChaosGame.
...@@ -36,6 +41,7 @@ public class ChaosGame { ...@@ -36,6 +41,7 @@ public class ChaosGame {
this.description = description; this.description = description;
this.canvas = new ChaosCanvas(width, height, this.canvas = new ChaosCanvas(width, height,
description.getMinCoords(), description.getMaxCoords()); description.getMinCoords(), description.getMaxCoords());
this.observers = new ArrayList<>();
} }
...@@ -55,6 +61,7 @@ public class ChaosGame { ...@@ -55,6 +61,7 @@ public class ChaosGame {
} else { } else {
runStepsUniform(steps); runStepsUniform(steps);
} }
notifyObservers();
} }
private void runStepsUniform(int steps) { private void runStepsUniform(int steps) {
for (int i = 0; i < steps; i++) { for (int i = 0; i < steps; i++) {
...@@ -90,4 +97,25 @@ public class ChaosGame { ...@@ -90,4 +97,25 @@ public class ChaosGame {
canvas.putPixel(currentPoint); canvas.putPixel(currentPoint);
} }
} }
@Override
public void registerObserver(Observer observer) {
observers.add(observer);
System.out.println("Observer added");
}
@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
System.out.println("Observer removed");
}
@Override
public void notifyObservers() {
for (Observer observer : observers) {
observer.update();
System.out.println("Observer notified");
}
}
} }
...@@ -26,7 +26,6 @@ public class ChaosPage { ...@@ -26,7 +26,6 @@ public class ChaosPage {
private final Canvas canvas; private final Canvas canvas;
private final GraphicsContext gc; private final GraphicsContext gc;
private final Label errorLabel = new Label("Invalid input. Please enter a valid number."); private final Label errorLabel = new Label("Invalid input. Please enter a valid number.");
private final VBox runStepsBox = new VBox(); private final VBox runStepsBox = new VBox();
public ChaosPage() { public ChaosPage() {
...@@ -42,16 +41,8 @@ public class ChaosPage { ...@@ -42,16 +41,8 @@ public class ChaosPage {
stepsField.setPromptText("Enter number of steps"); stepsField.setPromptText("Enter number of steps");
ComboBox<String> contextMenu = new ComboBox<>(); ComboBox<String> contextMenu = new ComboBox<>();
contextMenu.setPromptText("Select chaos game"); contextMenu.setPromptText("Select chaos game");
ColorAdjust shade = new ColorAdjust();
shade.setBrightness(0.4);
contextMenu.addEventHandler(MouseEvent.MOUSE_ENTERED,
e -> contextMenu.setEffect(shade));
contextMenu.addEventHandler(MouseEvent.MOUSE_EXITED,
e -> contextMenu.setEffect(null));
contextMenu.getItems().addAll("Julia", "Sierpinski", "Barnsley", "Make your own"); contextMenu.getItems().addAll("Julia", "Sierpinski", "Barnsley", "Make your own");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment