Interface Graphics


public interface Graphics
Minimal drawing API for sketches.

This interface is a thin wrapper around the underlying rendering backend (Processing) with a much smaller surface area.

Coordinates and units

  • Coordinates are in pixels.
  • Default origin is the top-left of the drawable viewport: (0, 0).
  • Positive x goes right. Positive y goes down.

Colors

  • RGB(A) channel values are in [0, 255].
  • Grayscale values are in [0, 255].

State and isolation

  • Fill, stroke, stroke weight, text size, and image tint are part of the current drawing state, along with transformations.
  • Use isolate(Runnable) to contain transformations and style changes.
  • Use isolateStyle(Runnable) to contain style changes only (fill, stroke, stroke weight, text size, and image tint).
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static enum 
    Line end-cap style used for stroked lines and shape outlines.
  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    at(double x, double y, Runnable body)
    Runs body after translating to (x, y), then restores the previous state.
    default void
    at(Vec2 position, Runnable body)
    Runs a drawing block with its origin at the given position, restoring the previous state afterward.
    default void
    background(double gray)
    Fills the viewport with a solid grayscale color.
    default void
    background(double gray, double a)
    Fills the viewport with a solid grayscale color.
    void
    background(double r, double g, double b)
    Fills the viewport with a solid RGB color.
    void
    background(double r, double g, double b, double a)
    Fills the viewport with a solid RGB color.
    default void
    Fills the viewport with a Color.
    void
    circle(double cx, double cy, double r)
    Draws a circle defined by center and radius.
    default void
    circle(Vec2 center, double radius)
    Draws a circle centered at a vector at.
    void
    Draws a closed shape defined by vertex(double, double) or curveVertex(double, double) calls inside the block.
    void
    curveVertex(double x, double y)
    Adds a smooth curve point to the current shape.
    default void
    curveVertex(Vec2 position)
    Adds a smooth curve vertex at the given position.
    double
    cx()
    Returns center x coordinate of the viewport (width() / 2.0).
    double
    cy()
    Returns center y coordinate of the viewport (height() / 2.0).
    void
    ellipse(double cx, double cy, double w, double h)
    Draws an ellipse defined by center, width, and height.
    default void
    fill(double gray)
    Sets the fill color to a grayscale value.
    default void
    fill(double gray, double a)
    Sets the fill color to a grayscale value.
    void
    fill(double r, double g, double b)
    Sets the fill color for subsequent shapes.
    void
    fill(double r, double g, double b, double a)
    Sets the fill color for subsequent shapes.
    default void
    Sets the fill Color for subsequent shapes.
    default int
    h()
    Short alias for height().
    int
    Returns height of the drawable viewport in pixels.
    default void
    image(Image image, double x, double y)
    Draws an image at its native dimensions.
    void
    image(Image image, double x, double y, double w, double h)
    Draws an image, resizing it to the given destination dimensions.
    void
    Runs the block with isolated transform and style state.
    default void
    Convenience overload for isolating a block that wants the current Graphics.
    void
    Runs the block with isolated style state only.
    void
    line(double x1, double y1, double x2, double y2)
    Draws a line from (x1, y1) to (x2, y2).
    default void
    line(Vec2 start, Vec2 end)
    Draws a line between two positions.
    Loads an image from a file or packaged resource.
    void
    Disables filling for subsequent shapes.
    void
    Disables drawing outlines for subsequent shapes.
    void
    Clears the image and sprite tint for subsequent draws.
    void
    point(double x, double y)
    Draws a single point at (x, y).
    default void
    point(Vec2 position)
    Draws a point at a vector position.
    void
    Draws a connected open polyline defined by vertex(double, double) or curveVertex(double, double) calls inside the block.
    void
    quad(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
    Draws a quadrilateral connecting four points.
    void
    rect(double x, double y, double w, double h)
    Draws a rectangle with a top-left corner at (x, y).
    default void
    rect(Vec2 position, double width, double height)
    Draws a rectangle at the given top-left position.
    default void
    rect(Vec2 position, Vec2 size)
    Draws a rectangle using vectors for its position and dimensions.
    default void
    rectCenter(double cx, double cy, double w, double h)
    Draws a rectangle centered at (cx, cy).
    default void
    rectCenter(Vec2 center, double width, double height)
    Draws a rectangle centered at the given position.
    void
    rotate(double radians)
    Rotates the coordinate system around the current origin (0, 0).
    default void
    rotated(double radians, Runnable body)
    Runs body after rotating by radians, then restores the previous state.
    default void
    rotatedAt(double radians, double cx, double cy, Runnable body)
    Runs body with its origin translated to (cx, cy) and rotated.
    void
    scale(double factor)
    Scales the coordinate system uniformly in both dimensions.
    void
    scale(double x, double y)
    Scales the coordinate system independently in x and y.
    default void
    scaled(double factor, Runnable body)
    Runs body after scaling uniformly by factor, then restores the previous state.
    default void
    scaledAt(double factor, double cx, double cy, Runnable body)
    Runs body after translating to (cx, cy) and uniformly scaling the coordinate system.
    int
    Current scene index selected by the user (typically 1..9).
    void
    shearX(double radians)
    Shears the coordinate system along the x-axis.
    void
    shearY(double radians)
    Shears the coordinate system along the y-axis.
    default void
    Draws a sprite using its position, anchor, scale, rotation, flips, and optional tint.
    default void
    sprite(Sprite sprite, double x, double y)
    Draws a sprite at its native dimensions.
    void
    sprite(Sprite sprite, double x, double y, double w, double h)
    Draws a region of an image at the given destination dimensions.
    void
    square(double x, double y, double side)
    Draws a square with a top-left corner at (x, y).
    default void
    stroke(double gray)
    Sets the stroke (outline) color to a grayscale value.
    default void
    stroke(double gray, double a)
    Sets the stroke (outline) color to a grayscale value.
    void
    stroke(double r, double g, double b)
    Sets the stroke (outline) color for subsequent shapes.
    void
    stroke(double r, double g, double b, double a)
    Sets the stroke (outline) color for subsequent shapes.
    default void
    Sets the stroke (outline) Color for subsequent shapes.
    void
    Sets the stroke end-cap style for subsequent stroked drawing operations.
    void
    strokeWeight(double px)
    Sets the stroke width in pixels.
    default void
    text(double x, double y, String text)
    Draws text at (x, y) with the default size of 16 pixels.
    void
    text(double x, double y, String text, double size)
    Draws text at (x, y), with y specifying the text baseline.
    void
    tint(double r, double g, double b, double a)
    Sets a color multiplier for subsequently drawn images and sprites.
    default void
    tint(Color color)
    Sets the tint for images and sprites.
    void
    translate(double x, double y)
    Offsets the coordinate system by the given amount.
    default void
    translate(Vec2 offset)
    Translates the coordinate system by a vector.
    void
    triangle(double x1, double y1, double x2, double y2, double x3, double y3)
    Draws a triangle connecting three points.
    void
    vertex(double x, double y)
    Adds a point to the current shape.
    default void
    vertex(Vec2 position)
    Adds a vertex at the given position to the current shape.
    default int
    w()
    Short alias for width().
    int
    Returns width of the drawable viewport in pixels.
  • Method Details

    • width

      int width()
      Returns width of the drawable viewport in pixels.
      Returns:
      width of the viewport
    • height

      int height()
      Returns height of the drawable viewport in pixels.
      Returns:
      height of the viewport
    • cx

      double cx()
      Returns center x coordinate of the viewport (width() / 2.0).
      Returns:
      center x coordinate of the viewport
    • cy

      double cy()
      Returns center y coordinate of the viewport (height() / 2.0).
      Returns:
      center y coordinate of the viewport
    • scene

      int scene()
      Current scene index selected by the user (typically 1..9).

      How scenes are selected is backend-controlled. Treat this as a read-only selector used to switch between different visuals.

      Returns:
      current scene index
    • background

      void background(double r, double g, double b)
      Fills the viewport with a solid RGB color.
      Parameters:
      r - red (0..255)
      g - green (0..255)
      b - blue (0..255)
    • background

      void background(double r, double g, double b, double a)
      Fills the viewport with a solid RGB color.
      Parameters:
      r - red (0..255)
      g - green (0..255)
      b - blue (0..255)
      a - alpha (0..255)
    • background

      default void background(Color c)
      Fills the viewport with a Color.
      Parameters:
      c - background color
    • background

      default void background(double gray)
      Fills the viewport with a solid grayscale color.
      Parameters:
      gray - value (0=black, 255=white)
    • background

      default void background(double gray, double a)
      Fills the viewport with a solid grayscale color.
      Parameters:
      gray - value (0=black, 255=white)
      a - alpha (0..255)
    • fill

      void fill(double r, double g, double b)
      Sets the fill color for subsequent shapes.
      Parameters:
      r - red (0..255)
      g - green (0..255)
      b - blue (0..255)
    • fill

      void fill(double r, double g, double b, double a)
      Sets the fill color for subsequent shapes.
      Parameters:
      r - red (0..255)
      g - green (0..255)
      b - blue (0..255)
      a - alpha (0..255)
    • fill

      default void fill(Color c)
      Sets the fill Color for subsequent shapes.
      Parameters:
      c - fill color
    • fill

      default void fill(double gray)
      Sets the fill color to a grayscale value.
      Parameters:
      gray - value (0=black, 255=white)
    • fill

      default void fill(double gray, double a)
      Sets the fill color to a grayscale value.
      Parameters:
      gray - value (0=black, 255=white)
      a - alpha (0..255)
    • noFill

      void noFill()
      Disables filling for subsequent shapes.
    • stroke

      void stroke(double r, double g, double b)
      Sets the stroke (outline) color for subsequent shapes.
      Parameters:
      r - red (0..255)
      g - green (0..255)
      b - blue (0..255)
    • stroke

      default void stroke(Color c)
      Sets the stroke (outline) Color for subsequent shapes.
      Parameters:
      c - stroke color
    • stroke

      void stroke(double r, double g, double b, double a)
      Sets the stroke (outline) color for subsequent shapes.
      Parameters:
      r - red (0..255)
      g - green (0..255)
      b - blue (0..255)
      a - alpha (0..255)
    • stroke

      default void stroke(double gray)
      Sets the stroke (outline) color to a grayscale value.
      Parameters:
      gray - value (0=black, 255=white)
    • stroke

      default void stroke(double gray, double a)
      Sets the stroke (outline) color to a grayscale value.
      Parameters:
      gray - value (0=black, 255=white)
      a - alpha (0..255)
    • noStroke

      void noStroke()
      Disables drawing outlines for subsequent shapes.
    • strokeWeight

      void strokeWeight(double px)
      Sets the stroke width in pixels.
      Parameters:
      px - width in pixels
    • strokeCap

      void strokeCap(Graphics.StrokeCap cap)
      Sets the stroke end-cap style for subsequent stroked drawing operations.

      The default is SQUARE.

      Example:

      
       g.strokeWeight(6);
       g.strokeCap(Graphics.StrokeCap.ROUND);
       g.line(50, 50, 250, 50);
       
      Parameters:
      cap - the desired stroke cap style (must not be null)
      Throws:
      NullPointerException - if cap is null
    • line

      void line(double x1, double y1, double x2, double y2)
      Draws a line from (x1, y1) to (x2, y2).
      Parameters:
      x1 - first endpoint x-coordinate
      y1 - first endpoint y-coordinate
      x2 - second endpoint x-coordinate
      y2 - second endpoint y-coordinate
    • rect

      void rect(double x, double y, double w, double h)
      Draws a rectangle with a top-left corner at (x, y).
      Parameters:
      x - top-left x
      y - top-left y
      w - width
      h - height
    • square

      void square(double x, double y, double side)
      Draws a square with a top-left corner at (x, y).
      Parameters:
      x - top-left x
      y - top-left y
      side - side length
    • ellipse

      void ellipse(double cx, double cy, double w, double h)
      Draws an ellipse defined by center, width, and height.
      Parameters:
      cx - center x
      cy - center y
      w - width
      h - height
    • circle

      void circle(double cx, double cy, double r)
      Draws a circle defined by center and radius.
      Parameters:
      cx - center x
      cy - center y
      r - radius (pixels)
    • triangle

      void triangle(double x1, double y1, double x2, double y2, double x3, double y3)
      Draws a triangle connecting three points.
      Parameters:
      x1 - x of point 1
      y1 - y of point 1
      x2 - x of point 2
      y2 - y of point 2
      x3 - x of point 3
      y3 - y of point 3
    • quad

      void quad(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
      Draws a quadrilateral connecting four points.
      Parameters:
      x1 - x of point 1
      y1 - y of point 1
      x2 - x of point 2
      y2 - y of point 2
      x3 - x of point 3
      y3 - y of point 3
      x4 - x of point 4
      y4 - y of point 4
    • point

      void point(double x, double y)
      Draws a single point at (x, y).
      Parameters:
      x - horizontal coordinate
      y - vertical coordinate
    • translate

      void translate(double x, double y)
      Offsets the coordinate system by the given amount.
      Parameters:
      x - horizontal offset
      y - vertical offset
    • rotate

      void rotate(double radians)
      Rotates the coordinate system around the current origin (0, 0).

      With the default screen coordinate system (y down), positive angles rotate clockwise.

      Parameters:
      radians - angle to rotate, in radians
    • shearX

      void shearX(double radians)
      Shears the coordinate system along the x-axis.
      Parameters:
      radians - shear angle in radians
    • shearY

      void shearY(double radians)
      Shears the coordinate system along the y-axis.
      Parameters:
      radians - shear angle in radians
    • scale

      void scale(double factor)
      Scales the coordinate system uniformly in both dimensions.
      Parameters:
      factor - scale factor
    • scale

      void scale(double x, double y)
      Scales the coordinate system independently in x and y.
      Parameters:
      x - horizontal scale factor
      y - vertical scale factor
    • isolate

      void isolate(Runnable block)
      Runs the block with isolated transform and style state.

      Any changes made inside the block (translate/rotate/scale and fill/stroke/stroke weight/text size/image tint) are discarded when this method returns.

      Parameters:
      block - code to run in isolation
    • isolate

      default void isolate(Consumer<Graphics> cg)
      Convenience overload for isolating a block that wants the current Graphics.
      Parameters:
      cg - block that receives this Graphics
    • isolateStyle

      void isolateStyle(Runnable block)
      Runs the block with isolated style state only.

      Style changes (fill, stroke, strokeWeight/Cap, text size, tint) are discarded when this method returns. Transform changes (translate/rotate/scale) are not isolated.

      Parameters:
      block - code to run with isolated style
    • w

      default int w()
      Short alias for width().
      Returns:
      viewport width in pixels
    • h

      default int h()
      Short alias for height().
      Returns:
      viewport height in pixels
    • rectCenter

      default void rectCenter(double cx, double cy, double w, double h)
      Draws a rectangle centered at (cx, cy).
      Parameters:
      cx - center x
      cy - center y
      w - width
      h - height
    • at

      default void at(double x, double y, Runnable body)
      Runs body after translating to (x, y), then restores the previous state.
      Parameters:
      x - horizontal offset
      y - vertical offset
      body - drawing operations to run
    • rotated

      default void rotated(double radians, Runnable body)
      Runs body after rotating by radians, then restores the previous state.
      Parameters:
      radians - angle of rotation
      body - drawing operations to run
    • rotatedAt

      default void rotatedAt(double radians, double cx, double cy, Runnable body)
      Runs body with its origin translated to (cx, cy) and rotated.

      Inside the block, drawing at (0, 0) places a shape at the pivot. Coordinates are relative to that pivot, and the previous transform and style are restored after the block finishes.

      Parameters:
      radians - rotation angle in radians
      cx - pivot x-coordinate
      cy - pivot y-coordinate
      body - drawing operations to run
    • scaled

      default void scaled(double factor, Runnable body)
      Runs body after scaling uniformly by factor, then restores the previous state.
      Parameters:
      factor - scale multiplier
      body - drawing operations to run
    • scaledAt

      default void scaledAt(double factor, double cx, double cy, Runnable body)
      Runs body after translating to (cx, cy) and uniformly scaling the coordinate system. The previous state is restored afterward.
      Parameters:
      factor - scale multiplier
      cx - x-coordinate of the new origin
      cy - y-coordinate of the new origin
      body - drawing operations to run
    • vertex

      void vertex(double x, double y)
      Adds a point to the current shape.

      Only valid inside polyline(Runnable) or closedShape(Runnable).

      Parameters:
      x - vertex x-coordinate
      y - vertex y-coordinate
    • curveVertex

      void curveVertex(double x, double y)
      Adds a smooth curve point to the current shape.

      Only valid inside polyline(Runnable) or closedShape(Runnable).

      Parameters:
      x - curve vertex x-coordinate
      y - curve vertex y-coordinate
    • polyline

      void polyline(Runnable body)
      Draws a connected open polyline defined by vertex(double, double) or curveVertex(double, double) calls inside the block.

      The block should only call vertex(double, double) and/or curveVertex(double, double). Nested shape blocks are not supported. Style changes are isolated; transform changes inside the block are not isolated.

      
       g.polyline(() -> {
           g.curveVertex(0, 100);
           g.curveVertex(50, 0);
           g.curveVertex(100, 100);
       });
       
      Parameters:
      body - code that calls vertex(double, double) or curveVertex(double, double) to define the shape
    • closedShape

      void closedShape(Runnable body)
      Draws a closed shape defined by vertex(double, double) or curveVertex(double, double) calls inside the block.

      The block should only call vertex(double, double) and/or curveVertex(double, double). Nested shape blocks are not supported. Style changes are isolated; transform changes inside the block are not isolated.

      
       g.fill(255, 0, 0);
       g.closedShape(() -> {
           g.vertex(0, 100);
           g.vertex(50, 0);
           g.vertex(100, 100);
       });
       
      Parameters:
      body - code that calls vertex(double, double) or curveVertex(double, double) to define the shape
    • text

      void text(double x, double y, String text, double size)
      Draws text at (x, y), with y specifying the text baseline.
      Parameters:
      x - horizontal position in pixels
      y - baseline position in pixels
      text - text to draw
      size - text size in pixels
    • text

      default void text(double x, double y, String text)
      Draws text at (x, y) with the default size of 16 pixels. The y-coordinate specifies the text baseline.
      Parameters:
      x - horizontal position in pixels
      y - baseline position in pixels
      text - text to draw
    • tint

      void tint(double r, double g, double b, double a)
      Sets a color multiplier for subsequently drawn images and sprites. White preserves the source colors; the alpha channel controls opacity. Tint does not affect geometric shapes.
      Parameters:
      r - red multiplier, 0..255
      g - green multiplier, 0..255
      b - blue multiplier, 0..255
      a - opacity, 0..255
    • tint

      default void tint(Color color)
      Sets the tint for images and sprites.
      Parameters:
      color - color multiplier, including alpha
    • noTint

      void noTint()
      Clears the image and sprite tint for subsequent draws.
    • loadImage

      Image loadImage(String path)
      Loads an image from a file or packaged resource.

      If images are stored in the project's assets/ directory, pass the directory as part of the path, for example g.loadImage("assets/characters.png"). The assets/ prefix is not added automatically.

      Parameters:
      path - image path or resource name
      Returns:
      loaded image
    • image

      void image(Image image, double x, double y, double w, double h)
      Draws an image, resizing it to the given destination dimensions.
      Parameters:
      image - image to draw
      x - destination left edge
      y - destination top edge
      w - destination width in pixels
      h - destination height in pixels
    • image

      default void image(Image image, double x, double y)
      Draws an image at its native dimensions.
      Parameters:
      image - image to draw
      x - destination left edge
      y - destination top edge
    • sprite

      void sprite(Sprite sprite, double x, double y, double w, double h)
      Draws a region of an image at the given destination dimensions.
      Parameters:
      sprite - source sprite region
      x - destination left edge
      y - destination top edge
      w - destination width in pixels
      h - destination height in pixels
    • sprite

      default void sprite(Sprite sprite, double x, double y)
      Draws a sprite at its native dimensions.
      Parameters:
      sprite - source sprite region
      x - destination left edge
      y - destination top edge
    • sprite

      default void sprite(SpriteDraw draw)
      Draws a sprite using its position, anchor, scale, rotation, flips, and optional tint.

      Style and transforms are isolated, so the drawing instructions do not change the state used for subsequent shapes or sprites.

      Parameters:
      draw - drawing instructions for the sprite
    • point

      default void point(Vec2 position)
      Draws a point at a vector position.
      Parameters:
      position - point location
    • line

      default void line(Vec2 start, Vec2 end)
      Draws a line between two positions.
      Parameters:
      start - first endpoint
      end - second endpoint
    • circle

      default void circle(Vec2 center, double radius)
      Draws a circle centered at a vector at.
      Parameters:
      center - circle center
      radius - circle radius in pixels
    • translate

      default void translate(Vec2 offset)
      Translates the coordinate system by a vector.
      Parameters:
      offset - horizontal and vertical displacement
    • at

      default void at(Vec2 position, Runnable body)
      Runs a drawing block with its origin at the given position, restoring the previous state afterward.
      Parameters:
      position - new origin
      body - drawing operations to run
    • rect

      default void rect(Vec2 position, double width, double height)
      Draws a rectangle at the given top-left position.
      Parameters:
      position - top-left corner
      width - rectangle width
      height - rectangle height
    • rect

      default void rect(Vec2 position, Vec2 size)
      Draws a rectangle using vectors for its position and dimensions.
      Parameters:
      position - top-left corner
      size - width and height
    • rectCenter

      default void rectCenter(Vec2 center, double width, double height)
      Draws a rectangle centered at the given position.
      Parameters:
      center - rectangle center
      width - rectangle width
      height - rectangle height
    • vertex

      default void vertex(Vec2 position)
      Adds a vertex at the given position to the current shape.
      Parameters:
      position - vertex coordinates
    • curveVertex

      default void curveVertex(Vec2 position)
      Adds a smooth curve vertex at the given position.
      Parameters:
      position - curve vertex coordinates