From 4b2bef6a574b41f2916127684beae09b1d22865d Mon Sep 17 00:00:00 2001
From: a23022716 <celia.arezki.1@etu.univ-amu.fr>
Date: Fri, 11 Oct 2024 11:35:13 +0200
Subject: [PATCH] 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

---
 src/main/java/shape/tp5/Circle.java    | 11 +++++------
 src/main/java/shape/tp5/Drawer.java    | 24 ++++++++++++++++++++----
 src/main/java/shape/tp5/Rectangle.java |  5 +++--
 src/main/java/shape/tp5/ShapeApp.java  | 14 +++++++++++++-
 4 files changed, 41 insertions(+), 13 deletions(-)

diff --git a/src/main/java/shape/tp5/Circle.java b/src/main/java/shape/tp5/Circle.java
index 22c61f8..f468fd0 100644
--- a/src/main/java/shape/tp5/Circle.java
+++ b/src/main/java/shape/tp5/Circle.java
@@ -1,7 +1,7 @@
 package shape.tp5;
 import javafx.scene.canvas.GraphicsContext;
 
-public class Circle implements Shape {
+class Circle implements Shape {
     private double x, y, radius;
 
     public Circle(double x, double y, double radius) {
@@ -12,14 +12,13 @@ public class Circle implements Shape {
 
     @Override
     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
     public boolean contains(double x, double y) {
-        double dx = x - this.x;
-        double dy = y - this.y;
-        return dx * dx + dy * dy <= radius * radius;
+        return Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2) <= Math.pow(radius, 2);
     }
 
     @Override
@@ -27,4 +26,4 @@ public class Circle implements Shape {
         this.x += dx;
         this.y += dy;
     }
-}
+}
\ No newline at end of file
diff --git a/src/main/java/shape/tp5/Drawer.java b/src/main/java/shape/tp5/Drawer.java
index de1d6ae..9e7a2f0 100644
--- a/src/main/java/shape/tp5/Drawer.java
+++ b/src/main/java/shape/tp5/Drawer.java
@@ -13,6 +13,7 @@ class Drawer extends Canvas {
     private List<Shape> shapes = new ArrayList<>();
     private double startX, startY, endX, endY;
     private boolean drawing = false;
+    private String currentShape = "rectangle"; // Forme par défaut
 
     public Drawer(double width, double height) {
         super(width, height);
@@ -21,6 +22,10 @@ class Drawer extends Canvas {
         setOnMouseReleased(this::handleMouseReleased);
     }
 
+    public void setCurrentShape(String shape) {
+        this.currentShape = shape;
+    }
+
     private void handleMousePressed(MouseEvent event) {
         if (event.getButton() == MouseButton.PRIMARY) {
             startX = event.getX();
@@ -41,8 +46,13 @@ class Drawer extends Canvas {
         if (drawing) {
             endX = event.getX();
             endY = event.getY();
-            shapes.add(new Rectangle(Math.min(startX, endX), Math.min(startY, endY),
-                    Math.abs(endX - startX), Math.abs(endY - startY)));
+            if (currentShape.equals("rectangle")) {
+                shapes.add(new Rectangle(Math.min(startX, endX), Math.min(startY, endY),
+                        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;
             repaint();
         }
@@ -56,8 +66,14 @@ class Drawer extends Canvas {
         }
         if (drawing) {
             gc.setStroke(javafx.scene.paint.Color.BLACK);
-            gc.strokeRect(Math.min(startX, endX), Math.min(startY, endY),
-                    Math.abs(endX - startX), Math.abs(endY - startY));
+            gc.setLineDashes(5);
+            if (currentShape.equals("rectangle")) {
+                gc.strokeRect(Math.min(startX, endX), Math.min(startY, endY),
+                        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);
+            }
         }
     }
 }
diff --git a/src/main/java/shape/tp5/Rectangle.java b/src/main/java/shape/tp5/Rectangle.java
index 0f2ebd4..db77de9 100644
--- a/src/main/java/shape/tp5/Rectangle.java
+++ b/src/main/java/shape/tp5/Rectangle.java
@@ -1,7 +1,7 @@
 package shape.tp5;
 import javafx.scene.canvas.GraphicsContext;
 
-public class Rectangle implements Shape {
+class Rectangle implements Shape {
     private double x, y, width, height;
 
     public Rectangle(double x, double y, double width, double height) {
@@ -14,6 +14,7 @@ public class Rectangle implements Shape {
     @Override
     public void paint(GraphicsContext graphicsContext) {
         graphicsContext.fillRect(x, y, width, height);
+        graphicsContext.strokeRect(x, y, width, height); // Contour
     }
 
     @Override
@@ -26,4 +27,4 @@ public class Rectangle implements Shape {
         this.x += dx;
         this.y += dy;
     }
-}
+}
\ No newline at end of file
diff --git a/src/main/java/shape/tp5/ShapeApp.java b/src/main/java/shape/tp5/ShapeApp.java
index e246162..720e1bd 100644
--- a/src/main/java/shape/tp5/ShapeApp.java
+++ b/src/main/java/shape/tp5/ShapeApp.java
@@ -1,6 +1,7 @@
 package shape.tp5;
 import javafx.application.Application;
 import javafx.scene.Scene;
+import javafx.scene.input.KeyCode;
 import javafx.scene.layout.Pane;
 import javafx.scene.layout.StackPane;
 import javafx.stage.Stage;
@@ -8,14 +9,25 @@ import javafx.stage.Stage;
 public class ShapeApp extends Application {
     public void start(Stage primaryStage) {
         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);
         Scene scene = new Scene(root, 400, 400);
         primaryStage.setTitle("Éditeur de formes");
         primaryStage.setScene(scene);
         primaryStage.show();
+
+        // Assurez-vous que le Canvas peut recevoir les événements de clavier
+        drawer.requestFocus();
     }
 
     public static void main(String[] args) {
         launch(args);
     }
-}
+}
\ No newline at end of file
-- 
GitLab