Class GradientPaint

java.lang.Object
com.codename1.art.ArtPaint
com.codename1.art.GradientPaint

public final class GradientPaint extends ArtPaint

A linear, radial or conic gradient.

Coordinates are in the painted shape's space, or relative to its bounding box (0 to 1) when isObjectBoundingBox is true, as SVG's gradientUnits allows. getTransform is applied to the gradient before it is painted.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Colors change with the angle around the center.
    static final int
    Colors change with the distance from the center measured along both axes together, so the bands are squares turned 45 degrees: Photoshop's and Figma's diamond.
    static final int
    Colors change along the line from start to end.
    static final int
    Colors change from the focal point out to the circle.
    static final int
    Past the ends the end colors continue.
    static final int
    Past the ends the gradient repeats mirrored.
    static final int
    Past the ends the gradient repeats.
  • Constructor Summary

    Constructors
    Constructor
    Description
    GradientPaint(int type, float[] positions, int[] colors)
    Creates a gradient.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    colorAt(double x, double y, double[] bbox)
    The color at a point of paint space.
    int
    colorFor(double t)
    The color at offset t after spreading.
    conic(double cx, double cy, double startAngle, float[] positions, int[] colors)
    A conic gradient.
    A deep copy.
    diamond(double cx, double cy, double r, float[] positions, int[] colors)
    A diamond gradient.
    int[]
    Stop colors, as a copy.
    double
    Focal x of a radial gradient.
    double
    Focal y of a radial gradient.
    float[]
    Stop offsets, as a copy.
    double
    Radius of a radial gradient.
    int
    One of the SPREAD_ constants.
    The gradient transform.
    int
    The gradient type.
    double
    First x.
    double
    Second x.
    double
    First y.
    double
    Second y.
    boolean
    True when coordinates are fractions of the shape's bounding box.
    linear(double x1, double y1, double x2, double y2, float[] positions, int[] colors)
    A linear gradient.
    radial(double cx, double cy, double r, float[] positions, int[] colors)
    A radial gradient with its focus at the center.
    void
    setObjectBoundingBox(boolean objectBoundingBox)
    Sets whether coordinates are relative to the bounding box.
    void
    setPoints(double x1, double y1, double x2, double y2)
    Sets the defining points: start and end for linear, center (twice) for radial, center and a point on the start ray for conic.
    void
    setRadial(double r, double fx, double fy)
    Sets the radius and focal point of a radial gradient.
    void
    setSpread(int spread)
    Sets the spread.
    void
    setStops(float[] positions, int[] colors)
    Replaces the stops.
    void
    setTransform(Matrix transform)
    Sets the gradient transform.

    Methods inherited from class Object

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

    • LINEAR

      public static final int LINEAR
      Colors change along the line from start to end.
      See Also:
    • RADIAL

      public static final int RADIAL
      Colors change from the focal point out to the circle.
      See Also:
    • CONIC

      public static final int CONIC
      Colors change with the angle around the center.
      See Also:
    • DIAMOND

      public static final int DIAMOND
      Colors change with the distance from the center measured along both axes together, so the bands are squares turned 45 degrees: Photoshop's and Figma's diamond.
      See Also:
    • SPREAD_PAD

      public static final int SPREAD_PAD
      Past the ends the end colors continue.
      See Also:
    • SPREAD_REFLECT

      public static final int SPREAD_REFLECT
      Past the ends the gradient repeats mirrored.
      See Also:
    • SPREAD_REPEAT

      public static final int SPREAD_REPEAT
      Past the ends the gradient repeats.
      See Also:
  • Constructor Details

    • GradientPaint

      public GradientPaint(int type, float[] positions, int[] colors)

      Creates a gradient.

      Parameters
      • type: LINEAR, RADIAL, CONIC or DIAMOND
      • positions: stop offsets from 0 to 1, ascending
      • colors: one 0xAARRGGBB color per stop
  • Method Details

    • linear

      public static GradientPaint linear(double x1, double y1, double x2, double y2, float[] positions, int[] colors)

      A linear gradient.

      Parameters
      • x1: start x
      • y1: start y
      • x2: end x
      • y2: end y
      • positions: stop offsets
      • colors: stop colors
    • radial

      public static GradientPaint radial(double cx, double cy, double r, float[] positions, int[] colors)

      A radial gradient with its focus at the center.

      Parameters
      • cx: center x
      • cy: center y
      • r: radius
      • positions: stop offsets
      • colors: stop colors
    • diamond

      public static GradientPaint diamond(double cx, double cy, double r, float[] positions, int[] colors)

      A diamond gradient.

      Parameters
      • cx: center x
      • cy: center y
      • r: the distance from the center to each corner of the outermost band
      • positions: stop offsets
      • colors: stop colors
    • conic

      public static GradientPaint conic(double cx, double cy, double startAngle, float[] positions, int[] colors)

      A conic gradient.

      Parameters
      • cx: center x
      • cy: center y
      • startAngle: angle of the first stop in radians, clockwise from 3 o'clock
      • positions: stop offsets
      • colors: stop colors
    • copy

      public ArtPaint copy()
      Description copied from class: ArtPaint
      A deep copy.
      Specified by:
      copy in class ArtPaint
    • setPoints

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

      Sets the defining points: start and end for linear, center (twice) for radial, center and a point on the start ray for conic.

      Parameters
      • x1: first x
      • y1: first y
      • x2: second x
      • y2: second y
    • setRadial

      public void setRadial(double r, double fx, double fy)

      Sets the radius and focal point of a radial gradient.

      Parameters
      • r: radius
      • fx: focal x
      • fy: focal y
    • getType

      public int getType()
      The gradient type.
    • getPositions

      public float[] getPositions()
      Stop offsets, as a copy.
    • getColors

      public int[] getColors()
      Stop colors, as a copy.
    • setStops

      public void setStops(float[] positions, int[] colors)

      Replaces the stops.

      Parameters
      • positions: offsets
      • colors: colors, one per offset
    • getX1

      public double getX1()
      First x.
    • getY1

      public double getY1()
      First y.
    • getX2

      public double getX2()
      Second x.
    • getY2

      public double getY2()
      Second y.
    • getRadius

      public double getRadius()
      Radius of a radial gradient.
    • getFx

      public double getFx()
      Focal x of a radial gradient.
    • getFy

      public double getFy()
      Focal y of a radial gradient.
    • getSpread

      public int getSpread()
      One of the SPREAD_ constants.
    • setSpread

      public void setSpread(int spread)

      Sets the spread.

      Parameters
      • spread: one of the SPREAD_ constants
    • isObjectBoundingBox

      public boolean isObjectBoundingBox()
      True when coordinates are fractions of the shape's bounding box.
    • setObjectBoundingBox

      public void setObjectBoundingBox(boolean objectBoundingBox)

      Sets whether coordinates are relative to the bounding box.

      Parameters
      • objectBoundingBox: true for fractions of the bounding box
    • getTransform

      public Matrix getTransform()
      The gradient transform.
    • setTransform

      public void setTransform(Matrix transform)

      Sets the gradient transform.

      Parameters
      • transform: applied to the gradient's coordinates
    • colorAt

      public int colorAt(double x, double y, double[] bbox)
      Description copied from class: ArtPaint

      The color at a point of paint space.

      Parameters
      • x: x in the space of the shape that is painted
      • y: y in that space
      • bbox: the shape's bounding box as x, y, w, h, for paints relative to it
      Specified by:
      colorAt in class ArtPaint
    • colorFor

      public int colorFor(double t)
      The color at offset t after spreading.