Skip to content

Commit

Permalink
Restructure of Drawable & Outlines!
Browse files Browse the repository at this point in the history
  • Loading branch information
aspiringLich committed Feb 6, 2023
1 parent 7f8311d commit e3bf034
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 75 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

// what should the group-id be breon
group 'paintingcanvas'
version '1.2.2'
version '1.2.3'

repositories {}
dependencies {}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/paintingcanvas/animation/OpacityAnimation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}
11 changes: 7 additions & 4 deletions src/main/java/paintingcanvas/drawable/Circle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 62 additions & 7 deletions src/main/java/paintingcanvas/drawable/Drawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,50 @@ public abstract class Drawable<T extends Drawable<T>> 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;
Expand All @@ -43,13 +87,6 @@ public abstract class Drawable<T extends Drawable<T>> 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
*
Expand Down Expand Up @@ -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.
* <pre>{@code
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/paintingcanvas/drawable/Ellipse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/paintingcanvas/drawable/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
29 changes: 11 additions & 18 deletions src/main/java/paintingcanvas/drawable/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ public class Path extends Drawable<Path> {
* @see Path2D
*/
Path2D path;
/**
* The stroke of the line
* @see Stroke
*/
Stroke stroke;

/**
* Create a new Path element. The path is initially empty.
Expand All @@ -36,7 +31,6 @@ public class Path extends Drawable<Path> {
public Path() {
super(0, 0, Color.BLACK);
this.path = new Path2D.Double();
this.stroke = new BasicStroke(1);
this.filled = false;
}

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

/**
Expand All @@ -67,7 +60,7 @@ public Path(Color color) {
*/
@SuppressWarnings("unused")
public Path setStroke(Stroke stroke) {
this.stroke = stroke;
this.unfilledStroke = stroke;
return this;
}

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

Expand Down Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/paintingcanvas/drawable/Polygon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/paintingcanvas/drawable/Rectangle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/paintingcanvas/drawable/Square.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/paintingcanvas/drawable/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
52 changes: 30 additions & 22 deletions src/main/java/paintingcanvas/drawable/Triangle.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ public class Triangle extends Drawable<Triangle> {
*/
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.
* <pre>{@code
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
Loading

0 comments on commit e3bf034

Please sign in to comment.