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

Add GUI for drawing fractals.

parent 01ae6743
No related branches found
No related tags found
No related merge requests found
......@@ -14,22 +14,31 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version> 21.0.1</version>
<scope>main</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>21.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>21.0.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
......@@ -61,6 +70,7 @@
<version>3.4.1</version>
</plugin>
</plugins>
</build>
......
package edu.ntnu.stud.chaosgame;
import edu.ntnu.stud.chaosgame.controller.ChaosGameButtonController;
import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription;
import edu.ntnu.stud.chaosgame.model.game.ChaosGameFileHandler;
import edu.ntnu.stud.chaosgame.model.game.ChaosCanvas;
import edu.ntnu.stud.chaosgame.model.game.ChaosGame;
import edu.ntnu.stud.chaosgame.model.generators.ChaosGameDescriptionFactory;
import edu.ntnu.stud.chaosgame.view.ChaosGameGUIView;
import javafx.application.Application;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.Objects;
import java.util.Scanner;
......@@ -12,11 +17,19 @@ import java.util.Scanner;
/**
* Main class for the Chaos Game application.
*/
public class Main {
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
ChaosGameGUIView view = new ChaosGameGUIView(primaryStage);
}
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
......@@ -37,6 +50,7 @@ public class Main {
System.out.println(e.getMessage()); // TODO: Alter error game
}
/* Test whether factory successfully instantiates descriptions.
ChaosGameDescriptionFactory factory = new ChaosGameDescriptionFactory();
for (ChaosGameDescription desc : factory.getDescriptions()) {
......@@ -51,7 +65,7 @@ public class Main {
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);
//game.runSteps(100000);
System.out.println(
"What would you like to do? Type 1 for saving the fractal, anything else for ending the program");
......
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.01), event -> view.drawChaosGame(10)));
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);
});
}
}
......@@ -130,4 +130,12 @@ public class ChaosCanvas {
System.out.println();
}
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
......@@ -64,7 +64,7 @@ public class ChaosGame {
//System.out.println(currentPoint.getX0() + " " + currentPoint.getX1());
this.canvas.putPixel(currentPoint);
}
this.canvas.printCanvas();
//this.canvas.printCanvas();
}
}
......@@ -29,7 +29,7 @@ public class ChaosGameDescription {
* Constructor for ChaosGameDescription
* @param minCoords Inputs a {@link Vector2D} vector of lower left coordinates for the chaos game.
* @param maxCoords Inputs a {@link Vector2D} vector of upper right coordinates for the chaos game.
* @param transforms Inputs a list of transformations {@link AffineTransform2D} used in the chaos game.
* @param transforms Inputs a list of transformations {@link edu.ntnu.stud.chaosgame.model.transformations.AffineTransform2D} used in the chaos game.
*/
public ChaosGameDescription(Vector2D minCoords, Vector2D maxCoords, List<Transform2D> transforms) {
if (minCoords == null || maxCoords == null || transforms == null){
......
package edu.ntnu.stud.chaosgame.view;
import edu.ntnu.stud.chaosgame.controller.ChaosGameButtonController;
import edu.ntnu.stud.chaosgame.model.game.ChaosCanvas;
import edu.ntnu.stud.chaosgame.model.game.ChaosGame;
import edu.ntnu.stud.chaosgame.model.game.ChaosGameDescription;
import edu.ntnu.stud.chaosgame.model.generators.ChaosGameDescriptionFactory;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import java.io.IOException;
public class ChaosGameGUIView {
private int currentLine = 0;
private PixelWriter pixelWriter;
private ImageView imageView;
private int width;
private int height;
private ChaosGame game;
public ChaosGameGUIView(Stage primaryStage) throws IOException {
imageView = new ImageView();
width = 1100;
height = 1000;
WritableImage writableImage = new WritableImage(width, height);
pixelWriter = writableImage.getPixelWriter();
imageView.setImage(writableImage);
VBox sideMenu = new VBox();
Button startButton = new Button("Start");
Button stopButton = new Button("Stop");
Button newButton = new Button("New");
Button clearButton = new Button("Clear");
ChaosGameButtonController chaosGameButtonController = new ChaosGameButtonController(this,startButton,stopButton,newButton,clearButton);
sideMenu.getChildren().addAll(startButton,stopButton,newButton,clearButton);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(imageView);
borderPane.setRight(sideMenu);
Scene scene = new Scene(borderPane,1700,1000);
primaryStage.setTitle("Fractal Chaos Game");
primaryStage.setScene(scene);
primaryStage.show();
//TEMPORARY CODE to test Chaos Games in GUI
ChaosGameDescriptionFactory factory = new ChaosGameDescriptionFactory();
ChaosGameDescription description = factory.getDescriptions().get(2);
ChaosCanvas canvas = new ChaosCanvas(1000, 1000, description.getMinCoords(), description.getMaxCoords());
game = new ChaosGame(description, canvas);
}
public void drawChaosGame(int numLines){
ChaosCanvas canvas = game.getCanvas();
game.runSteps(2000);
// Test implementation for drawing fractals
int[][] betaArray = canvas.getCanvasArray();
for (int i = 0; i < canvas.getWidth(); i++) {
for (int j = 0; j < canvas.getHeight(); j++) {
if (betaArray[i][j] == 1) {
pixelWriter.setColor(j,i,Color.BLACK);
}
}
}
}
public int getWidth(){
return this.width;
}
public int getHeight(){
return this.height;
}
public ImageView getImageView(){
return this.imageView;
}
public void setCurrentLine(int currentLine) {
this.currentLine = currentLine;
}
public void setPixelWriter(PixelWriter pixelWriter) {
this.pixelWriter = pixelWriter;
}
public void setImageViewFromImage(Image inputView) {
this.imageView.setImage(inputView);
}
}
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