mirror of
https://github.com/Akomry/makeyourownapp-jam.git
synced 2025-12-06 03:33:54 +00:00
feat(newCanvas): possibilité de créer un nouveau canvas de taille spécifiée
This commit is contained in:
parent
3f5bb98f8d
commit
041f721ed5
5 changed files with 190 additions and 11 deletions
|
|
@ -42,6 +42,11 @@
|
|||
<artifactId>controlsfx</artifactId>
|
||||
<version>11.2.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.synedra</groupId>
|
||||
<artifactId>validatorfx</artifactId>
|
||||
<version>0.5.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -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<Stroke> strokes = new Vector<>();
|
||||
private Vector<Stroke> lastSaved = new Vector<>();
|
||||
private Vector<Vector<Stroke>> lastSaved = new Vector<>();
|
||||
private Vector<Vector<Stroke>> 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<ScrollEvent>() {
|
||||
@Override
|
||||
public void handle(ScrollEvent event) {
|
||||
|
|
@ -78,7 +87,7 @@ public class HelloController implements Initializable {
|
|||
for (Vector<Stroke> 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> 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<Stroke> strokeVector : lastSaved) {
|
||||
for (Stroke stroke: strokeVector) {
|
||||
stroke.draw(gc);
|
||||
System.out.println(stroke);
|
||||
}
|
||||
}
|
||||
strokes = (Vector<Stroke>) lastSaved.clone();
|
||||
}
|
||||
|
||||
private void onActionSave(ActionEvent actionEvent) {
|
||||
GraphicsContext gc = drawingCanvas.getGraphicsContext2D();
|
||||
lastSaved = (Vector<Stroke>) strokes.clone();
|
||||
|
||||
lastSaved = (Vector<Vector<Stroke>>) lines.clone();
|
||||
System.out.println(lastSaved.size());
|
||||
}
|
||||
|
||||
private void resetPos(MouseEvent mouseEvent) {
|
||||
posX = 0;
|
||||
posY = 0;
|
||||
mouseX = 0;
|
||||
mouseY = 0;
|
||||
lines.add((Vector<Stroke>) strokes.clone());
|
||||
System.out.println(lines.size());
|
||||
System.out.println(lines);
|
||||
strokes.clear();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,56 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.TextField?>
|
||||
<?import javafx.scene.layout.ColumnConstraints?>
|
||||
<?import javafx.scene.layout.GridPane?>
|
||||
<?import javafx.scene.layout.HBox?>
|
||||
<?import javafx.scene.layout.RowConstraints?>
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
|
||||
<VBox xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fr.emiko.graphicalapp.NewCanvasController">
|
||||
<children>
|
||||
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity">
|
||||
<columnConstraints>
|
||||
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
|
||||
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="200.0" />
|
||||
</columnConstraints>
|
||||
<rowConstraints>
|
||||
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
|
||||
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
|
||||
</rowConstraints>
|
||||
<children>
|
||||
<Label text="Height :" GridPane.hgrow="ALWAYS" />
|
||||
<Label text="Width :" GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" />
|
||||
<TextField fx:id="heightTextField" promptText="300" GridPane.columnIndex="1">
|
||||
<GridPane.margin>
|
||||
<Insets left="10.0" />
|
||||
</GridPane.margin>
|
||||
</TextField>
|
||||
<TextField fx:id="widthTextField" promptText="300" GridPane.columnIndex="1" GridPane.rowIndex="1">
|
||||
<GridPane.margin>
|
||||
<Insets left="10.0" />
|
||||
</GridPane.margin>
|
||||
</TextField>
|
||||
</children>
|
||||
<padding>
|
||||
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
|
||||
</padding>
|
||||
</GridPane>
|
||||
<HBox alignment="TOP_CENTER" spacing="10.0" VBox.vgrow="ALWAYS">
|
||||
<children>
|
||||
<Button fx:id="createButton" mnemonicParsing="false" text="Create">
|
||||
<HBox.margin>
|
||||
<Insets />
|
||||
</HBox.margin>
|
||||
</Button>
|
||||
<Button fx:id="cancelButton" mnemonicParsing="false" text="Cancel" />
|
||||
</children>
|
||||
<padding>
|
||||
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
|
||||
</padding>
|
||||
</HBox>
|
||||
</children>
|
||||
</VBox>
|
||||
Loading…
Add table
Add a link
Reference in a new issue