Skip to content
Snippets Groups Projects
Commit 891d8e82 authored by MANSOUR Chadi's avatar MANSOUR Chadi
Browse files

correcting the code

parent a92a539e
No related branches found
No related tags found
No related merge requests found
...@@ -3,17 +3,16 @@ package shape; ...@@ -3,17 +3,16 @@ package shape;
import javafx.geometry.Point2D; import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
public abstract class AbstractShape implements Shape { public abstract class AbstractShape implements Shape {
private List<Point2D> points;
public AbstractShape(List<Point2D> points) { protected List<Point2D> points = new ArrayList<>();
this.points = points;
}
public void addPoints(Point2D... points){ public void addPoints(Point2D... points){
for (Point2D point: points) this.points.addAll(Arrays.asList(points));
this.points.add(point);
} }
@Override @Override
public int pointsCount() { public int pointsCount() {
...@@ -22,20 +21,13 @@ public abstract class AbstractShape implements Shape { ...@@ -22,20 +21,13 @@ public abstract class AbstractShape implements Shape {
@Override @Override
public Point2D point(int index) { public Point2D point(int index) {
return index < points.size() ? points.get(index) : null; if( index >= 0 && index < points.size()){
return points.get(index);
}else {
throw new IndexOutOfBoundsException("Index out of bounds");
} }
@Override
public void draw(GraphicsContext context) {
if (pointsCount()>0){
context.beginPath();
context.moveTo(point(0).getX(), point(0).getY());
for (int i = 1; i<pointsCount(); i++){
context.lineTo(point(i).getX(),point(i).getY());
}
context.closePath();
context.stroke();
} }
} @Override
public abstract void draw(GraphicsContext context) ;
} }
...@@ -21,12 +21,20 @@ public class App extends Application { ...@@ -21,12 +21,20 @@ public class App extends Application {
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
Group root = new Group(); Group root = new Group();
Canvas canvas = new Canvas(130, 110); Canvas canvas = new Canvas(300, 300);
GraphicsContext graphicsContext = canvas.getGraphicsContext2D(); GraphicsContext graphicsContext = canvas.getGraphicsContext2D();
ShapeContainer shapeContainer = new ShapeContainer(); ShapeContainer shapeContainer = new ShapeContainer();
graphicsContext.setFill(Color.AQUAMARINE);
graphicsContext.fillOval(10,10,10,10); Shape rectangle = new Rectangle(Color.BLUE, new Point2D(50, 50), new Point2D(150, 100));
shapeContainer.addShape(new Rectangle(Color.BLUE,new Point2D(10,10), new Point2D(40,40))); 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); shapeContainer.draw(graphicsContext);
root.getChildren().add(canvas); root.getChildren().add(canvas);
primaryStage.setScene(new Scene(root)); primaryStage.setScene(new Scene(root));
......
...@@ -6,7 +6,7 @@ import javafx.scene.paint.Color; ...@@ -6,7 +6,7 @@ import javafx.scene.paint.Color;
public class BorderDecorator extends Decorator { public class BorderDecorator extends Decorator {
private double radius; private final double radius;
public BorderDecorator(Shape decoratedShape ,double radius) { public BorderDecorator(Shape decoratedShape ,double radius) {
super(decoratedShape); super(decoratedShape);
...@@ -15,9 +15,16 @@ public class BorderDecorator extends Decorator { ...@@ -15,9 +15,16 @@ public class BorderDecorator extends Decorator {
protected void drawDecoration(GraphicsContext context) { protected void drawDecoration(GraphicsContext context) {
context.setStroke(Color.BLACK); context.setStroke(Color.BLACK);
context.setLineWidth(2); context.setLineWidth(2);
for (int i=0; i<pointsCount(); i++){
Point2D point = point(i); if (decoratedshape.pointsCount() >= 2){
context.strokeOval(point.getX() - radius, point.getY()- radius,radius*2,radius*2); Point2D point0 = decoratedshape.point(0);
Point2D point1 = decoratedshape.point(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());
context.strokeRoundRect(x,y, width,height,radius, radius);
} }
} }
} }
...@@ -5,23 +5,22 @@ import javafx.scene.canvas.GraphicsContext; ...@@ -5,23 +5,22 @@ import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
public class CenterDecorator extends Decorator{ public class CenterDecorator extends Decorator{
private double radius; private final double radius;
public CenterDecorator(Shape decoratedShape, double radius) { public CenterDecorator(Shape decoratedShape, double radius) {
super(decoratedShape); super(decoratedShape);
this.radius = radius; this.radius = radius;
} }
protected void drawDecoration(GraphicsContext context) { protected void drawDecoration(GraphicsContext context) {
double centerX = 0;
double centerY = 0; if (decoratedshape.pointsCount() >= 2) {
for (int i =0; i<pointsCount(); i++){ Point2D point0 = decoratedshape.point(0);
Point2D point = point(i); Point2D point1 = decoratedshape.point(1);
centerX += point.getX();
centerY += point.getY(); double centerX = point0.getX() + point1.getX() / 2;
} double centerY = point0.getY() + point1.getY() / 2;
centerX /= pointsCount();
centerY /= pointsCount(); context.setFill(Color.RED);
context.setStroke(Color.RED);
context.setLineWidth(2);
context.strokeOval(centerX - radius, centerY - radius, radius * 2, radius * 2); context.strokeOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
} }
} }
}
...@@ -4,7 +4,7 @@ import javafx.geometry.Point2D; ...@@ -4,7 +4,7 @@ import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
public abstract class Decorator implements Shape { public abstract class Decorator implements Shape {
protected Shape decoratedshape; protected final Shape decoratedshape;
public Decorator(Shape decoratedShape) { public Decorator(Shape decoratedShape) {
this.decoratedshape = decoratedShape; this.decoratedshape = decoratedShape;
} }
...@@ -23,7 +23,5 @@ public abstract class Decorator implements Shape { ...@@ -23,7 +23,5 @@ public abstract class Decorator implements Shape {
decoratedshape.draw(context); decoratedshape.draw(context);
drawDecoration(context); drawDecoration(context);
} }
protected void drawDecoration(GraphicsContext context) { protected abstract void drawDecoration(GraphicsContext context) ;
}
} }
...@@ -8,28 +8,24 @@ import java.util.List; ...@@ -8,28 +8,24 @@ import java.util.List;
public class Polygon extends AbstractShape { public class Polygon extends AbstractShape {
private Color color; private final Color color;
public Polygon(Color color, Point2D... points) { public Polygon(Color color, Point2D... points) {
super(List.of(points));
this.color = color; this.color = color;
addPoints(points);
} }
@Override
public int pointsCount() {
return super.pointsCount();
}
@Override
public Point2D point(int index) {
return super.point(index);
}
@Override @Override
public void draw(GraphicsContext context) { public void draw(GraphicsContext context) {
context.setFill(color); context.setStroke(color);
super.draw(context); context.beginPath();
context.fill(); context.moveTo(points.get(0).getX(), points.get(0).getY());
for (int i = 1; i < pointsCount() ; i++) {
context.lineTo(points.get(i).getX(), points.get(i).getY());
}
context.closePath();
context.stroke();
} }
} }
...@@ -7,27 +7,25 @@ import javafx.scene.paint.Color; ...@@ -7,27 +7,25 @@ import javafx.scene.paint.Color;
import java.util.List; import java.util.List;
public class Rectangle extends AbstractShape{ public class Rectangle extends AbstractShape{
private Color color; private final Color color;
Rectangle(Color color, Point2D point0, Point2D point1){
super(List.of(point0, new Point2D(point1.getX(),point0.getY()), public Rectangle(Color color, Point2D point0, Point2D point1){
point1,new Point2D(point0.getX(),point1.getY())));
this.color = color; this.color = color;
addPoints(point0, point1);
} }
@Override @Override
public int pointsCount() { public void draw(GraphicsContext context) {
return super.pointsCount(); context.setStroke(color);
}
@Override Point2D point0 = points.get(0);
public Point2D point(int index) { Point2D point1 = points.get(1);
return super.point(index);
}
@Override double x = Math.min(point0.getX(), point1.getX());
public void draw(GraphicsContext context) { double y = Math.min(point0.getY(), point1.getY());
context.setFill(color); double width = Math.abs(point1.getX() - point0.getX());
super.draw(context); double height = Math.abs(point1.getY() - point0.getY());
context.fill();
context.strokeRect(x,y,width,height);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment