Skip to content

Commit

Permalink
Turn CanvasOptions into a builder
Browse files Browse the repository at this point in the history
The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming. The intent of the builder design pattern is to separate the construction of a complex object from its representation.
  • Loading branch information
connorslade committed Sep 18, 2023
1 parent 45f17a5 commit 4271d9c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
82 changes: 82 additions & 0 deletions src/main/java/paintingcanvas/canvas/CanvasOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.awt.*;

// TODO: Make options private as they have getters and setters

/**
* A list of options that control the behavior of the canvas
*/
Expand Down Expand Up @@ -35,4 +37,84 @@ public class CanvasOptions {

public CanvasOptions() {
}

/**
* @return Weather {@link paintingcanvas.drawable.Drawable Drawable}s are automatically added to the canvas upon construction
* @see #autoAdd(boolean)
*/
public boolean isAutoAdd() {
return autoAdd;
}

/**
* Default: {@code true}
*
* @param autoAdd Whether to automatically add {@link paintingcanvas.drawable.Drawable Drawable}'s upon construction / instantiation.
* @return This object for chaining
* @see #isAutoAdd()
*/
public CanvasOptions autoAdd(boolean autoAdd) {
this.autoAdd = autoAdd;
return this;
}

/**
* @return Whether to automatically center what's on screen whenever the screen is resized
* @see #autoCenter(boolean)
*/
public boolean isAutoCenter() {
return autoCenter;
}

/**
* Default: {@code true}
*
* @param autoCenter Whether to automatically center what's on screen whenever the screen is resized
* @return This object for chaining
* @see #isAutoCenter()
*/
public CanvasOptions autoCenter(boolean autoCenter) {
this.autoCenter = autoCenter;
return this;
}

/**
* @return Whether to use antialiasing
* @see #antiAlias(boolean)
*/
public boolean isAntiAlias() {
return antiAlias;
}

/**
* Default: {@code true}
*
* @param antiAlias Whether to use antialiasing
* @return This object for chaining
* @see #isAntiAlias()
*/
public CanvasOptions antiAlias(boolean antiAlias) {
this.antiAlias = antiAlias;
return this;
}

/**
* @return The background color of the canvas
* @see #backgroundColor(Color)
*/
public Color getBackgroundColor() {
return backgroundColor;
}

/**
* Default: {@link Color#WHITE}
*
* @param backgroundColor The background color of the canvas
* @return This object for chaining
* @see #getBackgroundColor()
*/
public CanvasOptions backgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
return this;
}
}
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 @@ -59,7 +59,7 @@ public abstract class Drawable<T extends Drawable<T>> implements Animatable {
this.color = color;

var canvas = Canvas.getGlobalInstance();
if (canvas.options.autoAdd)
if (canvas.options.isAutoAdd())
canvas.elements.add(this);
}

Expand Down
3 changes: 1 addition & 2 deletions src/test/java/stress_tests/AnimationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public class AnimationTest {
public static void main(String[] argv) {
System.setProperty("sun.java2d.opengl", "true");

CanvasOptions options = new CanvasOptions();
options.autoCenter = false;
var options = new CanvasOptions().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[]{
Expand Down

0 comments on commit 4271d9c

Please sign in to comment.