diff --git a/src/main/java/paintingcanvas/canvas/Canvas.java b/src/main/java/paintingcanvas/canvas/Canvas.java index 92bdb96..10e9ccf 100644 --- a/src/main/java/paintingcanvas/canvas/Canvas.java +++ b/src/main/java/paintingcanvas/canvas/Canvas.java @@ -37,18 +37,17 @@ public class Canvas { */ public final CanvasPanel panel; /** - * Used to lock the thread to wait for animations to finish + * Sync with animations: Notifies on animation finish */ protected final Object animationSync = new Object(); /** - * Used to lock the thread to wait for a frame count + * Sync with frame: Notifies on end of frame */ protected final Object frameSync = new Object(); /** - * Used to synchronize with drawables + * Sync with drawables: Use when modifying a drawable */ public static final Object drawableSync = new Object(); - public boolean autoAdd; /** * The current frame */ @@ -58,16 +57,16 @@ public class Canvas { */ public List renderLifecycles = new Vector<>(); /** - * The background color of the canvas + * The options for the behavior of the canvas, see {@link CanvasOptions} */ - public Color backgroundColor = Color.WHITE; + public CanvasOptions options; /** * Initializes the canvas with a default size of 900x600 * and a title of "Canvas" */ public Canvas() { - this(900, 600, "Canvas"); + this(900, 600, "Canvas", new CanvasOptions()); } /** @@ -78,6 +77,18 @@ public Canvas() { * @param title the title of the canvas */ public Canvas(int width, int height, String title) { + this(width, height, title, new CanvasOptions()); + } + + /** + * Initializes the canvas + * + * @param width the width of the canvas + * @param height the height of the canvas + * @param title the title of the canvas + * @param options options for the canvas + */ + public Canvas(int width, int height, String title, CanvasOptions options) { super(); this.startSize = new Dimension(width, height); this.translation = new Point2D.Float(0, 0); @@ -87,12 +98,11 @@ public Canvas(int width, int height, String title) { throw new RuntimeException("There can only be one Canvas instance"); Canvas.globalInstance = this; - this.renderLifecycles.add(new RenderLifecycle.AntiAliasingLifecycle()); - if (getProp("paintingcanvas.autoCenter", true)) + this.options = options; + if (options.antiAlias) + this.renderLifecycles.add(new RenderLifecycle.AntiAliasingLifecycle()); + if (options.autoCenter) this.renderLifecycles.add(new RenderLifecycle.CenteringLifecycle()); - if (getProp("paintingcanvas.autoAdd", true)) - this.autoAdd = true; - render(); } @@ -107,12 +117,6 @@ static public Canvas getGlobalInstance() { return globalInstance; } - private boolean getProp(String prop, boolean _default) { - var val = System.getProperties().getProperty(prop); - if (val == null) return _default; - return Boolean.parseBoolean(val); - } - /** * Sets the background color of the canvas * @@ -120,7 +124,7 @@ private boolean getProp(String prop, boolean _default) { */ @SuppressWarnings("unused") public void setBackgroundColor(Color color) { - this.backgroundColor = color; + this.options.backgroundColor = color; } /** diff --git a/src/main/java/paintingcanvas/canvas/CanvasOptions.java b/src/main/java/paintingcanvas/canvas/CanvasOptions.java new file mode 100644 index 0000000..230a8ba --- /dev/null +++ b/src/main/java/paintingcanvas/canvas/CanvasOptions.java @@ -0,0 +1,37 @@ +package paintingcanvas.canvas; + +import java.awt.*; + +/** + * A list of options that control the behavior of the canvas + */ +public class CanvasOptions { + /** + * Whether to automatically add {@link paintingcanvas.drawable.Drawable Drawable}'s + * upon construction / instantiation. + *

+ * default: {@code true} + */ + public boolean autoAdd = true; + /** + * Whether to automatically center what's on screen whenever the screen is resized + *

+ * default: {@code true} + */ + public boolean autoCenter = true; + /** + * Whether to do antialiasing when drawing shapes + *

+ * default: {@code true} + */ + public boolean antiAlias = true; + + /** + * The background color of the canvas + *

+ * default: {@link Color#WHITE} + */ + public Color backgroundColor = Color.WHITE; + + public CanvasOptions() {} +} diff --git a/src/main/java/paintingcanvas/canvas/CanvasPanel.java b/src/main/java/paintingcanvas/canvas/CanvasPanel.java index 456a516..f6f6c3d 100644 --- a/src/main/java/paintingcanvas/canvas/CanvasPanel.java +++ b/src/main/java/paintingcanvas/canvas/CanvasPanel.java @@ -82,7 +82,7 @@ public void paintComponent(Graphics g) { image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); var ig = (Graphics2D) image.getGraphics(); - ig.setColor(Color.WHITE); + ig.setColor(canvas.options.backgroundColor); ig.fillRect(0, 0, getWidth(), getHeight()); synchronized (canvas.translation) { // System.out.println(canvas.translation); diff --git a/src/main/java/paintingcanvas/drawable/Drawable.java b/src/main/java/paintingcanvas/drawable/Drawable.java index 2e10271..b9ac6fd 100644 --- a/src/main/java/paintingcanvas/drawable/Drawable.java +++ b/src/main/java/paintingcanvas/drawable/Drawable.java @@ -53,7 +53,7 @@ public abstract class Drawable> implements Animatable { this.color = color; var canvas = Canvas.getGlobalInstance(); - if (canvas.autoAdd) { + if (canvas.options.autoAdd) { synchronized (Canvas.drawableSync) { canvas.elements.add(this); } diff --git a/src/test/java/stress_tests/AnimationTest.java b/src/test/java/stress_tests/AnimationTest.java index bf68e72..47b2a1f 100644 --- a/src/test/java/stress_tests/AnimationTest.java +++ b/src/test/java/stress_tests/AnimationTest.java @@ -3,6 +3,7 @@ import paintingcanvas.animation.Animation; import paintingcanvas.animation.Easing; import paintingcanvas.canvas.Canvas; +import paintingcanvas.canvas.CanvasOptions; import paintingcanvas.drawable.Rectangle; import paintingcanvas.extensions.FrameCounter; @@ -17,8 +18,10 @@ public class AnimationTest { public static void main(String[] argv) { System.setProperty("sun.java2d.opengl", "true"); - System.setProperty("paintingcanvas.autoCenter", "false"); - var canvas = new Canvas(width * size, height * size /*+ 32*/, "test"); + + CanvasOptions options = new CanvasOptions(); + options.autoCenter = false; + var canvas = new Canvas(width * size, height * size /*+ 32*/, "test", options); // var rec = new Recorder().attach().record(Path.of("rec"), "jpg"); new FrameCounter().lines(() -> new String[]{ String.format("Frame: %d", canvas.frame),