Skip to content

Commit

Permalink
Improve auto-centering
Browse files Browse the repository at this point in the history
and fix blue color chanel clamp
  • Loading branch information
connorslade committed Mar 13, 2023
1 parent 2877917 commit d3e477f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 38 deletions.
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 @@ -74,18 +75,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 @@ -100,8 +103,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 @@ -119,8 +124,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 @@ -129,8 +135,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 @@ -198,10 +205,8 @@ 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);
}
}
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
39 changes: 20 additions & 19 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,36 +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.elements) {
canvas.elements.forEach(s -> {
s.x += widthDiff;
s.y += 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 @@ -137,7 +137,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
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,4 +6,8 @@ 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) {
return Math.max(a.doubleValue(), b.doubleValue()) - error.doubleValue() <= Math.min(a.doubleValue(), b.doubleValue()) + error.doubleValue();
}
}

0 comments on commit d3e477f

Please sign in to comment.