diff --git a/src/main/java/shape/Circle.java b/src/main/java/shape/Circle.java index ed3af2ee9a9e6f3696614bde8fc407bd1f9b4cee..ab52746b1db28ebb18a9b8e1dbf0399dbd9b20b7 100644 --- a/src/main/java/shape/Circle.java +++ b/src/main/java/shape/Circle.java @@ -2,6 +2,7 @@ package shape; import javafx.geometry.Point2D; import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; public class Circle implements Shape { @@ -16,14 +17,15 @@ public class Circle implements Shape { @Override public void paint(GraphicsContext graphicsContext) { + graphicsContext.setStroke(Color.RED); graphicsContext.strokeOval(x-radius, y-radius, 2 * radius, 2 * radius); } @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; + double dx = x - this.x; + double dy = y - this.y; + return Math.sqrt(dx*dx + dy*dy) <= radius; } @Override @@ -31,5 +33,8 @@ public class Circle implements Shape { this.x += dx; this.y += dy; } + public void updateRadius(double newX, double newY) { + this.radius = Math.sqrt(Math.pow(newX - this.x, 2) + Math.pow(newY - this.y, 2)); + } } diff --git a/src/main/java/shape/Drawer.java b/src/main/java/shape/Drawer.java index 6a33ef2dfde2497b5be607bc620f4b58c0739834..6947a1559eebb83d34f0f2daa6bbd71dbf60a9f0 100644 --- a/src/main/java/shape/Drawer.java +++ b/src/main/java/shape/Drawer.java @@ -2,6 +2,7 @@ package shape; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; +import javafx.scene.input.KeyCode; import javafx.scene.input.MouseButton; @@ -15,7 +16,8 @@ public class Drawer { private Canvas canvas; private GraphicsContext gc; - private Rectangle tempRectangle = null; + private String currentShapeType = "rectangle"; + private Shape tempShape = null; public Drawer(double width, double height) { // this.width = width; @@ -24,6 +26,7 @@ public class Drawer { canvas = new Canvas(width,height); gc = canvas.getGraphicsContext2D(); setupMouseHandlers(); + setupKeyHandlers(); } public void add(Shape shape) { @@ -34,8 +37,8 @@ public class Drawer { for(Shape shape : shapes){ shape.paint(gc); } - if (tempRectangle != null){ - tempRectangle.paint(gc); + if (tempShape != null){ + tempShape.paint(gc); } } @@ -44,36 +47,57 @@ public class Drawer { if(event.getButton() == MouseButton.PRIMARY){ double x = event.getX(); double y = event.getY(); - tempRectangle = new Rectangle(x, y, 0, 0); + // tempShape = new Rectangle(x, y, 0, 0); + if (currentShapeType.equals("rectangle")) { + tempShape = new Rectangle(x, y, 0, 0); + }else if (currentShapeType.equals("circle")) { + tempShape = new Circle(x, y, 0); + } } }); canvas.setOnMouseDragged(event ->{ - if (tempRectangle != null) { + if (tempShape != null) { double x = event.getX(); double y = event.getY(); - tempRectangle.updateSize(x,y); + if (tempShape instanceof Rectangle){ + ((Rectangle) tempShape).updateSize(x,y); + }else if (tempShape instanceof Circle){ + ((Circle) tempShape).updateRadius(x,y); + } repaint(); } }); canvas.setOnMouseReleased(event -> { - if(event.getButton() == MouseButton.PRIMARY){ - shapes.add(tempRectangle); - tempRectangle = null; + if(event.getButton() == MouseButton.PRIMARY && tempShape != null){ + shapes.add(tempShape); + tempShape = null; repaint(); } }); } + private void setupKeyHandlers(){ + canvas.setFocusTraversable(true); + canvas.setOnKeyPressed(event -> { + if (event.getCode() == KeyCode.R){ + currentShapeType = "rectangle"; + System.out.println("Current shape type is: " + currentShapeType); + }else if (event.getCode() == KeyCode.C){ + currentShapeType = "circle"; + System.out.println("Current shape type is: " + currentShapeType); + } + }); + } - public Shape shapeContaining(double x, double y){ + /* 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;