From ed11c01f99a14b3d1fdfbbacd8c1dc2530cbb134 Mon Sep 17 00:00:00 2001
From: m19023837 <chadi.mansour@etu.univ-amu.fr>
Date: Fri, 4 Oct 2024 11:28:05 +0200
Subject: [PATCH] exercice 1 and a bit of the 2

---
 src/main/java/shape/AbstractShape.java   |  5 +-
 src/main/java/shape/App.java             | 47 +++++++++-------
 src/main/java/shape/BorderDecorator.java | 25 +++------
 src/main/java/shape/CenterDecorator.java | 23 ++++----
 src/main/java/shape/Circle.java          | 24 +++++---
 src/main/java/shape/Decorator.java       | 19 -------
 src/main/java/shape/Drawer.java          | 71 ++++++++++++++++++++++++
 src/main/java/shape/Polygon.java         | 15 +++++
 src/main/java/shape/Rectangle.java       | 37 +++++++-----
 src/main/java/shape/Shape.java           |  6 +-
 src/main/java/shape/ShapeContainer.java  |  2 -
 11 files changed, 176 insertions(+), 98 deletions(-)
 create mode 100644 src/main/java/shape/Drawer.java

diff --git a/src/main/java/shape/AbstractShape.java b/src/main/java/shape/AbstractShape.java
index db97dad..50e5d3f 100644
--- a/src/main/java/shape/AbstractShape.java
+++ b/src/main/java/shape/AbstractShape.java
@@ -14,12 +14,12 @@ public abstract class AbstractShape implements Shape {
     public void addPoints(Point2D... points){
         this.points.addAll(Arrays.asList(points));
     }
-    @Override
+
     public int pointsCount() {
         return points.size();
     }
 
-    @Override
+
     public Point2D point(int index) {
         if( index >= 0 && index < points.size()){
             return points.get(index);
@@ -28,6 +28,5 @@ public abstract class AbstractShape implements Shape {
         }
     }
 
-    @Override
     public abstract void draw(GraphicsContext context) ;
 }
diff --git a/src/main/java/shape/App.java b/src/main/java/shape/App.java
index 0ba88a5..faeb608 100644
--- a/src/main/java/shape/App.java
+++ b/src/main/java/shape/App.java
@@ -13,30 +13,35 @@ import javafx.stage.Stage;
 
 public class App extends Application {
 
-    public static void main(String[] args) {
-        launch(args);
-    }
-
     @Override
     public void start(Stage primaryStage) {
+        Drawer drawer = new Drawer(400, 400);
+
+        Circle circle = new Circle(200, 200, 50);
+        Rectangle rectangle = new Rectangle(100, 100, 150, 100);
+
+        drawer.add(circle);
+        drawer.add(rectangle);
+
+        drawer.repaint();
+
+        drawer.getCanvas().setOnMouseClicked(event -> {
+            double mouseX = event.getX();
+            double mouseY = event.getY();
+            Shape clickedShape = drawer.shapeContaining(mouseX, mouseY);
+            if (clickedShape != null) {
+                System.out.println("Shape clicked: " + clickedShape);
+            }
+        });
         Group root = new Group();
-        Canvas canvas = new Canvas(300, 300);
-        GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
-        ShapeContainer shapeContainer = new ShapeContainer();
-
-        Shape rectangle = new Rectangle(Color.BLUE, new Point2D(50, 50), new Point2D(150, 100));
-        rectangle = new BorderDecorator(rectangle, 10);
-        rectangle = new CenterDecorator(rectangle, 5);
-        shapeContainer.addShape(rectangle);
-
-        Shape polygone = new Polygon(Color.YELLOW, new Point2D(210,100),new Point2D(250,100),new Point2D(150,160), new Point2D(90,100));
-        polygone = new BorderDecorator(polygone, 15);
-        polygone = new CenterDecorator(polygone, 5);
-        shapeContainer.addShape(polygone);
-
-        shapeContainer.draw(graphicsContext);
-        root.getChildren().add(canvas);
-        primaryStage.setScene(new Scene(root));
+        root.getChildren().add(drawer.getCanvas());
+        Scene scene = new Scene(root, 400, 400);
+
+        primaryStage.setTitle("JavaFX Drawer");
+        primaryStage.setScene(scene);
         primaryStage.show();
     }
+    public static void main(String[] args) {
+        launch(args);
+    }
 }
\ No newline at end of file
diff --git a/src/main/java/shape/BorderDecorator.java b/src/main/java/shape/BorderDecorator.java
index bb08656..2240908 100644
--- a/src/main/java/shape/BorderDecorator.java
+++ b/src/main/java/shape/BorderDecorator.java
@@ -6,25 +6,18 @@ import javafx.scene.paint.Color;
 
 public class BorderDecorator extends Decorator {
 
-    private final double radius;
+    @Override
+    public void paint(GraphicsContext graphicsContext) {
 
-    public BorderDecorator(Shape decoratedShape ,double radius) {
-        super(decoratedShape);
-        this.radius = radius;
     }
-    protected void drawDecoration(GraphicsContext context) {
-        context.setStroke(Color.BLACK);
-        context.setLineWidth(2);
 
-        if (decoratedshape.pointsCount() >= 2){
-            Point2D point0 = decoratedshape.point(0);
-            Point2D point1 = decoratedshape.point(1);
+    @Override
+    public boolean contains(double x, double y) {
+        return false;
+    }
+
+    @Override
+    public void translate(double dx, double dy) {
 
-            double x = Math.min(point0.getX(), point1.getX());
-            double y = Math.min(point0.getY(), point1.getY());
-            double width = Math.abs(point1.getX() - point0.getX());
-            double height = Math.abs(point1.getY() - point0.getY());
-            context.strokeRoundRect(x,y, width,height,radius, radius);
-        }
     }
 }
diff --git a/src/main/java/shape/CenterDecorator.java b/src/main/java/shape/CenterDecorator.java
index 8a7c68a..0d9a7e2 100644
--- a/src/main/java/shape/CenterDecorator.java
+++ b/src/main/java/shape/CenterDecorator.java
@@ -5,22 +5,19 @@ import javafx.scene.canvas.GraphicsContext;
 import javafx.scene.paint.Color;
 
 public class CenterDecorator extends Decorator{
-    private final double radius;
-    public CenterDecorator(Shape decoratedShape, double radius) {
-        super(decoratedShape);
-        this.radius = radius;
+
+    @Override
+    public void paint(GraphicsContext graphicsContext) {
+
     }
-        protected void drawDecoration(GraphicsContext context) {
 
-        if (decoratedshape.pointsCount() >= 2) {
-            Point2D point0 = decoratedshape.point(0);
-            Point2D point1 = decoratedshape.point(1);
+    @Override
+    public boolean contains(double x, double y) {
+        return false;
+    }
 
-            double centerX = point0.getX() + point1.getX() / 2;
-            double centerY = point0.getY() + point1.getY() / 2;
+    @Override
+    public void translate(double dx, double dy) {
 
-            context.setFill(Color.RED);
-            context.strokeOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
-        }
     }
 }
diff --git a/src/main/java/shape/Circle.java b/src/main/java/shape/Circle.java
index 81d9175..ed3af2e 100644
--- a/src/main/java/shape/Circle.java
+++ b/src/main/java/shape/Circle.java
@@ -5,21 +5,31 @@ import javafx.scene.canvas.GraphicsContext;
 
 public class Circle implements Shape {
 
-    public Circle(double x, double y, double sqrt) {
+    private double radius, x, y;
+
+    public Circle(double x, double y, double radius) {
+        this.x = x;
+        this.y = y;
+        this.radius = radius;
     }
 
+
     @Override
-    public int pointsCount() {
-        return 0;
+    public void paint(GraphicsContext graphicsContext) {
+        graphicsContext.strokeOval(x-radius, y-radius, 2 * radius, 2 * radius);
     }
 
     @Override
-    public Point2D point(int index) {
-        return null;
+    public boolean contains(double x, double y) {
+        double dx = x -= this.x;
+        double dy = y -= this.y;
+        return dx*dx + dy*dy <= radius*radius;
     }
 
     @Override
-    public void draw(GraphicsContext context) {
-
+    public void translate(double dx, double dy) {
+        this.x += dx;
+        this.y += dy;
     }
+
 }
diff --git a/src/main/java/shape/Decorator.java b/src/main/java/shape/Decorator.java
index 08bed2e..c453446 100644
--- a/src/main/java/shape/Decorator.java
+++ b/src/main/java/shape/Decorator.java
@@ -4,24 +4,5 @@ import javafx.geometry.Point2D;
 import javafx.scene.canvas.GraphicsContext;
 
 public abstract class Decorator implements Shape {
-    protected final Shape decoratedshape;
-    public Decorator(Shape decoratedShape) {
-        this.decoratedshape = decoratedShape;
-    }
-    @Override
-    public int pointsCount() {
-        return decoratedshape.pointsCount();
-    }
 
-    @Override
-    public Point2D point(int index) {
-        return decoratedshape.point(index);
-    }
-
-    @Override
-    public void draw(GraphicsContext context) {
-        decoratedshape.draw(context);
-        drawDecoration(context);
-    }
-    protected abstract void drawDecoration(GraphicsContext context) ;
 }
diff --git a/src/main/java/shape/Drawer.java b/src/main/java/shape/Drawer.java
new file mode 100644
index 0000000..43195b2
--- /dev/null
+++ b/src/main/java/shape/Drawer.java
@@ -0,0 +1,71 @@
+package shape;
+
+import javafx.scene.canvas.Canvas;
+import javafx.scene.canvas.GraphicsContext;
+import javafx.scene.input.MouseButton;
+
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Drawer {
+    private List<Shape> shapes;
+    private double width;
+    private double height;
+    private Canvas canvas;
+    private GraphicsContext gc;
+
+    private Rectangle tempRectangle = null;
+
+    public Drawer(double width, double height) {
+        this.width = width;
+        this.height = height;
+        shapes = new ArrayList<>();
+        canvas = new Canvas(width,height);
+        gc = canvas.getGraphicsContext2D();
+        setupMouseHandlers();
+    }
+
+    public void add(Shape shape) {
+        shapes.add(shape);
+    }
+    public void repaint(){
+        gc.clearRect(0, 0, width, height);
+        for(Shape shape : shapes){
+            shape.paint(gc);
+        }
+        if (tempRectangle != null){
+            tempRectangle.paint(gc);
+        }
+    }
+
+    private void setupMouseHandlers(){
+        canvas.setOnMousePressed(event -> {
+            if(event.getButton() == MouseButton.PRIMARY){
+                double x = event.getX();
+                double y = event.getY();
+                tempRectangle = new Rectangle(x, y, 0, 0);
+            }
+        });
+        canvas.setOnMouseReleased(event -> {
+            if(event.getButton() == MouseButton.PRIMARY){
+                shapes.add(tempRectangle);
+                tempRectangle = null;
+                repaint();
+            }
+        });
+    }
+
+    public Shape shapeContaining(double x, double y){
+        for(Shape shape : shapes){
+            if (shape.contains(x, y)){
+                return shape;
+            }
+        }
+        return null;
+    }
+
+    public Canvas getCanvas() {
+        return canvas;
+    }
+}
diff --git a/src/main/java/shape/Polygon.java b/src/main/java/shape/Polygon.java
index 9140320..c4d1357 100644
--- a/src/main/java/shape/Polygon.java
+++ b/src/main/java/shape/Polygon.java
@@ -26,4 +26,19 @@ public class Polygon extends AbstractShape {
         context.stroke();
 
     }
+
+    @Override
+    public void paint(GraphicsContext graphicsContext) {
+
+    }
+
+    @Override
+    public boolean contains(double x, double y) {
+        return false;
+    }
+
+    @Override
+    public void translate(double dx, double dy) {
+
+    }
 }
diff --git a/src/main/java/shape/Rectangle.java b/src/main/java/shape/Rectangle.java
index efb2f0a..8e81bd8 100644
--- a/src/main/java/shape/Rectangle.java
+++ b/src/main/java/shape/Rectangle.java
@@ -4,26 +4,35 @@ import javafx.geometry.Point2D;
 import javafx.scene.canvas.GraphicsContext;
 import javafx.scene.paint.Color;
 
-public class Rectangle extends AbstractShape{
-    private final Color color;
+public class Rectangle implements Shape{
 
-    public Rectangle(Color color, Point2D point0, Point2D point1){
-        this.color = color;
-        addPoints(point0, point1);
+    private double x;
+    private double y;
+    private double width;
+    private double height;
+
+    public Rectangle(double x, double y, double width, double height) {
+        this.x = x;
+        this.y = y;
+        this.width = width;
+        this.height = height;
     }
 
     @Override
-    public void draw(GraphicsContext context) {
-        context.setStroke(color);
+    public void paint(GraphicsContext graphicsContext) {
+        graphicsContext.strokeRect(x, y, width, height);
 
-        Point2D point0 = points.get(0);
-        Point2D point1 = points.get(1);
+    }
 
-        double x = Math.min(point0.getX(), point1.getX());
-        double y = Math.min(point0.getY(), point1.getY());
-        double width = Math.abs(point1.getX() - point0.getX());
-        double height = Math.abs(point1.getY() - point0.getY());
+    @Override
+    public boolean contains(double x, double y) {
+        return x>= this.x && x<= this.x + this.width
+                && y>= this.y && y<= this.y + this.height;
+    }
 
-        context.strokeRect(x,y,width,height);
+    @Override
+    public void translate(double dx, double dy) {
+        x += dx;
+        y += dy;
     }
 }
diff --git a/src/main/java/shape/Shape.java b/src/main/java/shape/Shape.java
index 16766e4..f376617 100644
--- a/src/main/java/shape/Shape.java
+++ b/src/main/java/shape/Shape.java
@@ -4,7 +4,7 @@ import javafx.geometry.Point2D;
 import javafx.scene.canvas.GraphicsContext;
 
 public interface Shape {
-    int pointsCount();
-    Point2D point(int index);
-    void draw(GraphicsContext context);
+    void paint(GraphicsContext graphicsContext);
+    boolean contains(double x, double y);
+    void translate(double dx, double dy);
 }
diff --git a/src/main/java/shape/ShapeContainer.java b/src/main/java/shape/ShapeContainer.java
index aae70ed..c475da3 100644
--- a/src/main/java/shape/ShapeContainer.java
+++ b/src/main/java/shape/ShapeContainer.java
@@ -14,7 +14,5 @@ public class ShapeContainer{
     }
 
     public void  draw(GraphicsContext context){
-        for(Shape shape : shapes)
-            shape.draw(context);
     }
 }
-- 
GitLab