diff --git a/src/main/java/edu/ntnu/stud/chaosgame/Main.java b/src/main/java/edu/ntnu/stud/chaosgame/Main.java index 7dfd16e0938d8e9e141a7210e123711e5739c6da..20194187fa3cb4039f9ca3b44fe6279bd08b4ecd 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/Main.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/Main.java @@ -25,63 +25,8 @@ public class Main extends Application { } public static void main(String[] args) throws IOException { - Scanner sc = new Scanner(System.in); - System.out.println("Hi"); launch(args); - String path = "src/main/resources/descriptions/templates/sierpinski.txt"; - - ChaosGameDescription description = null; // Declare description - - ChaosGame game = null; - - ChaosGameDescriptionFactory factory = new ChaosGameDescriptionFactory(); - - try { - //ChaosGameDescription description = new ChaosGameFileHandler().readFromFile(path); - //description = new ChaosGameDescription(minCoords, maxCoords, transforms); - //description = new ChaosGameFileHandler().readFromFile(path); - description = factory.getDescriptions().get(0); - ChaosCanvas canvas = new ChaosCanvas(100, 100, description.getMinCoords(), description.getMaxCoords()); // Create canvas - game = new ChaosGame(description, canvas); - - } catch (Exception e) { - System.out.println(e.getMessage()); // TODO: Alter error game - } - - - /* Test whether factory successfully instantiates descriptions. - ChaosGameDescriptionFactory factory = new ChaosGameDescriptionFactory(); - for (ChaosGameDescription desc : factory.getDescriptions()) { - System.out.println(desc); - }*/ - - boolean exit = false; - - // While loop for simple cli-app. - while (!exit) { - - System.out.println("What would you like to do? Type 1 for displaying fractal, " - + "or anything else for ending the program"); - if (Objects.equals(sc.next(), "1")) { - //game.runSteps(100000); - System.out.println( - "What would you like to do? Type 1 for saving the fractal, anything else for ending the program"); - - if (Objects.equals(sc.next(), "1")) { - ChaosGameFileHandler handler = new ChaosGameFileHandler(); - handler.writeToFile(description, "src/main/resources/descriptions/output/output_1.txt"); - System.out.println("Wrote to file output_1.txt."); - } - else { - exit = true; - } - } - else { - exit = true; - } - } - System.out.println("Terminated program."); } diff --git a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosCanvas.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosCanvas.java index 7b85c7089d791cc449843fab787587cd5387590d..9f8d47225815a877c648d7c1185604d7e917d727 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosCanvas.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosCanvas.java @@ -81,13 +81,16 @@ public class ChaosCanvas { * @param point the point where the pixel is to be placed. */ public void putPixel(Vector2D point) { - int xIndex = (int) (transformCoordsToIndices.transform(point).getX0()); - int yIndex = (int) (transformCoordsToIndices.transform(point).getX1()); + int xOffset = (int) Math.abs(minCoords.getX0()); + int yOffset = (int) Math.abs(minCoords.getX1()); - if (xIndex >= 0 && xIndex < width && yIndex >= 0 && yIndex < height ) { + int xIndex = (int) transformCoordsToIndices.transform(point).getX0() + xOffset; + int yIndex = (int) transformCoordsToIndices.transform(point).getX1() + yOffset; + + if (xIndex >= 0 && xIndex < width && yIndex >= 0 && yIndex < height) { canvas[xIndex][yIndex] = 1; } else { - //System.out.println("Index out of bounds: " + xIndex + ", " + yIndex); + System.out.println("Index out of bounds: " + xIndex + ", " + yIndex); } } diff --git a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGame.java b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGame.java index 9de1009716c9b939577fc5fcce12a0580ea0f480..1a8e4d0ca02b6adc06f1dd741a7dce30376c0d9d 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGame.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/controller/game/ChaosGame.java @@ -86,6 +86,15 @@ public class ChaosGame { */ public Vector2D getCurrentPoint() { return this.currentPoint; } + /** + * Get the chaos game description. + * + * @param description the description to set. + */ + public void setDescription(ChaosGameDescription description) { + this.description = description; + } + /** * Notify the observers that a change has occurred in the ChaosGame. @@ -117,7 +126,8 @@ public class ChaosGame { this.currentPoint = this.description.getTransforms().get(j).transform(currentPoint); //System.out.println("After transform: " + currentPoint.getX0() + " " + currentPoint.getX1()); - this.canvas.putPixel(currentPoint);} + this.canvas.putPixel(currentPoint); + } //this.canvas.printCanvas(); } diff --git a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGUIView.java b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGUIView.java index ec513b9310e556c54f90d1a611a6897ba70ad739..c9d612ba20d53a9267e2b4907ffdfe63dd5d5308 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGUIView.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameGUIView.java @@ -43,24 +43,18 @@ public class ChaosGameGUIView { public ChaosGameGUIView(Stage primaryStage) throws IOException { - this.timeline = new Timeline(new KeyFrame(Duration.seconds(2), event -> this.drawChaosGame())); + this.timeline = new Timeline(new KeyFrame(Duration.seconds(0.05), event -> this.drawChaosGame())); imageView = new ImageView(); width = 1000; height = 1000; - canvasWidth = 1000; - canvasHeight = 1000; - WritableImage writableImage = new WritableImage(width, height); pixelWriter = writableImage.getPixelWriter(); imageView.setImage(writableImage); - imageView.setFitHeight(width); - imageView.setFitWidth(height); - VBox sideMenu = new VBox(); @@ -68,11 +62,11 @@ public class ChaosGameGUIView { ChaosGameDescriptionFactory factory = new ChaosGameDescriptionFactory(); AtomicReference<ChaosGameDescription> description = new AtomicReference<>(factory.getDescriptions().get(0)); - ChaosCanvas canvas = new ChaosCanvas(canvasWidth, canvasHeight, description.get().getMinCoords(), description.get().getMaxCoords()); + ChaosCanvas canvas = new ChaosCanvas(1000, 1000, description.get().getMinCoords(), description.get().getMaxCoords()); game = new ChaosGame(description.get(), canvas); Button startButton = new Button("Start"); - startButton.setOnAction(event -> this.drawChaosGame()); + startButton.setOnAction(event -> timeline.play()); Button stopButton = new Button("Stop"); stopButton.setOnAction(event -> timeline.stop()); @@ -114,7 +108,7 @@ public class ChaosGameGUIView { canvas.clearCanvas(); description.set(factory.getDescriptions().get(0)); // Assuming the Sierpinski description is at index 0 - canvasRef.set(new ChaosCanvas(canvasWidth, canvasHeight, description.get().getMinCoords(), description.get().getMaxCoords())); + canvasRef.set(new ChaosCanvas(1000, 1000, description.get().getMinCoords(), description.get().getMaxCoords())); game = new ChaosGame(description.get(), canvasRef.get()); }); @@ -126,7 +120,7 @@ public class ChaosGameGUIView { setImageViewFromImage(newWritableImage); canvas.clearCanvas(); description.set(factory.getDescriptions().get(1)); // Assuming the Sierpinski description is at index 0 - canvasRef.set(new ChaosCanvas(canvasWidth, canvasHeight, description.get().getMinCoords(), description.get().getMaxCoords())); + canvasRef.set(new ChaosCanvas(1000, 1000, description.get().getMinCoords(), description.get().getMaxCoords())); game = new ChaosGame(description.get(), canvasRef.get()); }); @@ -138,7 +132,7 @@ public class ChaosGameGUIView { setImageViewFromImage(newWritableImage); canvas.clearCanvas(); description.set(factory.getDescriptions().get(2)); // Assuming the Sierpinski description is at index 0 - canvasRef.set(new ChaosCanvas(canvasWidth, canvasHeight, description.get().getMinCoords(), description.get().getMaxCoords())); + canvasRef.set(new ChaosCanvas(1000, 1000, description.get().getMinCoords(), description.get().getMaxCoords())); game = new ChaosGame(description.get(), canvasRef.get()); }); @@ -207,13 +201,15 @@ public class ChaosGameGUIView { primaryStage.show(); + + } public void drawChaosGame(){ ChaosCanvas canvas = game.getCanvas(); - game.runSteps(1000000); + game.runSteps(100000); // Test implementation for drawing fractals int[][] betaArray = canvas.getCanvasArray(); diff --git a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameObserver.java b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameObserver.java index 24ab6a37de32be6c0e83ee0f0ad346f67c5241be..a9869bde642ec9a5dc4a61552501d03c451352c1 100644 --- a/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameObserver.java +++ b/src/main/java/edu/ntnu/stud/chaosgame/view/ChaosGameObserver.java @@ -1,6 +1,7 @@ package edu.ntnu.stud.chaosgame.view; import edu.ntnu.stud.chaosgame.controller.game.ChaosGame; +import edu.ntnu.stud.chaosgame.controller.game.ChaosGameDescription; /** * Observer interface for monitoring changes to the active @@ -23,6 +24,7 @@ public interface ChaosGameObserver { * @param description the description. */ //void updateDescription(ChaosGameDescription description); + void updateDescription(ChaosGameDescription description); /** * Update the observer based on changes to the chaos game.