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

Delete ChaosGameButtonController, move button features to ChaosGameGUIView

parent 9b10daa1
No related branches found
No related tags found
No related merge requests found
package edu.ntnu.stud.chaosgame.controller;
import edu.ntnu.stud.chaosgame.view.ChaosGameGUIView;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.control.Button;
import javafx.scene.image.WritableImage;
import javafx.util.Duration;
import javafx.animation.Animation;
public class ChaosGameButtonController {
private final Timeline timeline;
public ChaosGameButtonController(ChaosGameGUIView view, Button startButton, Button stopButton, Button newButton, Button clearButton) {
this.timeline = new Timeline(new KeyFrame(Duration.seconds(0.05), event -> view.drawChaosGame()));
this.timeline.setCycleCount(Timeline.INDEFINITE);
startButton.setOnAction(event -> timeline.play());
stopButton.setOnAction(event -> timeline.stop());
newButton.setOnAction(event ->{
WritableImage newWritableImage = new WritableImage(view.getWidth(), view.getHeight());
view.setPixelWriter(newWritableImage.getPixelWriter());
view.setImageViewFromImage(newWritableImage);
});
clearButton.setOnAction(event -> {
view.getImageView().setImage(null);
view.setCurrentLine(0);
});
}
}
......@@ -91,9 +91,11 @@ public class ChaosCanvas {
int yIndex = (int) transformCoordsToIndices.transform(point).getX1() + yOffset;
if (xIndex >= 0 && xIndex < width && yIndex >= 0 && yIndex < height) {
canvas[xIndex][yIndex] = 1;
canvas[xIndex][yIndex] = 1;
} else {
System.out.println("Index out of bounds: " + xIndex + ", " + yIndex);
}
}
}
/**
* Get the canvas array.
......
......@@ -33,8 +33,8 @@ public class ChaosGameFileHandler {
try (Scanner scanner = new Scanner(Files.newInputStream(paths))) {
scanner.useLocale(Locale.ENGLISH);
// Use a delimiter that splits on commas and new lines, and ignores comment lines.
scanner.useDelimiter(",\\s*|\\r?\\n|(\\s*#.*\\s*)");
// Use a delimiter that splits on commas and new lines, and ignores # comments
scanner.useDelimiter(",\\s*|\\r?\\n|(\\s*#.*\\s*)");
String firstLine = scanner.next().trim();
......
package edu.ntnu.stud.chaosgame.view;
import edu.ntnu.stud.chaosgame.controller.ChaosGameButtonController;
import edu.ntnu.stud.chaosgame.controller.game.ChaosCanvas;
import edu.ntnu.stud.chaosgame.controller.game.ChaosGame;
import edu.ntnu.stud.chaosgame.controller.game.ChaosGameDescription;
import edu.ntnu.stud.chaosgame.model.generators.ChaosGameDescriptionFactory;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
......@@ -16,6 +17,7 @@ import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.util.Duration;
import java.io.IOException;
......@@ -29,9 +31,14 @@ public class ChaosGameGUIView {
private int height;
private ChaosGame game;
private final Timeline timeline;
public ChaosGameGUIView(Stage primaryStage) throws IOException {
this.timeline = new Timeline(new KeyFrame(Duration.seconds(0.05), event -> this.drawChaosGame()));
imageView = new ImageView();
width = 1100;
height = 1000;
......@@ -44,10 +51,25 @@ public class ChaosGameGUIView {
VBox sideMenu = new VBox();
Button startButton = new Button("Start");
startButton.setOnAction(event -> timeline.play());
Button stopButton = new Button("Stop");
stopButton.setOnAction(event -> timeline.stop());
Button newButton = new Button("New");
newButton.setOnAction(event ->{
WritableImage newWritableImage = new WritableImage(width, height);
setPixelWriter(newWritableImage.getPixelWriter());
setImageViewFromImage(newWritableImage);
});
Button clearButton = new Button("Clear");
ChaosGameButtonController chaosGameButtonController = new ChaosGameButtonController(this,startButton,stopButton,newButton,clearButton);
clearButton.setOnAction(event -> {
getImageView().setImage(null);
setCurrentLine(0);
});
sideMenu.getChildren().addAll(startButton,stopButton,newButton,clearButton);
......@@ -64,7 +86,7 @@ public class ChaosGameGUIView {
//TEMPORARY CODE to test Chaos Games in GUI
ChaosGameDescriptionFactory factory = new ChaosGameDescriptionFactory();
ChaosGameDescription description = factory.getDescriptions().get(2);
ChaosGameDescription description = factory.getDescriptions().get(0);
ChaosCanvas canvas = new ChaosCanvas(1000, 1000, description.getMinCoords(), description.getMaxCoords());
game = new ChaosGame(description, canvas);
......@@ -74,7 +96,7 @@ public class ChaosGameGUIView {
public void drawChaosGame(){
ChaosCanvas canvas = game.getCanvas();
game.runSteps(1000);
game.runSteps(100000);
// Test implementation for drawing fractals
int[][] betaArray = canvas.getCanvasArray();
......
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