Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class RasterGraphics
- Object
- 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.
- Coordinates are user space, mapped to pixels by an affine transform
(
translate(double, double),scale(double, double),rotate(double),setTransform(double, double, double, double, double, double)). Pixel(x, y)covers the square from(x, y)to(x + 1, y + 1), so a one pixel wide line alongy = 10.5covers row 10 exactly. - Painting is source-over compositing of non-premultiplied colors, with
setColor(int)or aRasterPaintsuch asRasterGradient, scaled bysetAlpha(float). - Coverage is computed on eight sub-scanlines per pixel with exact
horizontal area;
setAntiAlias(boolean)turns it off. - Clipping is a rectangle in pixels:
clipRect(double, double, double, double)intersects the bounds of a transformed rectangle with it. - Text goes through the
RasterFontseam set withsetFont(RasterFont).
A graphics object is not thread safe; create one per thread.
Fields
public static final int CAP_BUTT = 0 | Stroke ends stop exactly at the end points. |
public static final int CAP_ROUND = 1 | Stroke ends are half circles. |
public static final int CAP_SQUARE = 2 | Stroke ends extend by half the line width. |
public static final int JOIN_MITER = 0 | Stroke corners extend to a sharp point, up to the miter limit. |
public static final int JOIN_ROUND = 1 | Stroke corners are rounded. |
public static final int JOIN_BEVEL = 2 | Stroke corners are cut flat. |
public static final int WIND_NON_ZERO = 0 | A point is inside when the path winds around it a non-zero number of times. |
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. |
Constructors
public RasterGraphics(RasterImage target) | Creates a graphics that draws into target; also available as RasterImage.createGraphics(). |
Methods
Inherited methods
Field details
CAP_BUTT
public static final int CAP_BUTT = 0Stroke ends stop exactly at the end points.
CAP_ROUND
public static final int CAP_ROUND = 1Stroke ends are half circles.
CAP_SQUARE
public static final int CAP_SQUARE = 2Stroke ends extend by half the line width.
JOIN_MITER
public static final int JOIN_MITER = 0Stroke corners extend to a sharp point, up to the miter limit.
JOIN_ROUND
public static final int JOIN_ROUND = 1Stroke corners are rounded.
JOIN_BEVEL
public static final int JOIN_BEVEL = 2Stroke corners are cut flat.
WIND_NON_ZERO
public static final int WIND_NON_ZERO = 0A 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 = 1A 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
targetRasterImage- 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
argbint- 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
paintRasterPaint- 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
alphafloat- 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
wdouble- the width
getStrokeWidth
public double getStrokeWidth()Returns the line width.
setLineCap
public void setLineCap(int cap)Sets how open line ends are drawn.
Parameters
capintCAP_BUTT,CAP_ROUNDorCAP_SQUARE
setLineJoin
public void setLineJoin(int join)Sets how line corners are drawn.
Parameters
joinintJOIN_MITER,JOIN_ROUNDorJOIN_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
limitdouble- at least 1
setWindingRule
public void setWindingRule(int rule)Sets the rule deciding what is inside a self-intersecting path.
Parameters
ruleintWIND_NON_ZEROorWIND_EVEN_ODD
setAntiAlias
public void setAntiAlias(boolean aa)Turns antialiasing on (the default) or off.
Parameters
aaboolean- true for smooth edges
setFont
public void setFont(RasterFont font)Sets the font
drawString(String, double, double) uses.Parameters
fontRasterFont- the font
getFont
public RasterFont getFont()Returns the font, or null.
translate
public void translate(double dx, double dy)Moves the origin.
Parameters
dxdouble- horizontal offset
dydouble- vertical offset
scale
public void scale(double sx, double sy)Scales user space.
Parameters
sxdouble- horizontal factor
sydouble- vertical factor
rotate
public void rotate(double radians)Rotates user space about the origin.
Parameters
radiansdouble- 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
adouble- x scale
bdouble- y shear
cdouble- x shear
ddouble- y scale
edouble- x translation
fdouble- 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
xdouble- left
ydouble- top
wdouble- width
hdouble- 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
xdouble- left
ydouble- top
wdouble- width
hdouble- 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
argbint- the color
fillRect
public void fillRect(double x, double y, double w, double h)Fills a rectangle.
Parameters
xdouble- left
ydouble- top
wdouble- width
hdouble- height
drawRect
public void drawRect(double x, double y, double w, double h)Strokes a rectangle’s outline.
Parameters
xdouble- left
ydouble- top
wdouble- width
hdouble- height
fillRoundRect
public void fillRoundRect(double x, double y, double w, double h, double arcW, double arcH)Fills a rectangle with elliptical corners.
Parameters
xdouble- left
ydouble- top
wdouble- width
hdouble- height
arcWdouble- horizontal corner diameter
arcHdouble- 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
xdouble- left
ydouble- top
wdouble- width
hdouble- height
arcWdouble- horizontal corner diameter
arcHdouble- vertical corner diameter
fillOval
public void fillOval(double x, double y, double w, double h)Fills the ellipse inscribed in a rectangle.
Parameters
xdouble- left
ydouble- top
wdouble- width
hdouble- height
drawOval
public void drawOval(double x, double y, double w, double h)Strokes the ellipse inscribed in a rectangle.
Parameters
xdouble- left
ydouble- top
wdouble- width
hdouble- 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
xdouble- left
ydouble- top
wdouble- width
hdouble- height
startDegdouble- start angle, degrees counterclockwise from three o’clock
extentDegdouble- 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
xdouble- left
ydouble- top
wdouble- width
hdouble- height
startDegdouble- start angle, degrees counterclockwise from three o’clock
extentDegdouble- extent in degrees
drawLine
public void drawLine(double x1, double y1, double x2, double y2)Strokes a straight line.
Parameters
x1double- start x
y1double- start y
x2double- end x
y2double- end y
fillPolygon
public void fillPolygon(double[] xs, double[] ys, int n)Fills a polygon.
Parameters
xsdouble[]- vertex x values
ysdouble[]- vertex y values
nint- vertex count
drawPolygon
public void drawPolygon(double[] xs, double[] ys, int n)Strokes a closed polygon.
Parameters
xsdouble[]- vertex x values
ysdouble[]- vertex y values
nint- vertex count
drawPolyline
public void drawPolyline(double[] xs, double[] ys, int n)Strokes an open chain of lines.
Parameters
xsdouble[]- vertex x values
ysdouble[]- vertex y values
nint- vertex count
drawString
public void drawString(String text, double x, double y)Draws text with the font set by
setFont(RasterFont).Parameters
textString- the string
xdouble- left end of the baseline
ydouble- the baseline
drawImage
public void drawImage(RasterImage img, double x, double y)Draws an image at its own size.
Parameters
imgRasterImage- the image
xdouble- left
ydouble- 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
imgRasterImage- the image
xdouble- left
ydouble- top
wdouble- width
hdouble- height
fill
public void fill(RasterPath path)Fills a path with the current color or paint and winding rule.
Parameters
pathRasterPath- the shape
draw
public void draw(RasterPath path)Strokes a path with the current line width, caps and joins.
Parameters
pathRasterPath- the shape