Skip to content
Snippets Groups Projects
Commit 4b2bef6a authored by AREZKI Celia's avatar AREZKI Celia
Browse files

fix circle rectagle drwer ads shapeApp class to Allow the user to choose the...

fix circle rectagle drwer ads shapeApp class to Allow the user to choose the shape to draw (rectangle or circle) by pressing a key on the keyboard before starting to draw
parent cd7ef6ea
Branches
No related tags found
No related merge requests found
package shape.tp5; package shape.tp5;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
public class Circle implements Shape { class Circle implements Shape {
private double x, y, radius; private double x, y, radius;
public Circle(double x, double y, double radius) { public Circle(double x, double y, double radius) {
...@@ -12,14 +12,13 @@ public class Circle implements Shape { ...@@ -12,14 +12,13 @@ public class Circle implements Shape {
@Override @Override
public void paint(GraphicsContext graphicsContext) { public void paint(GraphicsContext graphicsContext) {
graphicsContext.strokeOval(x - radius, y - radius, radius * 2, radius * 2); graphicsContext.fillOval(x - radius, y - radius, radius * 2, radius * 2);
graphicsContext.strokeOval(x - radius, y - radius, radius * 2, radius * 2); // Contour
} }
@Override @Override
public boolean contains(double x, double y) { public boolean contains(double x, double y) {
double dx = x - this.x; return Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2) <= Math.pow(radius, 2);
double dy = y - this.y;
return dx * dx + dy * dy <= radius * radius;
} }
@Override @Override
......
...@@ -13,6 +13,7 @@ class Drawer extends Canvas { ...@@ -13,6 +13,7 @@ class Drawer extends Canvas {
private List<Shape> shapes = new ArrayList<>(); private List<Shape> shapes = new ArrayList<>();
private double startX, startY, endX, endY; private double startX, startY, endX, endY;
private boolean drawing = false; private boolean drawing = false;
private String currentShape = "rectangle"; // Forme par défaut
public Drawer(double width, double height) { public Drawer(double width, double height) {
super(width, height); super(width, height);
...@@ -21,6 +22,10 @@ class Drawer extends Canvas { ...@@ -21,6 +22,10 @@ class Drawer extends Canvas {
setOnMouseReleased(this::handleMouseReleased); setOnMouseReleased(this::handleMouseReleased);
} }
public void setCurrentShape(String shape) {
this.currentShape = shape;
}
private void handleMousePressed(MouseEvent event) { private void handleMousePressed(MouseEvent event) {
if (event.getButton() == MouseButton.PRIMARY) { if (event.getButton() == MouseButton.PRIMARY) {
startX = event.getX(); startX = event.getX();
...@@ -41,8 +46,13 @@ class Drawer extends Canvas { ...@@ -41,8 +46,13 @@ class Drawer extends Canvas {
if (drawing) { if (drawing) {
endX = event.getX(); endX = event.getX();
endY = event.getY(); endY = event.getY();
if (currentShape.equals("rectangle")) {
shapes.add(new Rectangle(Math.min(startX, endX), Math.min(startY, endY), shapes.add(new Rectangle(Math.min(startX, endX), Math.min(startY, endY),
Math.abs(endX - startX), Math.abs(endY - startY))); Math.abs(endX - startX), Math.abs(endY - startY)));
} else if (currentShape.equals("circle")) {
double radius = Math.hypot(endX - startX, endY - startY);
shapes.add(new Circle(startX, startY, radius));
}
drawing = false; drawing = false;
repaint(); repaint();
} }
...@@ -56,8 +66,14 @@ class Drawer extends Canvas { ...@@ -56,8 +66,14 @@ class Drawer extends Canvas {
} }
if (drawing) { if (drawing) {
gc.setStroke(javafx.scene.paint.Color.BLACK); gc.setStroke(javafx.scene.paint.Color.BLACK);
gc.setLineDashes(5);
if (currentShape.equals("rectangle")) {
gc.strokeRect(Math.min(startX, endX), Math.min(startY, endY), gc.strokeRect(Math.min(startX, endX), Math.min(startY, endY),
Math.abs(endX - startX), Math.abs(endY - startY)); Math.abs(endX - startX), Math.abs(endY - startY));
} else if (currentShape.equals("circle")) {
double radius = Math.hypot(endX - startX, endY - startY);
gc.strokeOval(startX - radius, startY - radius, radius * 2, radius * 2);
}
} }
} }
} }
package shape.tp5; package shape.tp5;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
public class Rectangle implements Shape { class Rectangle implements Shape {
private double x, y, width, height; private double x, y, width, height;
public Rectangle(double x, double y, double width, double height) { public Rectangle(double x, double y, double width, double height) {
...@@ -14,6 +14,7 @@ public class Rectangle implements Shape { ...@@ -14,6 +14,7 @@ public class Rectangle implements Shape {
@Override @Override
public void paint(GraphicsContext graphicsContext) { public void paint(GraphicsContext graphicsContext) {
graphicsContext.fillRect(x, y, width, height); graphicsContext.fillRect(x, y, width, height);
graphicsContext.strokeRect(x, y, width, height); // Contour
} }
@Override @Override
......
package shape.tp5; package shape.tp5;
import javafx.application.Application; import javafx.application.Application;
import javafx.scene.Scene; import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane; import javafx.scene.layout.StackPane;
import javafx.stage.Stage; import javafx.stage.Stage;
...@@ -8,11 +9,22 @@ import javafx.stage.Stage; ...@@ -8,11 +9,22 @@ import javafx.stage.Stage;
public class ShapeApp extends Application { public class ShapeApp extends Application {
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
Drawer drawer = new Drawer(400, 400); Drawer drawer = new Drawer(400, 400);
drawer.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.R) {
drawer.setCurrentShape("rectangle");
} else if (event.getCode() == KeyCode.C) {
drawer.setCurrentShape("circle");
}
});
StackPane root = new StackPane(drawer); StackPane root = new StackPane(drawer);
Scene scene = new Scene(root, 400, 400); Scene scene = new Scene(root, 400, 400);
primaryStage.setTitle("Éditeur de formes"); primaryStage.setTitle("Éditeur de formes");
primaryStage.setScene(scene); primaryStage.setScene(scene);
primaryStage.show(); primaryStage.show();
// Assurez-vous que le Canvas peut recevoir les événements de clavier
drawer.requestFocus();
} }
public static void main(String[] args) { public static void main(String[] args) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment