Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
1 result

Target

Select target project
No results found
Select Git revision
  • master
1 result
Show changes

Commits on Source 11

9 files
+ 175
16
Compare changes
  • Side-by-side
  • Inline

Files

+27 −0
Original line number Diff line number Diff line
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractShape implements Shape {
    protected List<Point2D> points = new ArrayList<>();

    public void addPoints(Point2D... points) {
        for (Point2D point : points) {
            this.points.add(point);
        }
    }

    @Override
    public int pointsCount() {
        return points.size();
    }

    @Override
    public Point2D point(int index) {
        return points.get(index);
    }

    @Override
    public abstract void draw(GraphicsContext graphics);
}
Original line number Diff line number Diff line
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class BorderDecorator extends Decorator {
    private double radius;

    public BorderDecorator(Shape decoratedShape, double radius) {
        super(decoratedShape);
        this.radius = radius;
    }

    @Override
    protected void drawDecoration(GraphicsContext graphicsContext) {
        graphicsContext.setStroke(Color.BLACK);
        for (int i = 0; i < decoratedShape.pointsCount(); i++) {
            Point2D point = decoratedShape.point(i);
            graphicsContext.strokeOval(point.getX() - radius, point.getY() - radius, radius * 2, radius * 2);
        }
    }
}
Original line number Diff line number Diff line
package shape;
import javafx.scene.canvas.GraphicsContext;
import javafx.geometry.Point2D;
import javafx.scene.paint.Color;

public class CenterDecorator extends Decorator {
    private double radius;

    public CenterDecorator(Shape decoratedShape, double radius) {
        super(decoratedShape);
        this.radius = radius;
    }

    @Override
    protected void drawDecoration(GraphicsContext graphicsContext) {
        graphicsContext.setStroke(Color.RED);
        double centerX = 0, centerY = 0;
        for (int i = 0; i < decoratedShape.pointsCount(); i++) {
            Point2D point = decoratedShape.point(i);
            centerX += point.getX();
            centerY += point.getY();
        }
        centerX /= decoratedShape.pointsCount();
        centerY /= decoratedShape.pointsCount();

        graphicsContext.strokeOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
    }
}
Original line number Diff line number Diff line
@@ -2,24 +2,36 @@ package shape;

import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class Circle implements Shape {
    private Point2D center;
    private double radius;
    private Color color;

    public Circle(double x, double y, double sqrt) {
    public Circle(Color color, double x, double y, double radius) {
        this.color = color;
        this.center = new Point2D(x, y);
        this.radius = radius;
    }

    @Override
    public int pointsCount() {
        return 0;
        return 1;
    }

    @Override
    public Point2D point(int index) {
        return null;
        if (index == 0) {
            return center;
        }
        throw new IndexOutOfBoundsException("Circle has only one center point.");
    }

    @Override
    public void draw(GraphicsContext context) {

        context.setStroke(color);
        context.strokeOval(center.getX() - radius, center.getY() - radius,
                radius * 2, radius * 2);
    }
}
+29 −0
Original line number Diff line number Diff line
package shape;
import javafx.scene.canvas.GraphicsContext;
import javafx.geometry.Point2D;

public abstract class Decorator implements Shape {
    protected 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 graphicsContext) {
        decoratedShape.draw(graphicsContext);
        drawDecoration(graphicsContext);
    }

    protected abstract void drawDecoration(GraphicsContext graphicsContext);
}
+25 −0
Original line number Diff line number Diff line
package shape;

import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class Polygon extends AbstractShape {
    private Color color;

    public Polygon(Color color, Point2D... points) {
        this.color = color;
        addPoints(points);
    }

    @Override
    public void draw(GraphicsContext graphicsContext) {
        graphicsContext.setStroke(color);
        for (int i = 0; i < points.size(); i++) {
            Point2D p1 = points.get(i);
            Point2D p2 = points.get((i + 1) % points.size());
            graphicsContext.strokeLine(p1.getX(), p1.getY(), p2.getX(), p2.getY());
        }
    }
}
Original line number Diff line number Diff line
@@ -5,23 +5,35 @@ import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class Rectangle implements Shape {
    Color color;
    Rectangle(Color color, Point2D point0, Point2D point1){
    private Color color;
    private Point2D point0, point1;

    public Rectangle(Color color, Point2D point0, Point2D point1) {
        this.color = color;
        this.point0 = point0;
        this.point1 = point1;
    }

    @Override
    public int pointsCount() {
        return 0;
        return 2;
    }

    @Override
    public Point2D point(int index) {
        return null;
        if (index == 0) {
            return point0;
        } else if (index == 1) {
            return point1;
        }
        throw new IndexOutOfBoundsException("Rectangle has only 2 points.");
    }

    @Override
    public void draw(GraphicsContext context) {

        context.setStroke(color);
        context.strokeRect(point0.getX(), point0.getY(),
                point1.getX() - point0.getX(),
                point1.getY() - point0.getY());
    }
}
Original line number Diff line number Diff line
@@ -3,8 +3,10 @@ package shape;
import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;


public interface Shape {
    int pointsCount();
    Point2D point(int index);
    void draw(GraphicsContext context);
    void draw(GraphicsContext graphicsContext);
}
Original line number Diff line number Diff line
package shape;

import javafx.scene.canvas.GraphicsContext;

import java.util.ArrayList;
import java.util.List;

@@ -9,10 +8,13 @@ public class ShapeContainer{

    private List<Shape> shapes = new ArrayList<>();

    public void addShape(Shape shape){}
    public void addShape(Shape shape) {
        shapes.add(shape);
    }

    public void draw(GraphicsContext context) {
        for(Shape shape : shapes)
        for (Shape shape : shapes) {
            shape.draw(context);
        }
    }
}