From e3bf034bda2330745a60125118d298339cf4db10 Mon Sep 17 00:00:00 2001 From: aspiringLich Date: Sun, 5 Feb 2023 19:12:12 -0500 Subject: [PATCH] Restructure of Drawable & Outlines! --- build.gradle | 2 +- .../animation/OpacityAnimation.java | 6 ++ .../java/paintingcanvas/drawable/Circle.java | 11 +-- .../paintingcanvas/drawable/Drawable.java | 69 +++++++++++++++++-- .../java/paintingcanvas/drawable/Ellipse.java | 11 +-- .../java/paintingcanvas/drawable/Line.java | 9 ++- .../java/paintingcanvas/drawable/Path.java | 29 +++----- .../java/paintingcanvas/drawable/Polygon.java | 12 ++-- .../paintingcanvas/drawable/Rectangle.java | 11 +-- .../java/paintingcanvas/drawable/Square.java | 11 +-- .../java/paintingcanvas/drawable/Text.java | 10 ++- .../paintingcanvas/drawable/Triangle.java | 52 ++++++++------ src/test/java/examples/Test.java | 9 ++- 13 files changed, 167 insertions(+), 75 deletions(-) diff --git a/build.gradle b/build.gradle index b9350d5..de4f086 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { // what should the group-id be breon group 'paintingcanvas' -version '1.2.2' +version '1.2.3' repositories {} dependencies {} diff --git a/src/main/java/paintingcanvas/animation/OpacityAnimation.java b/src/main/java/paintingcanvas/animation/OpacityAnimation.java index c9752b6..1729c93 100644 --- a/src/main/java/paintingcanvas/animation/OpacityAnimation.java +++ b/src/main/java/paintingcanvas/animation/OpacityAnimation.java @@ -9,6 +9,7 @@ */ public class OpacityAnimation extends Animation { public int start; + public int outlineStart; public int end; public OpacityAnimation(int start, int duration, double end, Drawable drawable) { @@ -21,10 +22,15 @@ void updateAnimation(Drawable drawable, double progress) { Color color = drawable.color; int alpha = (int) (start + (end - start) * easing.ease(progress)); drawable.color = new Color(color.getRed(), color.getGreen(), color.getBlue(), alpha); + + Color outlineColor = drawable.outlineColor; + int outlineAlpha = (int) (start + (end - start) * easing.ease(progress)); + drawable.outlineColor = new Color(outlineColor.getRed(), outlineColor.getGreen(), outlineColor.getBlue(), outlineAlpha); } @Override void initAnimation(Drawable drawable) { this.start = drawable.color.getAlpha(); + this.outlineStart = drawable.outlineColor.getAlpha(); } } diff --git a/src/main/java/paintingcanvas/drawable/Circle.java b/src/main/java/paintingcanvas/drawable/Circle.java index d24b3c8..60cb945 100644 --- a/src/main/java/paintingcanvas/drawable/Circle.java +++ b/src/main/java/paintingcanvas/drawable/Circle.java @@ -49,10 +49,13 @@ public Circle(int centerX, int centerY, int radius, Color color) { } @Override - public void draw(Graphics2D gc) { - gc.setColor(color); - if (this.filled) gc.fillOval(x - radius, y - radius, radius * 2, radius * 2); - else gc.drawOval(x - radius, y - radius, radius, radius); + protected void drawFilled(Graphics2D gc) { + gc.fillOval(x - radius, y - radius, radius * 2, radius * 2); + } + + @Override + protected void drawOutline(Graphics2D gc) { + gc.drawOval(x - radius, y - radius, radius * 2, radius * 2); } @Override diff --git a/src/main/java/paintingcanvas/drawable/Drawable.java b/src/main/java/paintingcanvas/drawable/Drawable.java index 44c50f2..2bd64ab 100644 --- a/src/main/java/paintingcanvas/drawable/Drawable.java +++ b/src/main/java/paintingcanvas/drawable/Drawable.java @@ -35,6 +35,50 @@ public abstract class Drawable> implements Animatable { * The Y-position of the object */ public int y; + /** + * The color of the outline + */ + public Color outlineColor = Color.BLACK; + /** + * The stroke of the outline + */ + public Stroke outlineStroke; + /** + * The stroke used to draw the outline when the object is not filled + */ + public Stroke unfilledStroke = new BasicStroke(5); + + /** + * Sets the parameters for the outline of the shape + * @param color the color of the outline + * @param thickness the thickness of the outline + * @return The original object to allow method chaining + */ + public T setOutline(Color color, int thickness) { + this.outlineColor = color; + this.outlineStroke = new BasicStroke(thickness * 2); + return this.getThis(); + } + + /** + * Sets the parameters for the outline of the shape, with the color defaulting to black + * @param thickness the thickness of the outline + * @return The original object to allow method chaining + */ + public T setOutline(int thickness) { + this.outlineColor = color; + this.outlineStroke = new BasicStroke(thickness * 2); + return this.getThis(); + } + + /** + * Removes the outline from the shape + * @return The original object to allow method chaining + */ + public T removeOutline() { + this.outlineStroke = null; + return this.getThis(); + } Drawable(int x, int y, Color color) { this.x = x; @@ -43,13 +87,6 @@ public abstract class Drawable> implements Animatable { App.addElement(this); } - /** - * Draw the actual object onto the screen - * - * @param gc The graphics context - */ - public abstract void draw(Graphics2D gc); - /** * Get the object's centerpoint * @@ -86,8 +123,26 @@ public void render(Graphics g) { this.draw(gc); } + protected abstract void drawFilled(Graphics2D gc); + + protected abstract void drawOutline(Graphics2D gc); + protected abstract T getThis(); + public void draw(Graphics2D gc) { + if (this.outlineStroke != null) { + gc.setColor(this.outlineColor); + gc.setStroke(this.outlineStroke); + this.drawOutline(gc); + } + gc.setColor(this.color); + if (this.filled) this.drawFilled(gc); + else { + gc.setStroke(this.unfilledStroke); + this.drawOutline(gc); + } + } + /** * Hide the Object. *
{@code
diff --git a/src/main/java/paintingcanvas/drawable/Ellipse.java b/src/main/java/paintingcanvas/drawable/Ellipse.java
index 511b7f4..85b1bdf 100644
--- a/src/main/java/paintingcanvas/drawable/Ellipse.java
+++ b/src/main/java/paintingcanvas/drawable/Ellipse.java
@@ -57,10 +57,13 @@ public Ellipse(int centerX, int centerY, int width, int height, Color color) {
     }
 
     @Override
-    public void draw(Graphics2D gc) {
-        gc.setColor(color);
-        if (this.filled) gc.fillOval(x - width / 2, y - height / 2, width, height);
-        else gc.drawOval(x - width / 2, y - height / 2, width, height);
+    protected void drawOutline(Graphics2D gc) {
+        gc.drawOval(x - width / 2, y - height / 2, width, height);
+    }
+
+    @Override
+    protected void drawFilled(Graphics2D gc) {
+        gc.fillOval(x - width / 2, y - height / 2, width, height);
     }
 
     @Override
diff --git a/src/main/java/paintingcanvas/drawable/Line.java b/src/main/java/paintingcanvas/drawable/Line.java
index 8ace8ba..45e94c7 100644
--- a/src/main/java/paintingcanvas/drawable/Line.java
+++ b/src/main/java/paintingcanvas/drawable/Line.java
@@ -137,8 +137,13 @@ public Line setEndpoint(int x, int y) {
     }
 
     @Override
-    public void draw(Graphics2D gc) {
-        gc.setColor(color);
+    protected void drawFilled(Graphics2D gc) {
+        gc.setStroke(stroke);
+        gc.drawLine(this.x , this.y, this.x + endOffset.x, this.y + endOffset.y);
+    }
+
+    @Override
+    protected void drawOutline(Graphics2D gc) {
         gc.setStroke(stroke);
         gc.drawLine(this.x , this.y, this.x + endOffset.x, this.y + endOffset.y);
     }
diff --git a/src/main/java/paintingcanvas/drawable/Path.java b/src/main/java/paintingcanvas/drawable/Path.java
index 0405d3e..0be6ab3 100644
--- a/src/main/java/paintingcanvas/drawable/Path.java
+++ b/src/main/java/paintingcanvas/drawable/Path.java
@@ -15,11 +15,6 @@ public class Path extends Drawable {
      * @see Path2D
      */
     Path2D path;
-    /**
-     * The stroke of the line
-     * @see Stroke
-     */
-    Stroke stroke;
 
     /**
      * Create a new Path element. The path is initially empty.
@@ -36,7 +31,6 @@ public class Path extends Drawable {
     public Path() {
         super(0, 0, Color.BLACK);
         this.path = new Path2D.Double();
-        this.stroke = new BasicStroke(1);
         this.filled = false;
     }
 
@@ -55,8 +49,7 @@ public Path() {
     public Path(Color color) {
         super(0, 0, color);
         this.path = new Path2D.Double();
-        this.stroke = new BasicStroke(1);
-        this.filled = false;
+        this.unfilledStroke = new BasicStroke(1);
     }
 
     /**
@@ -67,7 +60,7 @@ public Path(Color color) {
      */
     @SuppressWarnings("unused")
     public Path setStroke(Stroke stroke) {
-        this.stroke = stroke;
+        this.unfilledStroke = stroke;
         return this;
     }
 
@@ -78,7 +71,7 @@ public Path setStroke(Stroke stroke) {
      * @return The original object to allow method chaining
      */
     public Path setThickness(int thickness) {
-        this.stroke = new BasicStroke(thickness);
+        this.unfilledStroke = new BasicStroke(thickness);
         return this;
     }
 
@@ -141,17 +134,17 @@ public Path curveTo(int x1, int y1, int x2, int y2, int x3, int y3) {
     }
 
     @Override
-    public void draw(Graphics2D gc) {
-        gc.setColor(color);
-        gc.setStroke(stroke);
+    protected void drawOutline(Graphics2D gc) {
         Path2D copy = (Path2D)path.clone();
         copy.transform(AffineTransform.getTranslateInstance(x, y));
+        gc.draw(copy);
+    }
 
-        if (this.filled) gc.fill(copy);
-        else {
-            gc.setStroke(stroke);
-            gc.draw(copy);
-        }
+    @Override
+    protected void drawFilled(Graphics2D gc) {
+        Path2D copy = (Path2D)path.clone();
+        copy.transform(AffineTransform.getTranslateInstance(x, y));
+        gc.fill(copy);
     }
 
     @Override
diff --git a/src/main/java/paintingcanvas/drawable/Polygon.java b/src/main/java/paintingcanvas/drawable/Polygon.java
index 6ee4129..13cb780 100644
--- a/src/main/java/paintingcanvas/drawable/Polygon.java
+++ b/src/main/java/paintingcanvas/drawable/Polygon.java
@@ -261,14 +261,18 @@ public Polygon(int[][] points, Color color) {
     }
 
     @Override
-    public void draw(Graphics2D gc) {
-        gc.setColor(color);
+    protected void drawOutline(Graphics2D gc) {
         polygon.translate(x, y);
-        if (this.filled) gc.fillPolygon(polygon);
-        else gc.drawPolygon(polygon);
+        gc.drawPolygon(polygon);
         polygon.translate(-x, -y);
     }
 
+    @Override
+    protected void drawFilled(Graphics2D gc) {
+        polygon.translate(x, y);
+        gc.fillPolygon(polygon);
+        polygon.translate(-x, -y);
+    }
 
     @Override
     public Point center(Graphics g) {
diff --git a/src/main/java/paintingcanvas/drawable/Rectangle.java b/src/main/java/paintingcanvas/drawable/Rectangle.java
index d85749e..7d07411 100644
--- a/src/main/java/paintingcanvas/drawable/Rectangle.java
+++ b/src/main/java/paintingcanvas/drawable/Rectangle.java
@@ -58,10 +58,13 @@ public Rectangle(int centerX, int centerY, int width, int height, Color color) {
     }
 
     @Override
-    public void draw(Graphics2D gc) {
-        gc.setColor(color);
-        if (this.filled) gc.fillRect(x - width / 2, y - height / 2, width, height);
-        else gc.drawRect(x - width / 2, y - height / 2, width, height);
+    protected void drawOutline(Graphics2D gc) {
+        gc.drawRect(x - width / 2, y - height / 2, width, height);
+    }
+
+    @Override
+    protected void drawFilled(Graphics2D gc) {
+        gc.fillRect(x - width / 2, y - height / 2, width, height);
     }
 
     @Override
diff --git a/src/main/java/paintingcanvas/drawable/Square.java b/src/main/java/paintingcanvas/drawable/Square.java
index 946a668..a7d2a1c 100644
--- a/src/main/java/paintingcanvas/drawable/Square.java
+++ b/src/main/java/paintingcanvas/drawable/Square.java
@@ -49,10 +49,13 @@ public Square(int centerX, int centerY, int size, Color color) {
     }
 
     @Override
-    public void draw(Graphics2D gc) {
-        gc.setColor(this.color);
-        if (this.filled) gc.fillRect(x - size / 2, y - size / 2, size, size);
-        else gc.drawRect(x - size / 2, y - size / 2, size, size);
+    protected void drawFilled(Graphics2D gc) {
+        gc.fillRect(x - size / 2, y - size / 2, size, size);
+    }
+
+    @Override
+    protected void drawOutline(Graphics2D gc) {
+        gc.drawRect(x - size / 2, y - size / 2, size, size);
     }
 
     @Override
diff --git a/src/main/java/paintingcanvas/drawable/Text.java b/src/main/java/paintingcanvas/drawable/Text.java
index c68f118..f9b858d 100644
--- a/src/main/java/paintingcanvas/drawable/Text.java
+++ b/src/main/java/paintingcanvas/drawable/Text.java
@@ -63,8 +63,14 @@ public Text(int x, int y, String text, Color color) {
     }
 
     @Override
-    public void draw(Graphics2D gc) {
-        gc.setColor(color);
+    protected void drawFilled(Graphics2D gc) {
+        gc.setFont(font);
+        var center = center(gc);
+        gc.drawString(text, 2 * x - center.x, 2 * y - center.y);
+    }
+
+    @Override
+    protected void drawOutline(Graphics2D gc) {
         gc.setFont(font);
         var center = center(gc);
         gc.drawString(text, 2 * x - center.x, 2 * y - center.y);
diff --git a/src/main/java/paintingcanvas/drawable/Triangle.java b/src/main/java/paintingcanvas/drawable/Triangle.java
index e21ca8d..1faec72 100644
--- a/src/main/java/paintingcanvas/drawable/Triangle.java
+++ b/src/main/java/paintingcanvas/drawable/Triangle.java
@@ -19,6 +19,16 @@ public class Triangle extends Drawable {
      */
     public int height;
 
+    private Polygon poly;
+
+    private java.awt.Polygon getPolygon() {
+        return new java.awt.Polygon(
+                new int[]{x - width / 2, x + width / 2, x},
+                new int[]{y + height / 2, y + height / 2, y - height / 2},
+                3
+        );
+    }
+
     /**
      * Create a new Triangle element.
      * 
{@code
@@ -56,28 +66,6 @@ public Triangle(int centerX, int centerY, int width, int height, Color color) {
         this.height = height;
     }
 
-    @Override
-    public void draw(Graphics2D gc) {
-        java.awt.Polygon poly = new java.awt.Polygon(
-                new int[]{x - width / 2, x + width / 2, x},
-                new int[]{y + height / 2, y + height / 2, y - height / 2},
-                3
-        );
-        gc.setColor(this.color);
-        if (filled) gc.fillPolygon(poly);
-        else gc.drawPolygon(poly);
-    }
-
-    @Override
-    public Point center(Graphics g) {
-        return new Point(x, y);
-    }
-
-    @Override
-    protected Triangle getThis() {
-        return this;
-    }
-
     /**
      * Gets the width of the triangle.
      *
@@ -121,4 +109,24 @@ public Triangle setHeight(int h) {
         this.height = h;
         return this;
     }
+
+    @Override
+    protected void drawFilled(Graphics2D gc) {
+        gc.fillPolygon(this.getPolygon());
+    }
+
+    @Override
+    protected void drawOutline(Graphics2D gc) {
+        gc.drawPolygon(this.getPolygon());
+    }
+
+    @Override
+    public Point center(Graphics g) {
+        return new Point(x, y);
+    }
+
+    @Override
+    protected Triangle getThis() {
+        return this;
+    }
 }
\ No newline at end of file
diff --git a/src/test/java/examples/Test.java b/src/test/java/examples/Test.java
index eaced42..fcff353 100644
--- a/src/test/java/examples/Test.java
+++ b/src/test/java/examples/Test.java
@@ -24,16 +24,19 @@ public static void main(String[] args) {
                 .setColor(0xff0000);
 
         Polygon gaming = new Polygon(new int[][]{{100, 100}, {200, 200}, {200, 100}})
-                .setColor(0xffff00);
+                .setColor(0xffff00)
+                .setOutline(Color.RED, 5);
         Square square = new Square(0, 0, 100)
-                .setColor(0x00ff00);
+                .setColor(0x00ff00)
+                .setFilled(false);
 
         Path curvey = new Path()
                 .setThickness(10)
                 .cursorTo(300, 300)
                 .lineTo(200, 300)
                 .quadTo(300, 300, 200, 200)
-                .setColor(0x00ffff);
+                .setColor(0x00ffff)
+                .setOutline(Color.BLUE, 15);
         curvey.moveTo(100, 100, 3)
                 .rotateTo(90, 3);
         line.rotateTo(180, 3);