diff --git a/src/main/java/shape/tp5/Circle.java b/src/main/java/shape/tp5/Circle.java index 22c61f87c163643314339e5621d0e3af12eb18c3..f468fd016eb510cd8ecc5df680b569864d09078e 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 de1d6ae85840540e01b3947d9c01dfa470c39d22..9e7a2f019acac9b2234fe3aa245cad4d0b1e52eb 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 0f2ebd406ea51cc80881c304b7f34c161a629ece..db77de9247746b0379bed0ef9a0d29e63ee21c86 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 e246162cd13857cd864ce0b5dc9a5f7d2647bba3..720e1bdd007adc3046a423955ba64f5afc1f1f54 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