Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class RasterGraphics

  1. Object
  2. RasterGraphics

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.

Fields

public static final int CAP_BUTT = 0Stroke ends stop exactly at the end points.
public static final int CAP_ROUND = 1Stroke ends are half circles.
public static final int CAP_SQUARE = 2Stroke ends extend by half the line width.
public static final int JOIN_MITER = 0Stroke corners extend to a sharp point, up to the miter limit.
public static final int JOIN_ROUND = 1Stroke corners are rounded.
public static final int JOIN_BEVEL = 2Stroke corners are cut flat.
public static final int WIND_NON_ZERO = 0A point is inside when the path winds around it a non-zero number of times.
public static final int WIND_EVEN_ODD = 1A point is inside when the path crosses a ray from it an odd number of times.

Constructors

public RasterGraphics(RasterImage target)Creates a graphics that draws into target; also available as RasterImage.createGraphics().

Methods

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

Inherited methods

Field details

CAP_BUTT

public static final int CAP_BUTT = 0
Stroke ends stop exactly at the end points.

CAP_ROUND

public static final int CAP_ROUND = 1
Stroke ends are half circles.

CAP_SQUARE

public static final int CAP_SQUARE = 2
Stroke ends extend by half the line width.

JOIN_MITER

public static final int JOIN_MITER = 0
Stroke corners extend to a sharp point, up to the miter limit.

JOIN_ROUND

public static final int JOIN_ROUND = 1
Stroke corners are rounded.

JOIN_BEVEL

public static final int JOIN_BEVEL = 2
Stroke corners are cut flat.

WIND_NON_ZERO

public static final int WIND_NON_ZERO = 0
A point is inside when the path winds around it a non-zero number of times.

WIND_EVEN_ODD

public static final int WIND_EVEN_ODD = 1
A point is inside when the path crosses a ray from it an odd number of times.

Constructor details

RasterGraphics

public RasterGraphics(RasterImage target)
Creates a graphics that draws into target; also available as RasterImage.createGraphics().

Parameters

target RasterImage
the image to draw into

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 int
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 RasterPaint
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 float
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 double
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 int
CAP_BUTT, CAP_ROUND or CAP_SQUARE

setLineJoin

public void setLineJoin(int join)
Sets how line corners are drawn.

Parameters

join int
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 double
at least 1

setWindingRule

public void setWindingRule(int rule)
Sets the rule deciding what is inside a self-intersecting path.

Parameters

rule int
WIND_NON_ZERO or WIND_EVEN_ODD

setAntiAlias

public void setAntiAlias(boolean aa)
Turns antialiasing on (the default) or off.

Parameters

aa boolean
true for smooth edges

setFont

public void setFont(RasterFont font)

Parameters

font RasterFont
the font

getFont

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

translate

public void translate(double dx, double dy)
Moves the origin.

Parameters

dx double
horizontal offset
dy double
vertical offset

scale

public void scale(double sx, double sy)
Scales user space.

Parameters

sx double
horizontal factor
sy double
vertical factor

rotate

public void rotate(double radians)
Rotates user space about the origin.

Parameters

radians double
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 double
x scale
b double
y shear
c double
x shear
d double
y scale
e double
x translation
f double
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 double
left
y double
top
w double
width
h double
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 double
left
y double
top
w double
width
h double
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 int
the color

fillRect

public void fillRect(double x, double y, double w, double h)
Fills a rectangle.

Parameters

x double
left
y double
top
w double
width
h double
height

drawRect

public void drawRect(double x, double y, double w, double h)
Strokes a rectangle’s outline.

Parameters

x double
left
y double
top
w double
width
h double
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 double
left
y double
top
w double
width
h double
height
arcW double
horizontal corner diameter
arcH double
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 double
left
y double
top
w double
width
h double
height
arcW double
horizontal corner diameter
arcH double
vertical corner diameter

fillOval

public void fillOval(double x, double y, double w, double h)
Fills the ellipse inscribed in a rectangle.

Parameters

x double
left
y double
top
w double
width
h double
height

drawOval

public void drawOval(double x, double y, double w, double h)
Strokes the ellipse inscribed in a rectangle.

Parameters

x double
left
y double
top
w double
width
h double
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 double
left
y double
top
w double
width
h double
height
startDeg double
start angle, degrees counterclockwise from three o’clock
extentDeg double
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 double
left
y double
top
w double
width
h double
height
startDeg double
start angle, degrees counterclockwise from three o’clock
extentDeg double
extent in degrees

drawLine

public void drawLine(double x1, double y1, double x2, double y2)
Strokes a straight line.

Parameters

x1 double
start x
y1 double
start y
x2 double
end x
y2 double
end y

fillPolygon

public void fillPolygon(double[] xs, double[] ys, int n)
Fills a polygon.

Parameters

xs double[]
vertex x values
ys double[]
vertex y values
n int
vertex count

drawPolygon

public void drawPolygon(double[] xs, double[] ys, int n)
Strokes a closed polygon.

Parameters

xs double[]
vertex x values
ys double[]
vertex y values
n int
vertex count

drawPolyline

public void drawPolyline(double[] xs, double[] ys, int n)
Strokes an open chain of lines.

Parameters

xs double[]
vertex x values
ys double[]
vertex y values
n int
vertex count

drawString

public void drawString(String text, double x, double y)
Draws text with the font set by setFont(RasterFont).

Parameters

text String
the string
x double
left end of the baseline
y double
the baseline

drawImage

public void drawImage(RasterImage img, double x, double y)
Draws an image at its own size.

Parameters

img RasterImage
the image
x double
left
y double
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 RasterImage
the image
x double
left
y double
top
w double
width
h double
height

fill

public void fill(RasterPath path)
Fills a path with the current color or paint and winding rule.

Parameters

path RasterPath
the shape

draw

public void draw(RasterPath path)
Strokes a path with the current line width, caps and joins.

Parameters

path RasterPath
the shape