Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class GradientPaint

  1. Object
  2. ArtPaint
  3. GradientPaint

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.

Fields

public static final int LINEAR = 0Colors change along the line from start to end.
public static final int RADIAL = 1Colors change from the focal point out to the circle.
public static final int CONIC = 2Colors change with the angle around the center.
public static final int DIAMOND = 3Colors 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.
public static final int SPREAD_PAD = 0Past the ends the end colors continue.
public static final int SPREAD_REFLECT = 1Past the ends the gradient repeats mirrored.
public static final int SPREAD_REPEAT = 2Past the ends the gradient repeats.

Constructors

public GradientPaint(int type, float[] positions, int[] colors)Creates a gradient.

Methods

public static GradientPaint linear(double x1, double y1, double x2, double y2, float[] positions, int[] colors)A linear gradient.
public static GradientPaint radial(double cx, double cy, double r, float[] positions, int[] colors)A radial gradient with its focus at the center.
public static GradientPaint diamond(double cx, double cy, double r, float[] positions, int[] colors)A diamond gradient.
public static GradientPaint conic(double cx, double cy, double startAngle, float[] positions, int[] colors)A conic gradient.
public ArtPaint copy()A deep copy.
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.
public void setRadial(double r, double fx, double fy)Sets the radius and focal point of a radial gradient.
public int getType()The gradient type.
public float[] getPositions()Stop offsets, as a copy.
public int[] getColors()Stop colors, as a copy.
public void setStops(float[] positions, int[] colors)Replaces the stops.
public double getX1()First x.
public double getY1()First y.
public double getX2()Second x.
public double getY2()Second y.
public double getRadius()Radius of a radial gradient.
public double getFx()Focal x of a radial gradient.
public double getFy()Focal y of a radial gradient.
public int getSpread()One of the SPREAD_ constants.
public void setSpread(int spread)Sets the spread.
public boolean isObjectBoundingBox()True when coordinates are fractions of the shape’s bounding box.
public void setObjectBoundingBox(boolean objectBoundingBox)Sets whether coordinates are relative to the bounding box.
public Matrix getTransform()The gradient transform.
public void setTransform(Matrix transform)Sets the gradient transform.
public int colorAt(double x, double y, double[] bbox)The color at a point of paint space.
public int colorFor(double t)The color at offset t after spreading.

Inherited methods

Field details

LINEAR

public static final int LINEAR = 0
Colors change along the line from start to end.

RADIAL

public static final int RADIAL = 1
Colors change from the focal point out to the circle.

CONIC

public static final int CONIC = 2
Colors change with the angle around the center.

DIAMOND

public static final int DIAMOND = 3
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.

SPREAD_PAD

public static final int SPREAD_PAD = 0
Past the ends the end colors continue.

SPREAD_REFLECT

public static final int SPREAD_REFLECT = 1
Past the ends the gradient repeats mirrored.

SPREAD_REPEAT

public static final int SPREAD_REPEAT = 2
Past the ends the gradient repeats.

Constructor details

GradientPaint

public GradientPaint(int type, float[] positions, int[] colors)
Creates a gradient.

Parameters

type int
LINEAR, RADIAL, CONIC or DIAMOND
positions float[]
stop offsets from 0 to 1, ascending
colors int[]
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 double
start x
y1 double
start y
x2 double
end x
y2 double
end y
positions float[]
stop offsets
colors int[]
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 double
center x
cy double
center y
r double
radius
positions float[]
stop offsets
colors int[]
stop colors

diamond

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

Parameters

cx double
center x
cy double
center y
r double
the distance from the center to each corner of the outermost band
positions float[]
stop offsets
colors int[]
stop colors

conic

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

Parameters

cx double
center x
cy double
center y
startAngle double
angle of the first stop in radians, clockwise from 3 o’clock
positions float[]
stop offsets
colors int[]
stop colors

copy

public ArtPaint copy()
A deep copy.

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 double
first x
y1 double
first y
x2 double
second x
y2 double
second y

setRadial

public void setRadial(double r, double fx, double fy)
Sets the radius and focal point of a radial gradient.

Parameters

r double
radius
fx double
focal x
fy double
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 float[]
offsets
colors int[]
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 int
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 boolean
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 Matrix
applied to the gradient’s coordinates

colorAt

public int colorAt(double x, double y, double[] bbox)
The color at a point of paint space.

Parameters

x double
x in the space of the shape that is painted
y double
y in that space
bbox double[]
the shape’s bounding box as x, y, w, h, for paints relative to it

colorFor

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