Skip to content
Snippets Groups Projects
Commit c2248e16 authored by Magnus Eik's avatar Magnus Eik
Browse files

Reformat initializeMainButtons based on MVC.

parent 77ca0357
No related branches found
No related tags found
No related merge requests found
...@@ -270,4 +270,5 @@ public class ChaosCanvas { ...@@ -270,4 +270,5 @@ public class ChaosCanvas {
public int[] getCentre() { public int[] getCentre() {
return new int[]{this.centreX, this.centreY}; return new int[]{this.centreX, this.centreY};
} }
} }
...@@ -5,6 +5,7 @@ import edu.ntnu.stud.chaosgame.view.ChaosCanvasToImageConverter; ...@@ -5,6 +5,7 @@ import edu.ntnu.stud.chaosgame.view.ChaosCanvasToImageConverter;
import edu.ntnu.stud.chaosgame.view.ChaosGameGui; import edu.ntnu.stud.chaosgame.view.ChaosGameGui;
import javafx.animation.KeyFrame; import javafx.animation.KeyFrame;
import javafx.animation.Timeline; import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.WritableImage; import javafx.scene.image.WritableImage;
import javafx.util.Duration; import javafx.util.Duration;
...@@ -27,6 +28,7 @@ public class GuiButtonController { ...@@ -27,6 +28,7 @@ public class GuiButtonController {
this.factory = new ChaosGameDescriptionFactory(); this.factory = new ChaosGameDescriptionFactory();
this.chaosCanvas = new ChaosCanvas(1000, 1000, this.factory.getDescriptions().get(0).getMinCoords(), this.chaosCanvas = new ChaosCanvas(1000, 1000, this.factory.getDescriptions().get(0).getMinCoords(),
this.factory.getDescriptions().get(0).getMaxCoords()); this.factory.getDescriptions().get(0).getMaxCoords());
initializeMainButtons();
} }
public void runGameSteps(int steps) { public void runGameSteps(int steps) {
...@@ -41,6 +43,7 @@ public class GuiButtonController { ...@@ -41,6 +43,7 @@ public class GuiButtonController {
// Convert the canvas to either an image with coloured pixels based on intensity, or just black and white. // Convert the canvas to either an image with coloured pixels based on intensity, or just black and white.
ChaosCanvasToImageConverter converter = new ChaosCanvasToImageConverter(chaosCanvas, ChaosCanvasToImageConverter converter = new ChaosCanvasToImageConverter(chaosCanvas,
gui.getColorCheckBox().isSelected()); gui.getColorCheckBox().isSelected());
WritableImage image = converter.getImage(); WritableImage image = converter.getImage();
gui.getCanvas().getGraphicsContext2D().drawImage(image, 0, 0); gui.getCanvas().getGraphicsContext2D().drawImage(image, 0, 0);
gui.getImageView().setImage(image); gui.getImageView().setImage(image);
...@@ -52,12 +55,26 @@ public class GuiButtonController { ...@@ -52,12 +55,26 @@ public class GuiButtonController {
public void stopGame() { public void stopGame() {
timeline.stop(); timeline.stop();
} }
public void newGame() {
game.getCanvas().clearCanvas();
timeline.play();
}
public void clearImageView() { public void clearImageView() {
GraphicsContext gc = gui.getCanvas().getGraphicsContext2D(); GraphicsContext gc = gui.getCanvas().getGraphicsContext2D();
gc.clearRect(0, 0, gui.getCanvas().getWidth(), gui.getCanvas().getHeight()); gc.clearRect(0, 0, gui.getCanvas().getWidth(), gui.getCanvas().getHeight());
gui.getImageView().setImage(null); gui.getImageView().setImage(null);
} }
public void quitGame() {
Platform.exit();
}
private void initializeMainButtons() {
gui.getStartButton().setOnAction(event -> startGame());
gui.getStopButton().setOnAction(event -> stopGame());
gui.getNewButton().setOnAction(event -> newGame());
gui.getClearButton().setOnAction(event -> clearImageView());
gui.getQuitButton().setOnAction(event -> quitGame());
}
/** /**
* Update the description of the chaos game. * Update the description of the chaos game.
...@@ -89,4 +106,6 @@ public class GuiButtonController { ...@@ -89,4 +106,6 @@ public class GuiButtonController {
game = new ChaosGame(description, chaosCanvas); game = new ChaosGame(description, chaosCanvas);
game.setUseColor(gui.getColorCheckBox().isSelected()); game.setUseColor(gui.getColorCheckBox().isSelected());
} }
} }
\ No newline at end of file
...@@ -121,10 +121,10 @@ public class ChaosGameGui implements ChaosGameObserver { ...@@ -121,10 +121,10 @@ public class ChaosGameGui implements ChaosGameObserver {
public ChaosGameGui(Stage primaryStage) throws IOException { public ChaosGameGui(Stage primaryStage) throws IOException {
this.controller = new GuiButtonController(game, this); // Initialize controller here
this.initializeComponents(); this.initializeComponents();
this.initializeGameComponents(); this.initializeGameComponents();
this.controller = new GuiButtonController(game, this); // Initialize controller here
primaryStage.setTitle("Fractal Chaos Game"); primaryStage.setTitle("Fractal Chaos Game");
primaryStage.setScene(scene); primaryStage.setScene(scene);
...@@ -140,11 +140,12 @@ public ChaosGameGui(Stage primaryStage) throws IOException { ...@@ -140,11 +140,12 @@ public ChaosGameGui(Stage primaryStage) throws IOException {
//this.timeline = new Timeline(new KeyFrame(Duration.seconds(0.05), event -> controller.drawChaosGame())); //this.timeline = new Timeline(new KeyFrame(Duration.seconds(0.05), event -> controller.drawChaosGame()));
this.initializeImageView(); this.initializeImageView();
// Side menu // Side menu
this.initializeMainButtons(); //this.initializeMainButtons();
this.initializeFractalButtons(); this.initializeFractalButtons();
this.initializeSideMenu(); this.initializeSideMenu();
...@@ -154,31 +155,31 @@ public ChaosGameGui(Stage primaryStage) throws IOException { ...@@ -154,31 +155,31 @@ public ChaosGameGui(Stage primaryStage) throws IOException {
/** /**
* Initialize the main buttons for the GUI. * Initialize the main buttons for the GUI.
*/ */
private void initializeMainButtons() { // private void initializeMainButtons() {
this.startButton = new Button("Start"); // this.startButton = new Button("Start");
startButton.setOnAction(event -> controller.startGame()); // startButton.setOnAction(event -> controller.startGame());
this.stopButton = new Button("Stop"); // this.stopButton = new Button("Stop");
stopButton.setOnAction(event -> controller.stopGame()); // stopButton.setOnAction(event -> controller.stopGame());
//
this.newButton = new Button("New"); // this.newButton = new Button("New");
//
newButton.setOnAction(event ->{ // newButton.setOnAction(event ->{
this.canvas.getGraphicsContext2D().clearRect(0, 0, canvas.getWidth(), canvas.getHeight()); // this.canvas.getGraphicsContext2D().clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
chaosCanvas.clearCanvas(); // chaosCanvas.clearCanvas();
}); // });
//
this.clearButton = new Button("Clear"); // this.clearButton = new Button("Clear");
//
clearButton.setOnAction(event -> { // clearButton.setOnAction(event -> {
getImageView().setImage(null); // getImageView().setImage(null);
setCurrentLine(0); // setCurrentLine(0);
}); // });
//
// Quit button // // Quit button
this.quitButton = new Button("Quit"); // this.quitButton = new Button("Quit");
quitButton.setOnAction(event -> Platform.exit()); // quitButton.setOnAction(event -> Platform.exit());
//
} // }
/** /**
* Initialize the components related to the chaos game itself. * Initialize the components related to the chaos game itself.
...@@ -264,6 +265,15 @@ public ChaosGameGui(Stage primaryStage) throws IOException { ...@@ -264,6 +265,15 @@ public ChaosGameGui(Stage primaryStage) throws IOException {
* Initialize the side menu. * Initialize the side menu.
*/ */
private void initializeSideMenu() { private void initializeSideMenu() {
this.startButton = new Button("Start");
this.stopButton = new Button("Stop");
this.newButton = new Button("New");
this.clearButton = new Button("Clear");
this.quitButton = new Button("Quit");
this.sideMenuButton = new Button("Side Menu");
this.sideMenu = new VBox(); this.sideMenu = new VBox();
// Parameters // Parameters
VBox parameterBox = new VBox(); VBox parameterBox = new VBox();
...@@ -342,6 +352,8 @@ public ChaosGameGui(Stage primaryStage) throws IOException { ...@@ -342,6 +352,8 @@ public ChaosGameGui(Stage primaryStage) throws IOException {
Region sideMenuButtonRegion = new Region(); Region sideMenuButtonRegion = new Region();
sideMenuButtonRegion.setMinWidth(200); sideMenuButtonRegion.setMinWidth(200);
HBox sideMenuButtonBox = new HBox(); HBox sideMenuButtonBox = new HBox();
sideMenuButtonBox.getChildren().addAll(sideMenuButtonRegion, sideMenuButton); sideMenuButtonBox.getChildren().addAll(sideMenuButtonRegion, sideMenuButton);
// The right VBox containing both the sidebar and the sidebar toggle button. // The right VBox containing both the sidebar and the sidebar toggle button.
...@@ -473,7 +485,21 @@ public ChaosGameGui(Stage primaryStage) throws IOException { ...@@ -473,7 +485,21 @@ public ChaosGameGui(Stage primaryStage) throws IOException {
return this.canvas; return this.canvas;
} }
public Button getStartButton() {
return this.startButton;
}
public Button getStopButton() {
return this.stopButton;
}
public Button getNewButton() {
return this.newButton;
}
public Button getClearButton() {
return this.clearButton;
}
public Button getQuitButton() {
return this.quitButton;
}
......
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