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