From d2bea44564b63401b560db2470204a82a5959aa3 Mon Sep 17 00:00:00 2001 From: a23022716 <celia.arezki.1@etu.univ-amu.fr> Date: Fri, 11 Oct 2024 11:03:26 +0200 Subject: [PATCH] add Drawer class that manages the shapes wich extends Canvas (javafx class) --- src/main/java/shape/tp5/Drawer.java | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/main/java/shape/tp5/Drawer.java diff --git a/src/main/java/shape/tp5/Drawer.java b/src/main/java/shape/tp5/Drawer.java new file mode 100644 index 0000000..d46422e --- /dev/null +++ b/src/main/java/shape/tp5/Drawer.java @@ -0,0 +1,37 @@ +package shape.tp5; + +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; +import java.util.ArrayList; +import java.util.List; + +public class Drawer extends Canvas { + private List<Shape> shapes; + + public Drawer(double width, double height) { + super(width, height); + shapes = new ArrayList<>(); + } + + public void add(Shape shape) { + shapes.add(shape); + repaint(); + } + + public void repaint() { + GraphicsContext gc = getGraphicsContext2D(); + gc.clearRect(0, 0, getWidth(), getHeight()); + for (Shape shape : shapes) { + shape.paint(gc); + } + } + + public Shape shapeContaining(double x, double y) { + for (Shape shape : shapes) { + if (shape.contains(x, y)) { + return shape; + } + } + return null; + } +} -- GitLab