Skip to content

Commit

Permalink
CanvasOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
aspiringLich committed Apr 27, 2023
1 parent a2c650f commit 9e3bcee
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 23 deletions.
42 changes: 23 additions & 19 deletions src/main/java/paintingcanvas/canvas/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -58,16 +57,16 @@ public class Canvas {
*/
public List<RenderLifecycle> 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());
}

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

Expand All @@ -107,20 +117,14 @@ 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
*
* @param color the new background color
*/
@SuppressWarnings("unused")
public void setBackgroundColor(Color color) {
this.backgroundColor = color;
this.options.backgroundColor = color;
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/paintingcanvas/canvas/CanvasOptions.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* default: {@code true}
*/
public boolean autoAdd = true;
/**
* Whether to automatically center what's on screen whenever the screen is resized
* <p>
* default: {@code true}
*/
public boolean autoCenter = true;
/**
* Whether to do antialiasing when drawing shapes
* <p>
* default: {@code true}
*/
public boolean antiAlias = true;

/**
* The background color of the canvas
* <p>
* default: {@link Color#WHITE}
*/
public Color backgroundColor = Color.WHITE;

public CanvasOptions() {}
}
2 changes: 1 addition & 1 deletion src/main/java/paintingcanvas/canvas/CanvasPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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 @@ -53,7 +53,7 @@ public abstract class Drawable<T extends Drawable<T>> implements Animatable {
this.color = color;

var canvas = Canvas.getGlobalInstance();
if (canvas.autoAdd) {
if (canvas.options.autoAdd) {
synchronized (Canvas.drawableSync) {
canvas.elements.add(this);
}
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/stress_tests/AnimationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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),
Expand Down

0 comments on commit 9e3bcee

Please sign in to comment.