Add SCircle

This commit is contained in:
Thibaud Gasser 2019-03-19 21:54:57 +01:00
parent 5be59b37f0
commit 98b05e435e
5 changed files with 82 additions and 9 deletions

View File

@ -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)
)
);
}

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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)){

View File

@ -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("<div id=\"rec" + rect.hashCode() + "\"></div>");
@ -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("<div class=\"circle"+circle.hashCode()+"\"></div>");
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);