diff --git a/graphical-app/pom.xml b/graphical-app/pom.xml index fef568a..284adfd 100644 --- a/graphical-app/pom.xml +++ b/graphical-app/pom.xml @@ -42,6 +42,11 @@ controlsfx 11.2.1 + + net.synedra + validatorfx + 0.5.1 + diff --git a/graphical-app/src/main/java/fr/emiko/graphicalapp/HelloController.java b/graphical-app/src/main/java/fr/emiko/graphicalapp/HelloController.java index 37ff268..f612cd1 100644 --- a/graphical-app/src/main/java/fr/emiko/graphicalapp/HelloController.java +++ b/graphical-app/src/main/java/fr/emiko/graphicalapp/HelloController.java @@ -2,7 +2,10 @@ package fr.emiko.graphicalapp; import javafx.event.ActionEvent; import javafx.event.EventHandler; +import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; +import javafx.scene.Node; +import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.control.Label; import javafx.scene.canvas.GraphicsContext; @@ -15,7 +18,10 @@ import javafx.scene.paint.Color; import fr.emiko.graphicsElement.Stroke; import javafx.scene.robot.Robot; import javafx.scene.transform.Scale; +import javafx.stage.Modality; +import javafx.stage.Stage; +import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import java.util.Vector; @@ -32,14 +38,14 @@ public class HelloController implements Initializable { public Pane pane; private double posX = 0; private double posY = 0; + private double mouseX = 0; + private double mouseY = 0; private Vector strokes = new Vector<>(); - private Vector lastSaved = new Vector<>(); + private Vector> lastSaved = new Vector<>(); private Vector> lines = new Vector<>(); @Override public void initialize(URL url, ResourceBundle resourceBundle) { - drawingCanvas.setOnMouseDragged(this::printLine); - drawingCanvas.setOnMouseClicked(this::resetPos); saveButton.setOnAction(this::onActionSave); loadButton.setOnAction(this::onActionLoad); newCanvasButton.setOnAction(this::onActionCreateCanvas); @@ -49,6 +55,7 @@ public class HelloController implements Initializable { setupCanvas(); scrollPane.prefViewportHeightProperty().bind(pane.layoutYProperty()); scrollPane.prefViewportWidthProperty().bind(pane.layoutXProperty()); + } private void setupCanvas() { @@ -58,6 +65,8 @@ public class HelloController implements Initializable { brushSizeSlider.setValue(1); drawingCanvas.setTranslateX(scrollPane.getWidth()/2); drawingCanvas.setTranslateY(scrollPane.getHeight()/2); + drawingCanvas.setOnMouseDragged(this::printLine); + drawingCanvas.setOnMouseClicked(this::resetPos); scrollPane.addEventFilter(ScrollEvent.ANY, new EventHandler() { @Override public void handle(ScrollEvent event) { @@ -78,7 +87,7 @@ public class HelloController implements Initializable { for (Vector strokeVector : lines) { for (Stroke stroke: strokeVector) { stroke.draw(gc); - System.out.println(stroke); + //System.out.println(stroke); } } } @@ -106,37 +115,75 @@ public class HelloController implements Initializable { newScale.setPivotY(drawingCanvas.getScaleY()); drawingCanvas.getTransforms().add(newScale); - - System.out.println(pane.getHeight()); - System.out.println(pane.getWidth()); pane.setPrefHeight(pane.getHeight()*scaleFactor); pane.setPrefWidth(pane.getWidth()*scaleFactor); } } private void onActionCreateCanvas(ActionEvent actionEvent) { + try { + NewCanvasController controller = showNewStage("New canvas...", "new-canvas-view.fxml"); + + if (controller.isOk()) { + //drawingCanvas = new Canvas(controller.getCanvasWidth(), controller.getCanvasHeight()); + //setupCanvas(); + System.out.println(controller.getCanvasHeight()); + System.out.println(controller.getCanvasWidth()); + drawingCanvas.setWidth(controller.getCanvasWidth()); + drawingCanvas.setHeight(controller.getCanvasHeight()); + drawingCanvas.getGraphicsContext2D().setFill(Color.WHITE); + drawingCanvas.getGraphicsContext2D().fillRect(0, 0, drawingCanvas.getWidth(), drawingCanvas.getHeight()); + drawingCanvas.getGraphicsContext2D().fill(); + pane.setScaleX(1); + pane.setScaleY(1); + System.out.println("New canvas created"); + } + } catch (IOException ignored) { + } + + } + + public T showNewStage(String title, String fxmlFileName) throws IOException { + FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxmlFileName)); + Scene scene = new Scene(fxmlLoader.load()); + Stage stage = new Stage(); + stage.initModality(Modality.APPLICATION_MODAL); + stage.setTitle(title); + stage.setResizable(false); + stage.setScene(scene); + stage.showAndWait(); + return fxmlLoader.getController(); } private void onActionLoad(ActionEvent actionEvent) { // drawingCanvas.getGraphicsContext2D().drawImage(lastSaved, 0, 0); GraphicsContext gc = drawingCanvas.getGraphicsContext2D(); gc.clearRect(0, 0, drawingCanvas.getWidth(), drawingCanvas.getHeight()); - for (Stroke stroke : lastSaved) { - stroke.draw(gc); + gc.setFill(Color.WHITE); + gc.fillRect(0, 0, drawingCanvas.getWidth(), drawingCanvas.getHeight()); + System.out.println(lastSaved.size()); + for (Vector strokeVector : lastSaved) { + for (Stroke stroke: strokeVector) { + stroke.draw(gc); + System.out.println(stroke); + } } strokes = (Vector) lastSaved.clone(); } private void onActionSave(ActionEvent actionEvent) { GraphicsContext gc = drawingCanvas.getGraphicsContext2D(); - lastSaved = (Vector) strokes.clone(); - + lastSaved = (Vector>) lines.clone(); + System.out.println(lastSaved.size()); } private void resetPos(MouseEvent mouseEvent) { posX = 0; posY = 0; + mouseX = 0; + mouseY = 0; lines.add((Vector) strokes.clone()); + System.out.println(lines.size()); System.out.println(lines); strokes.clear(); } diff --git a/graphical-app/src/main/java/fr/emiko/graphicalapp/NewCanvasController.java b/graphical-app/src/main/java/fr/emiko/graphicalapp/NewCanvasController.java new file mode 100644 index 0000000..98f71b5 --- /dev/null +++ b/graphical-app/src/main/java/fr/emiko/graphicalapp/NewCanvasController.java @@ -0,0 +1,70 @@ +package fr.emiko.graphicalapp; + +import javafx.event.ActionEvent; +import javafx.fxml.Initializable; +import javafx.scene.control.Button; +import javafx.scene.control.TextField; +import javafx.stage.Stage; +import net.synedra.validatorfx.Check; +import net.synedra.validatorfx.Validator; + +import java.net.URL; +import java.util.ResourceBundle; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class NewCanvasController implements Initializable { + public TextField heightTextField; + public TextField widthTextField; + public Button createButton; + public Button cancelButton; + private double canvasWidth; + private double canvasHeight; + private boolean ok = false; + private Validator validator = new Validator(); + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + createButton.setOnAction(this::create); + createButton.disableProperty().bind(validator.containsErrorsProperty()); + cancelButton.setOnAction(this::close); + + validator.createCheck() + .decorates(createButton) + .dependsOn("width", heightTextField.textProperty()) + .dependsOn("height", widthTextField.textProperty()) + .withMethod(this::checkWidthHeight) + .immediate(); + } + + private void checkWidthHeight(Check.Context context) { + Pattern pattern = Pattern.compile("\\d+"); + Matcher widthMatcher = pattern.matcher(widthTextField.getText()); + Matcher heightMatcher = pattern.matcher(heightTextField.getText()); + if (!widthMatcher.matches() || !heightMatcher.matches()) { + context.error("Width and height fields must contain only numbers."); + } + } + + public double getCanvasWidth() { + return canvasWidth; + } + + public double getCanvasHeight() { + return canvasHeight; + } + + public boolean isOk() { + return ok; + } + + private void close(ActionEvent actionEvent) { + ((Stage) createButton.getScene().getWindow()).close(); + } + + private void create(ActionEvent actionEvent) { + this.ok = true; + this.canvasWidth = Double.parseDouble(widthTextField.getText()); + this.canvasHeight = Double.parseDouble(heightTextField.getText()); + ((Stage) createButton.getScene().getWindow()).close(); + } +} diff --git a/graphical-app/src/main/java/module-info.java b/graphical-app/src/main/java/module-info.java index 0e41edb..60398df 100644 --- a/graphical-app/src/main/java/module-info.java +++ b/graphical-app/src/main/java/module-info.java @@ -3,6 +3,7 @@ module fr.emiko.graphicalapp { requires javafx.fxml; requires java.desktop; requires org.controlsfx.controls; + requires net.synedra.validatorfx; opens fr.emiko.graphicalapp to javafx.fxml; diff --git a/graphical-app/src/main/resources/fr/emiko/graphicalapp/new-canvas-view.fxml b/graphical-app/src/main/resources/fr/emiko/graphicalapp/new-canvas-view.fxml new file mode 100644 index 0000000..140e8d2 --- /dev/null +++ b/graphical-app/src/main/resources/fr/emiko/graphicalapp/new-canvas-view.fxml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +