diff --git a/src/main/java/shape/AbstractShape.java b/src/main/java/shape/AbstractShape.java index db97daddd5da35efae50a2178be048ab9c716e3f..50e5d3f09df19b86ce6c26679765f4fa8074ce20 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 0ba88a56c91b19fc643b0488c92db27a9362a639..faeb6084afdc08a67d83695f5f5dd9c79a137c79 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 bb0865612f230286f105742efda3e9c2a4fe51b0..22409083238517c3055f864690a14cecf7a0954a 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 8a7c68a07a6b52549afdd6e8ba75520136c3c5d6..0d9a7e2e1e641b54e77b738edfa1969da69e3b47 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 81d9175dc3977c5c95dec3693569c12ad12d8315..ed3af2ee9a9e6f3696614bde8fc407bd1f9b4cee 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 08bed2e38c7ac74ec230676535c0101f24189c61..c453446859d907efa682e3ae1704686d1f29025a 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 0000000000000000000000000000000000000000..43195b2b1a7407897c5f719c6afb3847f4f9d3d5 --- /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 9140320794131795fca7aed9cfd9cf713c133f65..c4d135724d40cebe94564d4b2915a66eabb412ef 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 efb2f0a8c1921129416949b2c95e29f6d50ab0b3..8e81bd899de692795421d446f7fd86e18ec07580 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 16766e4f3e3128b1582a817d665eb65d32c26047..f376617dda8eb0eefb4333230b65d296cff455c6 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 aae70ed5e542047b60b0f7147f021811017bdbc7..c475da35dc2d0c04aed3995a24943fd6e86f9849 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); } }