Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
aspiringLich committed Apr 14, 2023
2 parents 52e284c + 5934243 commit a7d25e1
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 45 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.3.4'
version '1.3.5'

repositories {}
dependencies {}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/paintingcanvas/animation/ColorAnimation.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ Color lerpColor(Color _a, Color _b, double delta) {
var b = _a.getBlue() + (_b.getBlue() - _a.getBlue()) * delta;
var a = _a.getAlpha() + (_b.getAlpha() - _a.getAlpha()) * delta;

return new Color(Misc.clamp(0, (int) r, 255), Misc.clamp(0, (int) g, 255), Misc.clamp(255, (int) b, 255), Misc.clamp(0, (int) a, 255));
return new Color(
Misc.clamp(0, (int) r, 255),
Misc.clamp(0, (int) g, 255),
Misc.clamp(0, (int) b, 255),
Misc.clamp(0, (int) a, 255)
);
}
}
37 changes: 21 additions & 16 deletions src/main/java/paintingcanvas/canvas/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import paintingcanvas.drawable.Drawable;

import java.awt.*;
import java.awt.geom.Point2D;
import java.util.List;
import java.util.Locale;
import java.util.Vector;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand All @@ -22,7 +22,8 @@ public class Canvas {
/**
* the initial size of the Canvas
*/
public final Point startSize;
public final Dimension startSize;
public final Point2D.Float translation;
/**
* the elements that are currently on the canvas
*/
Expand Down Expand Up @@ -78,18 +79,20 @@ public Canvas() {
*/
public Canvas(int width, int height, String title) {
super();
this.startSize = new Point(width, height);
this.startSize = new Dimension(width, height);
this.translation = new Point2D.Float(0, 0);
this.panel = new CanvasPanel(this, width, height, title);

if (globalInstance != null)
throw new RuntimeException("There can only be one Canvas instance");
Canvas.globalInstance = this;

this.renderLifecycles.add(new RenderLifecycle.AntiAliasingLifecycle());
if (enabledProp("paintingcanvas.autoCenter"))
if (getProp("paintingcanvas.autoCenter", true))
this.renderLifecycles.add(new RenderLifecycle.CenteringLifecycle());
if (enabledProp("paintingcanvas.autoAdd"))
if (getProp("paintingcanvas.autoAdd", true))
this.autoAdd = true;

if (globalInstance != null)
throw new RuntimeException("There can only be one Canvas instance");
Canvas.globalInstance = this;
render();
}

Expand All @@ -104,8 +107,10 @@ static public Canvas getGlobalInstance() {
return globalInstance;
}

private boolean enabledProp(String prop) {
return !System.getProperties().getOrDefault(prop, "").toString().toLowerCase(Locale.ROOT).equals("false");
private boolean getProp(String prop, boolean _default) {
var val = System.getProperties().getProperty(prop);
if (val == null) return _default;
return Boolean.parseBoolean(val);
}

/**
Expand All @@ -124,8 +129,9 @@ public void setBackgroundColor(Color color) {
* @return The width of the canvas
*/
public int getWidth() {
if (panel == null) return startSize.width;
var width = panel.getWidth();
return width == 0 ? startSize.x : width;
return width == 0 ? startSize.width : width;
}

/**
Expand All @@ -134,8 +140,9 @@ public int getWidth() {
* @return The height of the canvas
*/
public int getHeight() {
if (panel == null) return startSize.height;
var height = panel.getHeight();
return height == 0 ? startSize.y : height;
return height == 0 ? startSize.height : height;
}

/**
Expand Down Expand Up @@ -203,11 +210,9 @@ public void sleep(double seconds) {

public void render() {
// TODO: Account for the time it takes to run the render function
// (Implement the run with a loop and thread::sleep)
// (Implement the run with a loop and thread::sleep) or dont -- im sure you will get a warning for busy waiting
ScheduledThreadPoolExecutor poolExecutor = new ScheduledThreadPoolExecutor(1);
poolExecutor.scheduleAtFixedRate(() -> {
panel.repaint();
}, 0, 1000000 / fps, TimeUnit.MICROSECONDS);
poolExecutor.scheduleAtFixedRate(panel::repaint, 0, 1000000 / fps, TimeUnit.MICROSECONDS);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/paintingcanvas/canvas/CanvasPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public void paintComponent(Graphics g) {
canvas.frame++;
if (canvas.frame < 0) return;

synchronized (canvas.translation) {
g.translate((int)canvas.translation.x, (int)canvas.translation.y);
}

synchronized (canvas.animations) {
// Update animations
for (int i = 0; i < canvas.animations.size(); i++) {
Expand Down
38 changes: 20 additions & 18 deletions src/main/java/paintingcanvas/canvas/RenderLifecycle.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package paintingcanvas.canvas;

import paintingcanvas.animation.MovementAnimation;
import paintingcanvas.misc.Misc;

import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.geom.Point2D;

public interface RenderLifecycle {
default void renderStart(Graphics g) {
Expand Down Expand Up @@ -45,35 +46,36 @@ public void renderStart(Graphics g) {
}

class CenteringLifecycle implements RenderLifecycle {
private static Dimension lastSize;
final int error = 50;
private Point2D.Double lastSize;
private boolean active = false;

@Override
public void onResize(CanvasPanel canvasComponent, ComponentEvent e) {
if (lastSize == null) {
lastSize = e.getComponent().getSize();
if (e.getComponent() == null) return;
var canvas = Canvas.getGlobalInstance();
if (lastSize == null || !this.active) {
var size = e.getComponent().getSize();
lastSize = new Point2D.Double(size.width, size.height);
if (Misc.equality(lastSize.x, canvas.startSize.width, error) &&
Misc.equality(lastSize.y, canvas.startSize.height, error))
active = true;
return;
}

var canvas = Canvas.getGlobalInstance();
var newSize = canvasComponent.jframe.getSize();
var _newSize = canvasComponent.jframe.getSize();
var newSize = new Point2D.Double(_newSize.width, _newSize.height);
if (lastSize.equals(newSize)) return;

var widthDiff = (newSize.width - lastSize.width) / 2f;
var heightDiff = (newSize.height - lastSize.height) / 2f;
var widthDiff = (newSize.x - lastSize.x) / 2f;
var heightDiff = (newSize.y - lastSize.y) / 2f;
lastSize = newSize;

synchronized (Canvas.drawableSync) {
canvas.elements.forEach(s -> {
s.move((int)widthDiff, (int)heightDiff);
});
synchronized (canvas.translation) {
var old = canvas.translation;
canvas.translation.setLocation(old.x + widthDiff, old.y + heightDiff);
}

canvas.animations.stream().filter(a -> a instanceof MovementAnimation).forEach(s -> {
var anim = (MovementAnimation) s;
anim.start = new Point(anim.start.x + (int) widthDiff, anim.start.y + (int) heightDiff);
anim.end = new Point(anim.end.x + (int) widthDiff, anim.end.y + (int) heightDiff);
});

canvas.panel.jframe.repaint();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/paintingcanvas/drawable/Drawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void render(Graphics g) {
var gc = (Graphics2D) g;
var transform = gc.getTransform();
var center = this.center(g);
transform.setToRotation(this.rotation, center.x, center.y);
transform.rotate(this.rotation, center.x, center.y);
gc.setTransform(transform);

// if filled, draw filled then outline
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/paintingcanvas/extensions/InfoDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ public class InfoDisplay implements RenderLifecycle {
);
private final Stroke stroke = new BasicStroke(10);
private int fontSize = 12;
private Font font = new Font(Font.DIALOG, Font.PLAIN, 12);;
private Font font = new Font(Font.DIALOG, Font.PLAIN, 12);
;
private Color textColor = Color.black;

public InfoDisplay() {}
public InfoDisplay() {
}

/**
* Set the font size of the displayed text
*
* @param fontSize The new font size
*/
public void setFontSize(int fontSize) {
this.fontSize = fontSize;
this.font = new Font(Font.DIALOG, Font.PLAIN, fontSize);;
this.font = new Font(Font.DIALOG, Font.PLAIN, fontSize);
;
}

public void setFont(Font font) {
Expand Down Expand Up @@ -67,6 +71,8 @@ public void renderEnd(Graphics g) {
int x = 10, y = 30;
if (mouse.x >= width - textWidth) x = -x - textWidth;
if (mouse.y >= height - textHeight) y = -textHeight;
x += mouse.x;
y += mouse.y;

var cmp = canvas.panel;
var img = new BufferedImage(cmp.getWidth(), cmp.getHeight(), BufferedImage.TYPE_INT_RGB);
Expand Down Expand Up @@ -94,12 +100,12 @@ public void renderEnd(Graphics g) {
gc.drawLine(0, mouse.y, width, mouse.y);

gc.setColor(new Color(0, 0, 0, 180));
gc.fillRect(mouse.x + x - pad, mouse.y + y - fontSize - pad, textWidth + pad * 2, textHeight + pad * 2);
gc.fillRect(x - pad, y - fontSize - pad, textWidth + pad * 2, textHeight + pad * 2);

gc.setFont(font);
gc.setColor(Color.white);
gc.drawString(String.format("pos: (%d, %d)", mouse.x, mouse.y), mouse.x + x, mouse.y + y);
gc.drawString(String.format("rgb: (%d, %d, %d)", red, green, blue), mouse.x + x, mouse.y + y + fontSize);
gc.drawString(String.format("hex: (#%06X)", hex & 0x00ffffff), mouse.x + x, mouse.y + y + fontSize * 2);
gc.drawString(String.format("pos: (%d, %d)", mouse.x, mouse.y), x, y);
gc.drawString(String.format("rgb: (%d, %d, %d)", red, green, blue), x, y + fontSize);
gc.drawString(String.format("hex: (#%06X)", hex & 0x00ffffff), x, y + fontSize * 2);
}
}
6 changes: 5 additions & 1 deletion src/main/java/paintingcanvas/misc/Misc.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ public static <N extends Number> N clamp(N min, N val, N max) {
if (val.doubleValue() < min.doubleValue()) return min;
return val;
}
}

public static <N extends Number> boolean equality(N a, N b, N error) {
var err = error.doubleValue() / 2;
return Math.max(a.doubleValue(), b.doubleValue()) - err <= Math.min(a.doubleValue(), b.doubleValue()) + err;
}
}

0 comments on commit a7d25e1

Please sign in to comment.