Skip to content
Snippets Groups Projects
Commit d1e51518 authored by Emil Ruud's avatar Emil Ruud
Browse files

Merge branch '18-writetofilefix' into 'dev'

Resolve "writetofileFIx"

See merge request !27
parents 76aa1b51 5ff92ed3
Branches issue-10-fxmapcontrol
No related tags found
2 merge requests!78Development,!27Resolve "writetofileFIx"
Pipeline #286892 passed with stages
in 38 seconds
package edu.ntnu.idatt2003.controller;
import edu.ntnu.idatt2003.view.scenes.StartScene;
import javafx.scene.control.Button;
public class ButtonController {
private StartScene startScene;
public ButtonController(StartScene startScene) {
this.startScene = startScene;
}
public void initialize() {
setButtonHandlers();
}
private void setButtonHandlers() {
Button btnAdd10 = startScene.getBtnAdd10();
btnAdd10.setOnAction(e -> {
System.out.println("Add 10");
});
// Set handlers for other buttons if needed
Button btnAdd100 = startScene.getBtnAdd100();
btnAdd100.setOnAction(e -> {
System.out.println("Add 100");
});
Button btnAdd1000 = startScene.getBtnAdd1000();
btnAdd1000.setOnAction(e -> {
System.out.println("Add 1000");
});
Button btnReset = startScene.getBtnReset();
btnReset.setOnAction(e -> {
System.out.println("Reset");
});
}
}
......@@ -29,6 +29,14 @@ public ChaosCanvas(int width, int height, Vector2D minCoords, Vector2D maxCoords
initializeTransform();
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
/**
* Initializes the transformation from coordinates to indices.
*/
......
package edu.ntnu.idatt2003.view;
import edu.ntnu.idatt2003.controller.ButtonController;
import edu.ntnu.idatt2003.view.scenes.StartScene;
import javafx.application.Application;
import javafx.stage.Stage;
......@@ -8,9 +9,14 @@ public class App extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start (Stage primaryStage) {
public void start(Stage primaryStage) {
StartScene startScene = new StartScene();
startScene.start(primaryStage);
startScene.setUpStage(primaryStage);
// Initialize the controller and set up handlers
ButtonController buttonController = new ButtonController(startScene);
buttonController.initialize();
}
}
package edu.ntnu.idatt2003.view.scenes;
import edu.ntnu.idatt2003.model.*;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.control.ComboBox;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.util.List;
public class StartScene {
public void start(Stage primaryStage) {
private Button btnAdd10;
private Button btnAdd100;
private Button btnAdd1000;
private Button btnReset;
private ComboBox<String> comboBoxFractal;
private ComboBox<String> comboBoxColor;
private Button btnChangeStartVector;
private Scene scene;
//private ChaosCanvas chaosCanvas;
private WritableImage writableImage;
private ImageView imageView;
private ChaosCanvas chaosCanvas;
public StartScene() {
initializeComponents();
drawFractal();
}
private void initializeComponents() {
// Creating the root pane and setting its size
BorderPane root = new BorderPane();
// HBox for buttons
HBox buttonBox = new HBox(50); // spacing between buttons
HBox topBox = new HBox(50); // spacing between buttons
buttonBox.getStyleClass().add("button-box");
topBox.getStyleClass().add("top-box");
// Initialize buttons
btnAdd10 = new Button("Add 10");
btnAdd100 = new Button("Add 100");
btnAdd1000 = new Button("Add 1000");
btnReset = new Button("Reset");
btnChangeStartVector = new Button("Change start vector");
Button startButton = new Button("Start Game");
StackPane root = new StackPane();
root.getChildren().add(startButton);
comboBoxFractal = new ComboBox<>();
comboBoxFractal.setItems(FXCollections.observableArrayList(
"Option 1", "Option 2", "Option 3"
));
comboBoxFractal.setPromptText("Choose a fractal");
Scene scene = new Scene(root, 1300, 500);
comboBoxColor = new ComboBox<>();
comboBoxColor.setItems(FXCollections.observableArrayList(
"Blue", "Green", "Red"
));
comboBoxColor.setPromptText("Choose a color");
// Add buttons to the HBox
buttonBox.getChildren().addAll(btnAdd10, btnAdd100, btnAdd1000, btnReset, btnChangeStartVector);
buttonBox.setPadding(new Insets(0, 0, 50, 50));
topBox.getChildren().addAll(comboBoxFractal, comboBoxColor);
topBox.setPadding(new Insets(20, 0, 0, 50));
// Aligning the HBox at the bottom
root.setBottom(buttonBox);
root.setTop(topBox);
// Create a scene with the root and set the CSS style
scene = new Scene(root, 800, 600);
scene.getStylesheets().add(getClass().getResource("/style.css").toExternalForm());
// Initialize the ChaosCanvas and the WritableImage
chaosCanvas = new ChaosCanvas(600, 400, new Vector2D(0, 0), new Vector2D(600, 400));
writableImage = new WritableImage(600, 400);
imageView = new ImageView(writableImage);
root.setCenter(imageView);
}
public void setUpStage(Stage primaryStage) {
primaryStage.setTitle("ChaosGame");
primaryStage.setScene(scene);
primaryStage.setTitle("Chaos Game");
primaryStage.show();
}
public Button getBtnAdd10() {
return btnAdd10;
}
// Getters for other buttons if needed
public Button getBtnAdd100() {
return btnAdd100;
}
public Button getBtnAdd1000() {
return btnAdd1000;
}
public Button getBtnReset() {
return btnReset;
}
private void drawFractal() {
// Clear the ChaosCanvas
chaosCanvas.clear();
List<Transform2D> transforms = List.of(
new AffineTransform2D(new Matrix2x2(0.5, 0, 0, 0.5), new Vector2D(0, 0)),
new AffineTransform2D(new Matrix2x2(0.5, 0, 0, 0.5), new Vector2D(300, 0)),
new AffineTransform2D(new Matrix2x2(0.5, 0, 0, 0.5), new Vector2D(150, 200))
);
ChaosGameDescription description = new ChaosGameDescription(transforms, new Vector2D(0, 0), new Vector2D(600, 400));
ChaosGame chaosGame = new ChaosGame(description, 600, 400);
chaosGame.runSteps(1000);
// Draw the fractal
// This is just a placeholder. Replace this with your fractal drawing code.
chaosCanvas.putPixel(new Vector2D(1, 1));
chaosCanvas.putPixel(new Vector2D(1, 2));
chaosCanvas.putPixel(new Vector2D(100, 200));
chaosCanvas.putPixel(new Vector2D(200, 200));
chaosCanvas.putPixel(new Vector2D(200, 300));
chaosCanvas.putPixel(new Vector2D(200, 400));
chaosCanvas.putPixel(new Vector2D(200, 350));
chaosCanvas.putPixel(new Vector2D(200, 330));
chaosCanvas.putPixel(new Vector2D(200, 320));
chaosCanvas.putPixel(new Vector2D(200, 310));
chaosCanvas.putPixel(new Vector2D(200, 300));
chaosCanvas.putPixel(new Vector2D(200, 290));
chaosCanvas.putPixel(new Vector2D(200, 280));
// Update the WritableImage
updateImage();
}
private void updateImage() {
PixelWriter pixelWriter = writableImage.getPixelWriter();
int[][] canvasArray = chaosCanvas.getCanvasArray();
for (int y = 0; y < chaosCanvas.getHeight(); y++) {
for (int x = 0; x < chaosCanvas.getWidth(); x++) {
int pixel = canvasArray[y][x];
Color color = pixel == 1 ? Color.BLACK : Color.WHITE;
pixelWriter.setColor(x, y, color);
}
}
}
}
.button {
-fx-background-color: #bfbfbf;
-fx-border: none;
-fx-color: white;
-fx-text-decoration: none;
-fx-display: inline-block;
-fx-font-size: 16px;
-fx-margin: 4px 2px;
-fx-cursor: pointer;
-fx-transition-duration: 0.4s; /* Transition effect */
-fx-text-align: center;
-fx-border-radius: 10px;
}
.button:hover {
-fx-background-color: #96fa7d;
-fx-cursor: hand;
}
.combo-box {
-fx-background-color: #bfbfbf;
-fx-border: none;
-fx-color: white;
-fx-font-size: 16px;
-fx-cursor: pointer;
-fx-text-align: center;
-fx-border-radius: 10px;
}
.combo-box .list-cell {
-fx-background-color: white;
-fx-text-fill: black;
}
.combo-box:hover {
-fx-background-color: #96fa7d;
}
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