Skip to content
Snippets Groups Projects
Commit b7fe86e5 authored by Vetle Solheim Hodne's avatar Vetle Solheim Hodne
Browse files

Fixed bug for changing min and max coordinate.

parent 0532af4b
No related branches found
No related tags found
No related merge requests found
package org.example.chaosgame.controller;
import javafx.scene.Node;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.util.Pair;
......@@ -15,14 +12,13 @@ import org.example.chaosgame.controller.observer.Subject;
import org.example.chaosgame.controller.observer.GameController;
import org.example.chaosgame.model.chaos.*;
import org.example.chaosgame.model.linalg.Complex;
import org.example.chaosgame.model.linalg.Matrix2x2;
import org.example.chaosgame.model.linalg.Vector2D;
import org.example.chaosgame.model.transformations.AffineTransform2D;
import org.example.chaosgame.model.transformations.JuliaTransform;
import org.example.chaosgame.model.transformations.Transform2D;
import org.example.chaosgame.view.ChaosPage;
import org.example.chaosgame.view.components.AlertUtility;
import org.example.chaosgame.view.components.CreateFractalDialog;
import org.example.chaosgame.view.components.MinMaxDialog;
import org.example.chaosgame.view.components.*;
import java.io.File;
import java.io.IOException;
......@@ -89,16 +85,13 @@ public class ChaosGameController implements Observer, Subject, GameController {
public void setMaxMinCoords() {
MinMaxDialog dialog = new MinMaxDialog();
Optional<Pair<String, String>> result = dialog.showAndWait();
Optional<List<String>> result = dialog.showAndWait();
if (result.isPresent()) {
Pair<String, String> minMax = result.get();
String[] minCoords = minMax.getKey().split(",");
String[] maxCoords = minMax.getValue().split(",");
try {
Vector2D min = new Vector2D(Double.parseDouble(minCoords[0]), Double.parseDouble(minCoords[1]));
Vector2D max = new Vector2D(Double.parseDouble(maxCoords[0]), Double.parseDouble(maxCoords[1]));
List<String> coords = result.get();
Vector2D min = new Vector2D(Double.parseDouble(coords.get(0)), Double.parseDouble(coords.get(1)));
Vector2D max = new Vector2D(Double.parseDouble(coords.get(2)), Double.parseDouble(coords.get(3)));
if (validateCoordinates(min) && validateCoordinates(max)) {
updateChaosGame(new ChaosGameDescription(
......@@ -108,13 +101,18 @@ public class ChaosGameController implements Observer, Subject, GameController {
AlertUtility.showErrorDialog("Invalid input", "Please enter a double between -50 and 50.");
}
} catch (NumberFormatException e) {
AlertUtility.showErrorDialog("Invalid input", "Please enter a valid number.");
} catch (IndexOutOfBoundsException e) {
AlertUtility.showErrorDialog("Invalid input", "Please enter all coordinates.");
}
}
}
private boolean validateCoordinates(Vector2D vector) {
try {
System.out.println("parsing" + vector.getX() + " " + vector.getY());
double x = vector.getX();
double y = vector.getY();
if (x < -50 || x > 50 || y < -50 || y > 50) {
......@@ -164,6 +162,21 @@ public class ChaosGameController implements Observer, Subject, GameController {
transforms));
} else if (fractalData instanceof Pair) {
Pair<String, String> userInput = (Pair<String, String>) fractalData;
try { // Check if the input is a valid number
double real = Double.parseDouble(userInput.getKey());
double imaginary = Double.parseDouble(userInput.getValue());
if (real < -1 || real > 1 || imaginary < -1 || imaginary > 1) {
AlertUtility.showErrorDialog("Invalid input", "Please enter a double between -1 and 1. No letters are allowed.");
} else {
updateChaosGame(new ChaosGameDescription(
new Vector2D(-1.6, -1),
new Vector2D(1.6, 1.0),
List.of(new JuliaTransform(new Complex(real, imaginary), 1))));
}
} catch (NumberFormatException e) {
AlertUtility.showErrorDialog("Invalid input", "Please enter a valid number.");
}
double real = Double.parseDouble(userInput.getKey());
double imaginary = Double.parseDouble(userInput.getValue());
......@@ -210,7 +223,6 @@ public class ChaosGameController implements Observer, Subject, GameController {
public void resetGame() {
chaosGame.resetTotalSteps();
//chaosGame.setChaosGameDescription(null);
update();
chaosPage.clearCanvas();
}
......
......@@ -7,9 +7,6 @@ import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import org.example.chaosgame.model.linalg.Matrix2x2;
import org.example.chaosgame.model.linalg.Vector2D;
import org.example.chaosgame.model.transformations.AffineTransform2D;
/**
* Pane for creating affine transformations.
......@@ -100,23 +97,15 @@ public class CreateAffinePane extends GridPane {
this.getChildren().remove(lastVectorBox);
}
/**
* Returns the list of affine transformations created in the pane.
*
* @return The list of affine transformations.
*/
public List<AffineTransform2D> getResult() {
List<AffineTransform2D> transformations = new ArrayList<>();
for (List<TextField> fields : lines) {
double a = Double.parseDouble(fields.get(0).getText());
double b = Double.parseDouble(fields.get(1).getText());
double c = Double.parseDouble(fields.get(2).getText());
double d = Double.parseDouble(fields.get(3).getText());
double x = Double.parseDouble(fields.get(4).getText());
double y = Double.parseDouble(fields.get(5).getText());
transformations.add(new AffineTransform2D(new Matrix2x2(a, b, c, d), new Vector2D(x, y)));
public List<List<String>> getResult() {
List<List<String>> result = new ArrayList<>();
for (List<TextField> line : lines) {
List<String> lineResult = new ArrayList<>();
for (TextField field : line) {
lineResult.add(field.getText());
}
result.add(lineResult);
}
return transformations;
return result;
}
}
\ No newline at end of file
package org.example.chaosgame.view.components;
import javafx.geometry.Insets;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
......
......@@ -5,8 +5,11 @@ import javafx.scene.control.Dialog;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.util.Pair;
import org.example.chaosgame.model.linalg.Vector2D;
public class MinMaxDialog extends Dialog<Pair<String, String>> {
import java.util.List;
public class MinMaxDialog extends Dialog<List<String>> {
private final TextField minXField;
private final TextField minYField;
private final TextField maxXField;
......@@ -46,7 +49,7 @@ public class MinMaxDialog extends Dialog<Pair<String, String>> {
String minY = minYField.getText();
String maxX = maxXField.getText();
String maxY = maxYField.getText();
return new Pair<>(minX + "," + minY, maxX + "," + maxY);
return List.of(minX, minY, maxX, maxY);
}
return null;
});
......
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