Skip to content
Snippets Groups Projects
Commit eea18b70 authored by Edvard Granheim Harbo's avatar Edvard Granheim Harbo
Browse files

Merge branch 'task.2.6' into 'dev'

Started on javaFX

See merge request !20
parents 0558b21c bb8387c4
No related branches found
No related tags found
2 merge requests!54Final release,!20Started on javaFX
package edu.ntnu.idatt2003.mappevurderingprog2.enums;
/**
* An enum representing the buttons in the application
*/
public enum ButtonEnum {
Transform,
ClearTransformation,
ZoomIn,
ZoomOut,
ResetZoom,
RotateTransformationLeft,
RotateTransformationRight
}
...@@ -7,6 +7,8 @@ import edu.ntnu.idatt2003.mappevurderingprog2.models.Matrix2x2; ...@@ -7,6 +7,8 @@ import edu.ntnu.idatt2003.mappevurderingprog2.models.Matrix2x2;
import edu.ntnu.idatt2003.mappevurderingprog2.models.Transform2D; import edu.ntnu.idatt2003.mappevurderingprog2.models.Transform2D;
import edu.ntnu.idatt2003.mappevurderingprog2.models.UserInterface; import edu.ntnu.idatt2003.mappevurderingprog2.models.UserInterface;
import edu.ntnu.idatt2003.mappevurderingprog2.models.Vector2D; import edu.ntnu.idatt2003.mappevurderingprog2.models.Vector2D;
import edu.ntnu.idatt2003.mappevurderingprog2.views.Components.JuliaTransformationButton;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
...@@ -19,8 +21,10 @@ public class ChaosGame { ...@@ -19,8 +21,10 @@ public class ChaosGame {
private ChaosCanvas canvas; private ChaosCanvas canvas;
private ChaosGameDescription description; private ChaosGameDescription description;
private ChaosGameDescription juliaTransformDescription;
private Vector2D currentPoint; private Vector2D currentPoint;
public Random random; private Random random;
public ChaosGame(ChaosGameDescription description, int width, int height) { public ChaosGame(ChaosGameDescription description, int width, int height) {
this.description = description; this.description = description;
...@@ -55,4 +59,14 @@ public class ChaosGame { ...@@ -55,4 +59,14 @@ public class ChaosGame {
ChaosGameDescription description = new ChaosGameDescription(transforms, new Vector2D(0, 0), new Vector2D(1, 1)); ChaosGameDescription description = new ChaosGameDescription(transforms, new Vector2D(0, 0), new Vector2D(1, 1));
return description; return description;
} }
public static ChaosGameDescription createJuliaChaosGameDescription() {
List<Transform2D> transforms = new ArrayList<>();
// Add the JuliaTransform to the list of transforms
transforms.add(new JuliaTransform(new Complex(-0.74543, 0.11301), -1));
ChaosGameDescription juliaTransformDescription = new ChaosGameDescription(
transforms, new Vector2D(-1.6, -1.0), new Vector2D(1.6, 1.0));
return juliaTransformDescription;
}
} }
\ No newline at end of file
...@@ -5,5 +5,5 @@ import javafx.scene.paint.Color; ...@@ -5,5 +5,5 @@ import javafx.scene.paint.Color;
public final class Colorpalette{ public final class Colorpalette{
public static final Color White = Color.web("#FFFFFF"); public static final Color White = Color.web("#FFFFFF");
public static final Color Black = Color.web("#000000"); public static final Color Black = Color.web("#000000");
public static final Color Primary = Color.web("#778899"); public static final Color Primary = Color.web("#7971EA");
} }
\ No newline at end of file
package edu.ntnu.idatt2003.mappevurderingprog2.views.Components;
import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosCanvas;
import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGame;
import edu.ntnu.idatt2003.mappevurderingprog2.utils.Colorpalette;
import edu.ntnu.idatt2003.mappevurderingprog2.views.View;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
public class AffineTransformationButton extends Menu {
private final GraphicsContext gc;
public int[][] canvas;
private ChaosGame chaosGame;
private View view;
private Canvas affineTransformationCanvas; // Declare the Canvas object
public AffineTransformationButton(ChaosGame chaosGame, View view, Canvas affineTransformationCanvas) {
super("Affine Transformation"); // Set the name of the Menu
this.chaosGame = chaosGame;
this.view = view;
this.affineTransformationCanvas = affineTransformationCanvas; // Initialize the Canvas object
this.gc = affineTransformationCanvas.getGraphicsContext2D(); // Initialize the GraphicsContext object
MenuItem transformItem = new MenuItem("Transform");
transformItem.setOnAction(event -> performTransformation());
this.getItems().add(transformItem);
}
private void performTransformation() {
// Run steps on the existing ChaosGame instance
chaosGame.runSteps(20000);
// Get the ChaosCanvas and draw it on your application's canvas
ChaosCanvas chaosCanvas = chaosGame.getCanvas();
redrawCanvas(chaosCanvas); // Make sure the View class has a redrawCanvas method that accepts a ChaosCanvas object
redrawCanvas(chaosCanvas);
}
private void redrawCanvas(ChaosCanvas chaosCanvas) {
int[][] newCanvas = chaosCanvas.getCanvasArray();
this.canvas = newCanvas;
// Get the width and height of the canvas
double canvasWidth = affineTransformationCanvas.getWidth() - 20;
double canvasHeight = affineTransformationCanvas.getHeight() - 20;
// Calculate the size of each pixel
double pixelWidth = canvasWidth / canvas[0].length;
double pixelHeight = canvasHeight / canvas.length;
// Loop through each pixel in the canvas array
for (int i = 0; i < canvas.length; i++) {
for (int j = 0; j < canvas[i].length; j++) {
// Calculate the coordinates of the pixel on the canvas
double x = j * pixelWidth + 3;
double y = i * pixelHeight + 3;
// Set the fill color based on the pixel value
if (canvas[i][j] == 0) {
gc.setFill(Colorpalette.White);
} else {
gc.setFill(Colorpalette.Black);
}
// Fill a rectangle representing the pixel
gc.fillRect(x, y, pixelWidth, pixelHeight);
}
}
}
}
\ No newline at end of file
package edu.ntnu.idatt2003.mappevurderingprog2.views.Components;
import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosCanvas;
import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGame;
import edu.ntnu.idatt2003.mappevurderingprog2.utils.Colorpalette;
import edu.ntnu.idatt2003.mappevurderingprog2.views.View;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
public class JuliaTransformationButton extends Menu {
private final GraphicsContext gc;
public int[][] canvas;
private ChaosGame chaosGame;
private View view;
private Canvas juliaTransformationCanvas;
public JuliaTransformationButton (ChaosGame chaosGame, View view, Canvas juliaTransformationCanvas) {
super("Julia Transformation");
this.chaosGame = chaosGame;
this.view = view;
this.juliaTransformationCanvas = juliaTransformationCanvas;
this.gc = juliaTransformationCanvas.getGraphicsContext2D();
MenuItem transformItem = new MenuItem("Transform");
transformItem.setOnAction(event -> performTransformation());
this.getItems().add(transformItem);
}
private void performTransformation() {
chaosGame.runSteps(20000);
ChaosCanvas chaosCanvas = chaosGame.getCanvas();
redrawCanvas(chaosCanvas);
redrawCanvas(chaosCanvas);
}
private void redrawCanvas(ChaosCanvas chaosCanvas) {
int[][] newCanvas = chaosCanvas.getCanvasArray();
this.canvas = newCanvas;
// Get the width and height of the canvas
double canvasWidth = juliaTransformationCanvas.getWidth() - 20;
double canvasHeight = juliaTransformationCanvas.getHeight() - 20;
// Calculate the size of each pixel
double pixelWidth = canvasWidth / canvas[0].length;
double pixelHeight = canvasHeight / canvas.length;
// Loop through each pixel in the canvas array
for (int i = 0; i < canvas.length; i++) {
for (int j = 0; j < canvas[i].length; j++) {
// Calculate the coordinates of the pixel on the canvas
double x = j * pixelWidth + 3;
double y = i * pixelHeight + 3;
// Set the fill color based on the pixel value
if (canvas[i][j] == 0) {
gc.setFill(Colorpalette.White);
} else {
gc.setFill(Colorpalette.Black);
}
// Fill a rectangle representing the pixel
gc.fillRect(x, y, pixelWidth, pixelHeight);
}
}
}
}
...@@ -2,30 +2,26 @@ package edu.ntnu.idatt2003.mappevurderingprog2.views.Components; ...@@ -2,30 +2,26 @@ package edu.ntnu.idatt2003.mappevurderingprog2.views.Components;
import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGame; import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGame;
import edu.ntnu.idatt2003.mappevurderingprog2.views.View; import edu.ntnu.idatt2003.mappevurderingprog2.views.View;
import javafx.scene.control.Button; import javafx.scene.control.Menu;
import javafx.scene.control.ToolBar; import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
// In your MainBar class public class MainBar extends MenuBar {
public class MainBar extends ToolBar {
private ChaosGame chaosGame; private ChaosGame chaosGame;
private View view; private View view;
public MainBar(ChaosGame chaosGame, View view){ public MainBar(ChaosGame chaosGame, ChaosGame chaosGame1, View view) {
this.chaosGame = chaosGame; this.chaosGame = chaosGame;
this.view = view; this.view = view;
Button transformButton = new Button(); AffineTransformationButton affineTransformationButton = new AffineTransformationButton(
transformButton.setText("Transform"); chaosGame, view, view.getAffineTransformationCanvas());
transformButton.setOnAction(event -> performTransformation()); this.getMenus().add(affineTransformationButton);
getItems().add(transformButton);
}
private void performTransformation() {
// Run steps on the existing ChaosGame instance
chaosGame.runSteps(10000);
// Get the ChaosCanvas and draw it on your application's canvas JuliaTransformationButton juliaTransformationButton = new JuliaTransformationButton(
view.redrawCanvas(chaosGame.getCanvas()); chaosGame1, view, view.getJuliaTransformationCanvas());
this.getMenus().add(juliaTransformationButton);
} }
} }
...@@ -4,71 +4,53 @@ import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGame; ...@@ -4,71 +4,53 @@ import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGame;
import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGameDescription; import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosGameDescription;
import edu.ntnu.idatt2003.mappevurderingprog2.utils.Colorpalette; import edu.ntnu.idatt2003.mappevurderingprog2.utils.Colorpalette;
import edu.ntnu.idatt2003.mappevurderingprog2.views.Components.MainBar; import edu.ntnu.idatt2003.mappevurderingprog2.views.Components.MainBar;
import edu.ntnu.idatt2003.mappevurderingprog2.models.chaos.ChaosCanvas;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.canvas.Canvas; import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Background; import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill; import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.BorderPane;
import javafx.scene.layout.CornerRadii; import javafx.scene.layout.CornerRadii;
// In your View class
public class View extends BorderPane { public class View extends BorderPane {
public int [][] canvas;
private Canvas affineTransformationCanvas; private Canvas affineTransformationCanvas;
private GraphicsContext gc; private Canvas juliaTransformationCanvas;
public Canvas getAffineTransformationCanvas() {
return affineTransformationCanvas;
}
public Canvas getJuliaTransformationCanvas() {
return juliaTransformationCanvas;
}
public Scene createScene() { public Scene createScene() {
this.setBackground(new Background(new BackgroundFill(Colorpalette.Primary, CornerRadii.EMPTY, Insets.EMPTY))); this.setBackground(new Background(new BackgroundFill(Colorpalette.Primary, CornerRadii.EMPTY, Insets.EMPTY)));
ChaosGameDescription description = ChaosGame.createAffineChaosGameDescription(); ChaosGameDescription description = ChaosGame.createAffineChaosGameDescription();
ChaosGameDescription juliaTransformDescription = ChaosGame.createJuliaChaosGameDescription();
ChaosGame chaosGame = new ChaosGame(description, 600, 600); ChaosGame chaosGame = new ChaosGame(description, 600, 600);
ChaosGame chaosGame1 = new ChaosGame(juliaTransformDescription, 600, 600);
affineTransformationCanvas = new Canvas(600, 400); affineTransformationCanvas = new Canvas(600, 400);
juliaTransformationCanvas = new Canvas(600, 400);
gc = affineTransformationCanvas.getGraphicsContext2D();
MainBar mainBar = new MainBar(chaosGame, this); MainBar mainBar = new MainBar(chaosGame, chaosGame1, this);
this.setTop(mainBar); this.setTop(mainBar);
this.setCenter(juliaTransformationCanvas);
this.setCenter(affineTransformationCanvas); this.setCenter(affineTransformationCanvas);
return new Scene(this, 900, 700); return new Scene(this, 900, 700);
} }
public void redrawCanvas(ChaosCanvas chaosCanvas) {
int[][] newCanvas = chaosCanvas.getCanvasArray();
this.canvas = newCanvas;
// Get the width and height of the canvas
double canvasWidth = affineTransformationCanvas.getWidth() - 20;
double canvasHeight = affineTransformationCanvas.getHeight() - 20;
// Calculate the size of each pixel
double pixelWidth = canvasWidth / canvas[0].length;
double pixelHeight = canvasHeight / canvas.length;
// Loop through each pixel in the canvas array
for (int i = 0; i < canvas.length; i++) {
for (int j = 0; j < canvas[i].length; j++) {
// Calculate the coordinates of the pixel on the canvas
double x = j * pixelWidth + 3;
double y = i * pixelHeight + 3;
// Set the fill color based on the pixel value
if (canvas[i][j] == 0) {
gc.setFill(Colorpalette.White);
} else {
gc.setFill(Colorpalette.Black);
}
// Fill a rectangle representing the pixel
gc.fillRect(x, y, pixelWidth, pixelHeight);
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment