Class RasterGraphics

java.lang.Object
com.codename1.backend.image.RasterGraphics

public final class RasterGraphics extends Object

Draws antialiased shapes, gradients and images into a RasterImage.

Everything is pure Java, so the same drawing code runs on a device, in the simulator and on a Codename One server that generates images (charts, thumbnails, social cards) with no display at all.

A graphics object is not thread safe; create one per thread.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Stroke ends stop exactly at the end points.
    static final int
    Stroke ends are half circles.
    static final int
    Stroke ends extend by half the line width.
    static final int
    Stroke corners are cut flat.
    static final int
    Stroke corners extend to a sharp point, up to the miter limit.
    static final int
    Stroke corners are rounded.
    static final int
    A point is inside when the path crosses a ray from it an odd number of times.
    static final int
    A point is inside when the path winds around it a non-zero number of times.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a graphics that draws into target; also available as RasterImage.createGraphics().
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    clear(int argb)
    Replaces every pixel inside the clip with a color, without blending.
    void
    clipRect(double x, double y, double w, double h)
    Intersects the clip with the pixel bounds of a transformed rectangle.
    void
    Strokes a path with the current line width, caps and joins.
    void
    drawArc(double x, double y, double w, double h, double startDeg, double extentDeg)
    Strokes an open arc of the ellipse inscribed in a rectangle.
    void
    drawImage(RasterImage img, double x, double y)
    Draws an image at its own size.
    void
    drawImage(RasterImage img, double x, double y, double w, double h)
    Draws an image scaled to a rectangle, sampled bilinearly through the transform.
    void
    drawLine(double x1, double y1, double x2, double y2)
    Strokes a straight line.
    void
    drawOval(double x, double y, double w, double h)
    Strokes the ellipse inscribed in a rectangle.
    void
    drawPolygon(double[] xs, double[] ys, int n)
    Strokes a closed polygon.
    void
    drawPolyline(double[] xs, double[] ys, int n)
    Strokes an open chain of lines.
    void
    drawRect(double x, double y, double w, double h)
    Strokes a rectangle's outline.
    void
    drawRoundRect(double x, double y, double w, double h, double arcW, double arcH)
    Strokes a rectangle with elliptical corners.
    void
    drawString(String text, double x, double y)
    Draws text with the font set by setFont(RasterFont).
    void
    Fills a path with the current color or paint and winding rule.
    void
    fillArc(double x, double y, double w, double h, double startDeg, double extentDeg)
    Fills a pie slice of the ellipse inscribed in a rectangle.
    void
    fillOval(double x, double y, double w, double h)
    Fills the ellipse inscribed in a rectangle.
    void
    fillPolygon(double[] xs, double[] ys, int n)
    Fills a polygon.
    void
    fillRect(double x, double y, double w, double h)
    Fills a rectangle.
    void
    fillRoundRect(double x, double y, double w, double h, double arcW, double arcH)
    Fills a rectangle with elliptical corners.
    float
    Returns the opacity set by setAlpha(float).
    int[]
    Returns the clip as pixel bounds {x0, y0, x1, y1}, right and bottom exclusive.
    int
    Returns the solid color.
    Returns the font, or null.
    Returns the current paint, or null when drawing with the solid color.
    double
    Returns the line width.
    Returns the image this graphics draws into.
    double[]
    Returns the transform as {a, b, c, d, e, f}.
    void
    Removes the clip, so the whole image can be drawn.
    void
    Restores the identity transform.
    void
    rotate(double radians)
    Rotates user space about the origin.
    void
    scale(double sx, double sy)
    Scales user space.
    void
    setAlpha(float alpha)
    Sets an opacity that multiplies everything drawn.
    void
    setAntiAlias(boolean aa)
    Turns antialiasing on (the default) or off.
    void
    setClip(double x, double y, double w, double h)
    Replaces the clip with the pixel bounds of a transformed rectangle.
    void
    setColor(int argb)
    Sets a solid ARGB color and clears any paint.
    void
    void
    setLineCap(int cap)
    Sets how open line ends are drawn.
    void
    setLineJoin(int join)
    Sets how line corners are drawn.
    void
    setMiterLimit(double limit)
    Sets the longest a miter may be, as a multiple of half the line width; sharper corners are beveled.
    void
    Sets the paint used instead of the solid color; null returns to the color.
    void
    setStrokeWidth(double w)
    Sets the line width used by the draw methods, in user-space units.
    void
    setTransform(double a, double b, double c, double d, double e, double f)
    Replaces the transform: device x = a * x + c * y + e, y = b * x + d * y + f.
    void
    setWindingRule(int rule)
    Sets the rule deciding what is inside a self-intersecting path.
    void
    translate(double dx, double dy)
    Moves the origin.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • CAP_BUTT

      public static final int CAP_BUTT
      Stroke ends stop exactly at the end points.
      See Also:
    • CAP_ROUND

      public static final int CAP_ROUND
      Stroke ends are half circles.
      See Also:
    • CAP_SQUARE

      public static final int CAP_SQUARE
      Stroke ends extend by half the line width.
      See Also:
    • JOIN_MITER

      public static final int JOIN_MITER
      Stroke corners extend to a sharp point, up to the miter limit.
      See Also:
    • JOIN_ROUND

      public static final int JOIN_ROUND
      Stroke corners are rounded.
      See Also:
    • JOIN_BEVEL

      public static final int JOIN_BEVEL
      Stroke corners are cut flat.
      See Also:
    • WIND_NON_ZERO

      public static final int WIND_NON_ZERO
      A point is inside when the path winds around it a non-zero number of times.
      See Also:
    • WIND_EVEN_ODD

      public static final int WIND_EVEN_ODD
      A point is inside when the path crosses a ray from it an odd number of times.
      See Also:
  • Constructor Details

  • Method Details

    • getTarget

      public RasterImage getTarget()
      Returns the image this graphics draws into.
    • setColor

      public void setColor(int argb)

      Sets a solid ARGB color and clears any paint.

      Parameters
      • argb: the color, not premultiplied
    • getColor

      public int getColor()
      Returns the solid color.
    • setPaint

      public void setPaint(RasterPaint paint)

      Sets the paint used instead of the solid color; null returns to the color.

      Parameters
      • paint: a gradient or pattern
    • getPaint

      public RasterPaint getPaint()
      Returns the current paint, or null when drawing with the solid color.
    • setAlpha

      public void setAlpha(float alpha)

      Sets an opacity that multiplies everything drawn.

      Parameters
      • alpha: 0 (invisible) to 1 (opaque)
    • getAlpha

      public float getAlpha()
      Returns the opacity set by setAlpha(float).
    • setStrokeWidth

      public void setStrokeWidth(double w)

      Sets the line width used by the draw methods, in user-space units.

      Parameters
      • w: the width
    • getStrokeWidth

      public double getStrokeWidth()
      Returns the line width.
    • setLineCap

      public void setLineCap(int cap)

      Sets how open line ends are drawn.

      Parameters
      • cap: CAP_BUTT, CAP_ROUND or CAP_SQUARE
    • setLineJoin

      public void setLineJoin(int join)

      Sets how line corners are drawn.

      Parameters
      • join: JOIN_MITER, JOIN_ROUND or JOIN_BEVEL
    • setMiterLimit

      public void setMiterLimit(double limit)

      Sets the longest a miter may be, as a multiple of half the line width; sharper corners are beveled.

      Parameters
      • limit: at least 1
    • setWindingRule

      public void setWindingRule(int rule)

      Sets the rule deciding what is inside a self-intersecting path.

      Parameters
      • rule: WIND_NON_ZERO or WIND_EVEN_ODD
    • setAntiAlias

      public void setAntiAlias(boolean aa)

      Turns antialiasing on (the default) or off.

      Parameters
      • aa: true for smooth edges
    • setFont

      public void setFont(RasterFont font)

      Sets the font drawString(String, double, double) uses.

      Parameters
      • font: the font
    • getFont

      public RasterFont getFont()
      Returns the font, or null.
    • translate

      public void translate(double dx, double dy)

      Moves the origin.

      Parameters
      • dx: horizontal offset
      • dy: vertical offset
    • scale

      public void scale(double sx, double sy)

      Scales user space.

      Parameters
      • sx: horizontal factor
      • sy: vertical factor
    • rotate

      public void rotate(double radians)

      Rotates user space about the origin.

      Parameters
      • radians: the angle; positive turns x toward y (clockwise on screen)
    • setTransform

      public void setTransform(double a, double b, double c, double d, double e, double f)

      Replaces the transform: device x = a * x + c * y + e, y = b * x + d * y + f.

      Parameters
      • a: x scale
      • b: y shear
      • c: x shear
      • d: y scale
      • e: x translation
      • f: y translation
    • getTransform

      public double[] getTransform()
      Returns the transform as {a, b, c, d, e, f}.
    • resetTransform

      public void resetTransform()
      Restores the identity transform.
    • resetClip

      public void resetClip()
      Removes the clip, so the whole image can be drawn.
    • clipRect

      public void clipRect(double x, double y, double w, double h)

      Intersects the clip with the pixel bounds of a transformed rectangle.

      Parameters
      • x: left
      • y: top
      • w: width
      • h: height
    • setClip

      public void setClip(double x, double y, double w, double h)

      Replaces the clip with the pixel bounds of a transformed rectangle.

      Parameters
      • x: left
      • y: top
      • w: width
      • h: height
    • getClipBounds

      public int[] getClipBounds()
      Returns the clip as pixel bounds {x0, y0, x1, y1}, right and bottom exclusive.
    • clear

      public void clear(int argb)

      Replaces every pixel inside the clip with a color, without blending.

      Parameters
      • argb: the color
    • fillRect

      public void fillRect(double x, double y, double w, double h)

      Fills a rectangle.

      Parameters
      • x: left
      • y: top
      • w: width
      • h: height
    • drawRect

      public void drawRect(double x, double y, double w, double h)

      Strokes a rectangle's outline.

      Parameters
      • x: left
      • y: top
      • w: width
      • h: height
    • fillRoundRect

      public void fillRoundRect(double x, double y, double w, double h, double arcW, double arcH)

      Fills a rectangle with elliptical corners.

      Parameters
      • x: left
      • y: top
      • w: width
      • h: height
      • arcW: horizontal corner diameter
      • arcH: vertical corner diameter
    • drawRoundRect

      public void drawRoundRect(double x, double y, double w, double h, double arcW, double arcH)

      Strokes a rectangle with elliptical corners.

      Parameters
      • x: left
      • y: top
      • w: width
      • h: height
      • arcW: horizontal corner diameter
      • arcH: vertical corner diameter
    • fillOval

      public void fillOval(double x, double y, double w, double h)

      Fills the ellipse inscribed in a rectangle.

      Parameters
      • x: left
      • y: top
      • w: width
      • h: height
    • drawOval

      public void drawOval(double x, double y, double w, double h)

      Strokes the ellipse inscribed in a rectangle.

      Parameters
      • x: left
      • y: top
      • w: width
      • h: height
    • fillArc

      public void fillArc(double x, double y, double w, double h, double startDeg, double extentDeg)

      Fills a pie slice of the ellipse inscribed in a rectangle.

      Parameters
      • x: left
      • y: top
      • w: width
      • h: height
      • startDeg: start angle, degrees counterclockwise from three o'clock
      • extentDeg: extent in degrees
    • drawArc

      public void drawArc(double x, double y, double w, double h, double startDeg, double extentDeg)

      Strokes an open arc of the ellipse inscribed in a rectangle.

      Parameters
      • x: left
      • y: top
      • w: width
      • h: height
      • startDeg: start angle, degrees counterclockwise from three o'clock
      • extentDeg: extent in degrees
    • drawLine

      public void drawLine(double x1, double y1, double x2, double y2)

      Strokes a straight line.

      Parameters
      • x1: start x
      • y1: start y
      • x2: end x
      • y2: end y
    • fillPolygon

      public void fillPolygon(double[] xs, double[] ys, int n)

      Fills a polygon.

      Parameters
      • xs: vertex x values
      • ys: vertex y values
      • n: vertex count
    • drawPolygon

      public void drawPolygon(double[] xs, double[] ys, int n)

      Strokes a closed polygon.

      Parameters
      • xs: vertex x values
      • ys: vertex y values
      • n: vertex count
    • drawPolyline

      public void drawPolyline(double[] xs, double[] ys, int n)

      Strokes an open chain of lines.

      Parameters
      • xs: vertex x values
      • ys: vertex y values
      • n: vertex count
    • drawString

      public void drawString(String text, double x, double y)

      Draws text with the font set by setFont(RasterFont).

      Parameters
      • text: the string
      • x: left end of the baseline
      • y: the baseline
    • drawImage

      public void drawImage(RasterImage img, double x, double y)

      Draws an image at its own size.

      Parameters
      • img: the image
      • x: left
      • y: top
    • drawImage

      public void drawImage(RasterImage img, double x, double y, double w, double h)

      Draws an image scaled to a rectangle, sampled bilinearly through the transform. For large reductions scale it first with RasterImage.scale(int, int, int), which filters every source pixel.

      Parameters
      • img: the image
      • x: left
      • y: top
      • w: width
      • h: height
    • fill

      public void fill(RasterPath path)

      Fills a path with the current color or paint and winding rule.

      Parameters
      • path: the shape
    • draw

      public void draw(RasterPath path)

      Strokes a path with the current line width, caps and joins.

      Parameters
      • path: the shape