Skip to content
Snippets Groups Projects
Commit 3918fa08 authored by Håvard Daleng's avatar Håvard Daleng
Browse files

Phased out the implementation of ChaosGameGui assuming multithreading issues (AtomicReferences).

parent 951423fb
No related branches found
No related tags found
No related merge requests found
......@@ -15,6 +15,7 @@ import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
......@@ -28,7 +29,6 @@ import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;
public class ChaosGameGui implements ChaosGameObserver {
private int currentLine = 0;
......@@ -51,11 +51,6 @@ public class ChaosGameGui implements ChaosGameObserver {
*/
private ChaosGameDescriptionFactory factory;
/**
* The AtomicReference for the ChaosGameDescription.
*/
private AtomicReference<ChaosGameDescription> descriptionRef;
/**
* The ImageView for the GUI..
*/
......@@ -188,10 +183,10 @@ public class ChaosGameGui implements ChaosGameObserver {
private void initializeGameComponents() {
// Description
this.factory = new ChaosGameDescriptionFactory();
this.descriptionRef = new AtomicReference<>(factory.getDescriptions().get(0));
this.description = descriptionRef.get();
this.description = factory.getDescriptions().get(0);
this.chaosCanvas = new ChaosCanvas(100, 100, descriptionRef.get().getMinCoords(), descriptionRef.get().getMaxCoords());
this.chaosCanvas = new ChaosCanvas(100, 100, this.description.getMinCoords(),
this.description.getMaxCoords());
game = new ChaosGame(this.description, chaosCanvas);
}
......@@ -204,9 +199,9 @@ public class ChaosGameGui implements ChaosGameObserver {
width = 1000;
height = 1000;
this.canvas = new Canvas(width, height);
this.imageView.setImage(canvas.snapshot(null, null));
//this.imageView.setImage(canvas.snapshot(null, null));
//this.clearImageView();
this.clearImageView();
}
......@@ -214,19 +209,11 @@ public class ChaosGameGui implements ChaosGameObserver {
* Color the entire image view white.
*/
private void clearImageView() {
// Color the image white
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
this.canvas.getGraphicsContext2D().setFill(Color.WHITE);
this.canvas.getGraphicsContext2D().fillRect(i, j, 1, 1);
//this.imageView.setImage(canvas.snapshot(null, null));
}
}
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
imageView.setImage(null);
}
/**
* Initialize the buttons related to managing the fractals.
*/
......@@ -245,9 +232,6 @@ public class ChaosGameGui implements ChaosGameObserver {
improvedBarnsleyButton.setToggleGroup(group);
AtomicReference<ChaosCanvas> canvasRef = new AtomicReference<>(chaosCanvas);
// Set action for Sierpinski radio button.
sierpinskiRadioButton.setOnAction(event -> {
this.updateDescription(0);
......@@ -265,16 +249,6 @@ public class ChaosGameGui implements ChaosGameObserver {
improvedBarnsleyButton.setOnAction(event -> {
this.updateDescription(3);
/*for (int i = 0; i < this.description.getTransforms().size(); i++) {
System.out.println(((AffineTransform2D) this.description.getTransforms().get(i)).getMatrix().getA00());
System.out.println(((AffineTransform2D) this.description.getTransforms().get(i)).getMatrix().getA01());
System.out.println(((AffineTransform2D) this.description.getTransforms().get(i)).getMatrix().getA10());
System.out.println(((AffineTransform2D) this.description.getTransforms().get(i)).getMatrix().getA11());
System.out.println(((AffineTransform2D) this.description.getTransforms().get(i)).getVector().getX0());
System.out.println(((AffineTransform2D) this.description.getTransforms().get(i)).getVector().getX1());
}*/
});
// Load fractal file button
......@@ -463,19 +437,18 @@ public class ChaosGameGui implements ChaosGameObserver {
*/
@Override
public void updateDescription(int index) {
AtomicReference<ChaosCanvas> canvasRef = new AtomicReference<>(chaosCanvas);
timeline.stop();
canvasRef.get().clearCanvas();
this.chaosCanvas.clearCanvas();
this.canvas.getGraphicsContext2D().clearRect(0, 0, this.canvas.getGraphicsContext2D().
getCanvas().getWidth(), this.canvas.getGraphicsContext2D().getCanvas().getHeight());
this.clearImageView();
chaosCanvas.clearCanvas();
this.chaosCanvas.clearCanvas();
this.descriptionRef.set(factory.getDescriptions().get(index)); // Assuming the Sierpinski description is at index 0
canvasRef.set(new ChaosCanvas(1000, 1000, this.descriptionRef.get().getMinCoords(),
this.descriptionRef.get().getMaxCoords()));
this.updateCanvas(canvasRef.get());
game = new ChaosGame(this.descriptionRef.get(), canvasRef.get());
this.description = this.factory.getDescriptions().get(index); // Assuming the Sierpinski description is at index 0
this.chaosCanvas = new ChaosCanvas(1000, 1000, this.description.getMinCoords(),
this.description.getMaxCoords());
this.updateCanvas(this.chaosCanvas);
game = new ChaosGame(this.description, this.chaosCanvas);
//this.game.setDescription(description);
}
......@@ -485,6 +458,7 @@ public class ChaosGameGui implements ChaosGameObserver {
*
* @param canvas the canvas to update with.
*/
@Override
public void updateCanvas(ChaosCanvas canvas) {
float zoomRatio = (float) this.chaosCanvas.getHeight() / canvas.getHeight();
//this.imageView.fixedZoom(zoomRatio); // Set new zoom factor.
......
......@@ -130,7 +130,7 @@ public class ChaosGameImageView extends ImageView {
PopupManager.displayError("Zoom error", e.getMessage());
}
}
// TODO: remove if unused
private void updateController() {
this.controller.updateDetail(this.zoomLevel, this.centreX, this.centreY);
}
......
package edu.ntnu.stud.chaosgame.view;
import edu.ntnu.stud.chaosgame.controller.game.ChaosGame;
import edu.ntnu.stud.chaosgame.controller.game.ChaosCanvas;
import edu.ntnu.stud.chaosgame.controller.game.ChaosGameDescription;
/**
......@@ -8,15 +9,6 @@ import edu.ntnu.stud.chaosgame.controller.game.ChaosGameDescription;
* TODO: Do we want to have separate update methods for the canvas and description or just one for the whole game? (likely the latter)
*/
public interface ChaosGameObserver {
// TODO: Create interface
/**
* Perform update of the ChaosCanvas.
*
* @param canvas the canvas.
*/
//void updateCanvas(Chaosanvas canvas);
/**
* Perform update of the ChaosGameDescription.
......@@ -26,6 +18,13 @@ public interface ChaosGameObserver {
//void updateDescription(ChaosGameDescription description);
void updateDescription(int index);
/**
* Update the ChaosCanvas.
*
* @param canvas the canvas to update with.
*/
void updateCanvas(ChaosCanvas canvas);
/**
* Update the observer based on changes to the chaos game.
*
......
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