From 98b05e435eadc5195027af0cb12bc19338683ade Mon Sep 17 00:00:00 2001 From: Thibaud Date: Tue, 19 Mar 2019 21:54:57 +0100 Subject: [PATCH] Add SCircle --- src/main/java/ovh/gasser/newshapes/App.java | 4 ++- .../ovh/gasser/newshapes/ShapeVisitor.java | 3 ++ .../ovh/gasser/newshapes/shapes/SCircle.java | 31 +++++++++++++++++ .../gasser/newshapes/ui/ShapeDraftman.java | 20 ++++++++++- .../newshapes/ui/html/HTMLDraftman.java | 33 +++++++++++++++---- 5 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 src/main/java/ovh/gasser/newshapes/shapes/SCircle.java diff --git a/src/main/java/ovh/gasser/newshapes/App.java b/src/main/java/ovh/gasser/newshapes/App.java index a0244a1..2f0ba10 100644 --- a/src/main/java/ovh/gasser/newshapes/App.java +++ b/src/main/java/ovh/gasser/newshapes/App.java @@ -1,5 +1,6 @@ package ovh.gasser.newshapes; +import ovh.gasser.newshapes.shapes.SCircle; import ovh.gasser.newshapes.shapes.SCollection; import ovh.gasser.newshapes.shapes.SRectangle; import ovh.gasser.newshapes.shapes.Shape; @@ -31,7 +32,8 @@ public class App { SRectangle.create(70, 10, 40, 60), SCollection.of( SRectangle.create(100, 200, 40, 60, Color.MAGENTA), - SRectangle.create(150, 200, 40, 60, Color.MAGENTA) + SRectangle.create(150, 200, 40, 60, Color.MAGENTA), + SCircle.create(200, 200, 60) ) ); } diff --git a/src/main/java/ovh/gasser/newshapes/ShapeVisitor.java b/src/main/java/ovh/gasser/newshapes/ShapeVisitor.java index c740bab..c5e3d5d 100644 --- a/src/main/java/ovh/gasser/newshapes/ShapeVisitor.java +++ b/src/main/java/ovh/gasser/newshapes/ShapeVisitor.java @@ -1,5 +1,6 @@ package ovh.gasser.newshapes; +import ovh.gasser.newshapes.shapes.SCircle; import ovh.gasser.newshapes.shapes.SCollection; import ovh.gasser.newshapes.shapes.SRectangle; @@ -7,4 +8,6 @@ public interface ShapeVisitor { void visitRectangle(SRectangle sRectangle); void visitCollection(SCollection collection); + + void visitCircle(SCircle sCircle); } diff --git a/src/main/java/ovh/gasser/newshapes/shapes/SCircle.java b/src/main/java/ovh/gasser/newshapes/shapes/SCircle.java new file mode 100644 index 0000000..c910c34 --- /dev/null +++ b/src/main/java/ovh/gasser/newshapes/shapes/SCircle.java @@ -0,0 +1,31 @@ +package ovh.gasser.newshapes.shapes; + +import ovh.gasser.newshapes.ShapeVisitor; +import ovh.gasser.newshapes.attributes.SelectionAttributes; + +import java.awt.*; + +public class SCircle extends AbstractShape { + + private int radius; + + private SCircle(int x, int y, int radius) { + super(new Rectangle(x, y, radius, radius)); + this.radius = radius; + } + + @Override + public void accept(ShapeVisitor visitor) { + visitor.visitCircle(this); + } + + public int getRadius() { + return radius; + } + + public static SCircle create(int x, int y, int radius) { + var circle = new SCircle(x, y, radius); + circle.addAttributes(new SelectionAttributes()); + return circle; + } +} diff --git a/src/main/java/ovh/gasser/newshapes/ui/ShapeDraftman.java b/src/main/java/ovh/gasser/newshapes/ui/ShapeDraftman.java index 67427df..4460702 100644 --- a/src/main/java/ovh/gasser/newshapes/ui/ShapeDraftman.java +++ b/src/main/java/ovh/gasser/newshapes/ui/ShapeDraftman.java @@ -1,11 +1,12 @@ package ovh.gasser.newshapes.ui; import ovh.gasser.newshapes.ShapeVisitor; -import ovh.gasser.newshapes.shapes.Shape; import ovh.gasser.newshapes.attributes.ColorAttributes; import ovh.gasser.newshapes.attributes.SelectionAttributes; +import ovh.gasser.newshapes.shapes.SCircle; import ovh.gasser.newshapes.shapes.SCollection; import ovh.gasser.newshapes.shapes.SRectangle; +import ovh.gasser.newshapes.shapes.Shape; import java.awt.*; @@ -46,6 +47,23 @@ public class ShapeDraftman implements ShapeVisitor { drawHandlerIfSelected(collection); } + @Override + public void visitCircle(SCircle circle) { + ColorAttributes colAttrs = (ColorAttributes) circle.getAttributes(ColorAttributes.ID); + final Rectangle bounds = circle.getBounds(); + if (colAttrs == null) { + colAttrs = DEFAULT_COLOR_ATTRIBUTES; + } + if (colAttrs.filled) { + this.g2d.setColor(colAttrs.filledColor); + this.g2d.fillOval(bounds.x, bounds.y, circle.getRadius(), circle.getRadius()); + } + if (colAttrs.stroked) this.g2d.setColor(colAttrs.strokedColor); + this.g2d.drawOval(bounds.x, bounds.y, circle.getRadius(), circle.getRadius()); + + drawHandlerIfSelected(circle); + } + private void drawHandlerIfSelected(Shape s) { SelectionAttributes selAttrs = (SelectionAttributes) s.getAttributes(SelectionAttributes.ID); if ((selAttrs != null) && (selAttrs.selected)){ diff --git a/src/main/java/ovh/gasser/newshapes/ui/html/HTMLDraftman.java b/src/main/java/ovh/gasser/newshapes/ui/html/HTMLDraftman.java index d55a073..b696f2e 100644 --- a/src/main/java/ovh/gasser/newshapes/ui/html/HTMLDraftman.java +++ b/src/main/java/ovh/gasser/newshapes/ui/html/HTMLDraftman.java @@ -2,8 +2,10 @@ package ovh.gasser.newshapes.ui.html; import ovh.gasser.newshapes.ShapeVisitor; import ovh.gasser.newshapes.attributes.ColorAttributes; +import ovh.gasser.newshapes.shapes.SCircle; import ovh.gasser.newshapes.shapes.SCollection; import ovh.gasser.newshapes.shapes.SRectangle; +import ovh.gasser.newshapes.shapes.Shape; import java.awt.*; import java.io.PrintWriter; @@ -29,6 +31,11 @@ public class HTMLDraftman implements ShapeVisitor { this.cssOutput = cssOutput; } + @Override + public void visitCollection(SCollection collection) { + collection.stream().forEach(shape -> shape.accept(this)); + } + @Override public void visitRectangle(SRectangle rect) { htmlOutput.println("
"); @@ -41,8 +48,25 @@ public class HTMLDraftman implements ShapeVisitor { cssOutput.println(this.attributesToCss(rect) + " }"); } - private String attributesToCss(SRectangle rect) { - ColorAttributes attrs = (ColorAttributes) rect.getAttributes(ColorAttributes.ID); + @Override + public void visitCircle(SCircle circle) { + htmlOutput.println("
"); + cssOutput.println(".circle" + circle.hashCode() + "{ "); + cssOutput.println("position: absolute;"); + cssOutput.println("top:" + circle.getBounds().y + "px;"); + cssOutput.println("left:" + circle.getBounds().x + "px;"); + cssOutput.println("width:" + circle.getRadius() + "px;"); + cssOutput.println("height:" + circle.getRadius() + "px;"); + cssOutput.println("border-radius:" + circle.getRadius() / 2 + "px;"); + cssOutput.println("-webkit-border-radius:" + circle.getRadius() / 2 + "px;"); + cssOutput.println("-o-border-radius:" + circle.getRadius() / 2 + "px;"); + cssOutput.println("-moz-border-radius:" + circle.getRadius() / 2 + "px;"); + cssOutput.println("position: absolute;"); + cssOutput.println(this.attributesToCss(circle) + " }"); + } + + private String attributesToCss(Shape shape) { + ColorAttributes attrs = (ColorAttributes) shape.getAttributes(ColorAttributes.ID); String strokedColor = "#ffffff"; String filledColor = "#ffffff"; @@ -72,11 +96,6 @@ public class HTMLDraftman implements ShapeVisitor { return String.format("#%02x%02x%02x", r, g, b); } - @Override - public void visitCollection(SCollection collection) { - collection.stream().forEach(shape -> shape.accept(this)); - } - public void generateHTML(SCollection model) { htmlOutput.println(HEADER_TEMPLATE); visitCollection(model);