Package playn.core

Class Canvas

java.lang.Object
playn.core.Canvas
All Implemented Interfaces:
AutoCloseable, Closeable

public abstract class Canvas extends Object implements Closeable
A 2D drawing canvas. Rendering is performed by the CPU into a bitmap.
  • Field Details

    • image

      public final Image image
      The image that underlies this canvas.
    • width

      public final float width
      The width of this canvas.
    • height

      public final float height
      The height of this canvas.
  • Method Details

    • snapshot

      public abstract Image snapshot()
      Returns an immutable snapshot of the image that backs this canvas. Subsequent changes to this canvas will not be reflected in the returned image. If you are going to render a canvas image into another canvas image a lot, using a snapshot can improve performance.
    • close

      public void close()
      Informs the platform that this canvas, and its backing image will no longer be used. On some platforms this can free up memory earlier than if we waited for the canvas to be garbage collected.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • clear

      public abstract Canvas clear()
      Clears the entire canvas to rgba(0, 0, 0, 0).
    • clearRect

      public abstract Canvas clearRect(float x, float y, float width, float height)
      Clears the specified region to rgba (0, 0, 0, 0).
    • clip

      public abstract Canvas clip(Path clipPath)
      Intersects the current clip with the specified path.
    • clipRect

      public abstract Canvas clipRect(float x, float y, float width, float height)
      Intersects the current clip with the supplied rectangle.
    • createPath

      public Path createPath()
      Creates a path object.
    • createGradient

      public Gradient createGradient(Gradient.Config config)
      Creates a gradient fill pattern.
    • draw

      public Canvas draw(Canvas.Drawable image, float x, float y)
      Draws image at the specified location (x, y).
    • drawCentered

      public Canvas drawCentered(Canvas.Drawable image, float x, float y)
      Draws image centered at the specified location. Subtracts image.width/2 from x and image.height/2 from y.
    • draw

      public Canvas draw(Canvas.Drawable image, float x, float y, float w, float h)
      Draws a scaled image at the specified location (x, y) size (w x h).
    • draw

      public Canvas draw(Canvas.Drawable image, float dx, float dy, float dw, float dh, float sx, float sy, float sw, float sh)
      Draws a subregion of a image (sw x sh) @ (sx, sy) at the specified size (dw x dh) and location (dx, dy). TODO (jgw): Document whether out-of-bounds source coordinates clamp, repeat, or do nothing.
    • drawLine

      public abstract Canvas drawLine(float x0, float y0, float x1, float y1)
      Draws a line between the two specified points.
    • drawPoint

      public abstract Canvas drawPoint(float x, float y)
      Draws a single point at the specified location.
    • drawArc

      public abstract Canvas drawArc(float cx, float cy, float r, float startAngle, float arcAngle)
      Draws an arc of a circle centered at cx, cy, with radius r.

      The start and arc angles are specified in radians. Angles are interpreted such that 0 radians is at the 3 o'clock position. Positive values indicate a counter-clockwise rotation while negative values indicate a clockwise rotation.

    • drawText

      public abstract Canvas drawText(String text, float x, float y)
      Draws text at the specified location. The text will be drawn in the current fill color.
    • fillCircle

      public abstract Canvas fillCircle(float x, float y, float radius)
      Fills a circle at the specified center and radius.
    • fillPath

      public abstract Canvas fillPath(Path path)
      Fills the specified path.
    • fillRect

      public abstract Canvas fillRect(float x, float y, float width, float height)
      Fills the specified rectangle.
    • fillRoundRect

      public abstract Canvas fillRoundRect(float x, float y, float width, float height, float radius)
      Fills the specified rounded rectangle.
      Parameters:
      x - the x coordinate of the upper left of the rounded rectangle.
      y - the y coordinate of the upper left of the rounded rectangle.
      width - the width of the rounded rectangle.
      height - the width of the rounded rectangle.
      radius - the radius of the circle to use for the corner.
    • fillText

      public abstract Canvas fillText(TextLayout text, float x, float y)
      Fills the text at the specified location. The text will use the current fill color.
    • restore

      public abstract Canvas restore()
      Restores the canvas's previous state.
      See Also:
    • rotate

      public abstract Canvas rotate(float radians)
      Rotates the current transformation matrix by the specified angle in radians.
    • save

      public abstract Canvas save()
      The save and restore methods preserve and restore the state of the canvas, but not specific paths or graphics. The following values are saved:
      • transformation matrix
      • clipping path
      • stroke color
      • stroke width
      • line cap
      • line join
      • miter limit
      • fill color or gradient
      • composite operation
    • scale

      public abstract Canvas scale(float x, float y)
      Scales the current transformation matrix by the specified amount.
    • setAlpha

      public abstract Canvas setAlpha(float alpha)
      Set the global alpha value to be used for all painting.

      Values outside the range [0,1] will be clamped to the range [0,1].

      Parameters:
      alpha - alpha value in range [0,1] where 0 is transparent and 1 is opaque
    • setCompositeOperation

      public abstract Canvas setCompositeOperation(Canvas.Composite composite)
      Sets the Porter-Duff composite operation to be used for all painting.
    • setFillColor

      public abstract Canvas setFillColor(int color)
      Sets the color to be used for fill operations. This replaces any existing fill gradient or pattern.
    • setFillGradient

      public abstract Canvas setFillGradient(Gradient gradient)
      Sets the gradient to be used for fill operations. This replaces any existing fill color or pattern.
    • setFillPattern

      public abstract Canvas setFillPattern(Pattern pattern)
      Sets the pattern to be used for fill operations. This replaces any existing fill color or gradient.
    • setLineCap

      public abstract Canvas setLineCap(Canvas.LineCap cap)
      Sets the line-cap mode for strokes.
    • setLineJoin

      public abstract Canvas setLineJoin(Canvas.LineJoin join)
      Sets the line-join mode for strokes.
    • setMiterLimit

      public abstract Canvas setMiterLimit(float miter)
      Sets the miter limit for strokes.
    • setStrokeColor

      public abstract Canvas setStrokeColor(int color)
      Sets the color for strokes.
    • setStrokeWidth

      public abstract Canvas setStrokeWidth(float strokeWidth)
      Sets the width for strokes, in pixels.
    • strokeCircle

      public abstract Canvas strokeCircle(float x, float y, float radius)
      Strokes a circle at the specified center and radius.
    • strokePath

      public abstract Canvas strokePath(Path path)
      Strokes the specified path.
    • strokeRect

      public abstract Canvas strokeRect(float x, float y, float width, float height)
      Strokes the specified rectangle.
    • strokeRoundRect

      public abstract Canvas strokeRoundRect(float x, float y, float width, float height, float radius)
      Strokes the specified rounded rectangle.
      Parameters:
      x - the x coordinate of the upper left of the rounded rectangle.
      y - the y coordinate of the upper left of the rounded rectangle.
      width - the width of the rounded rectangle.
      height - the width of the rounded rectangle.
      radius - the radius of the circle to use for the corner.
    • strokeText

      public abstract Canvas strokeText(TextLayout text, float x, float y)
      Strokes the text at the specified location. The text will use the current stroke configuration (color, width, etc.).
    • toTexture

      public Texture toTexture()
      Calls toTexture(Texture.Config) with the default texture config.
    • toTexture

      public Texture toTexture(Texture.Config config)
      A helper function for creating a texture from this canvas's image, and then disposing this canvas. This is useful for situations where you create a canvas, draw something in it, turn it into a texture and then never use the canvas again.
    • transform

      public abstract Canvas transform(float m11, float m12, float m21, float m22, float dx, float dy)
      Multiplies the current transformation matrix by the given matrix.
    • translate

      public abstract Canvas translate(float x, float y)
      Translates the current transformation matrix by the given amount.