Class 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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intStroke ends stop exactly at the end points.static final intStroke ends are half circles.static final intStroke ends extend by half the line width.static final intStroke corners are cut flat.static final intStroke corners extend to a sharp point, up to the miter limit.static final intStroke corners are rounded.static final intA point is inside when the path crosses a ray from it an odd number of times.static final intA point is inside when the path winds around it a non-zero number of times. -
Constructor Summary
ConstructorsConstructorDescriptionRasterGraphics(RasterImage target) Creates a graphics that draws intotarget; also available asRasterImage.createGraphics(). -
Method Summary
Modifier and TypeMethodDescriptionvoidclear(int argb) Replaces every pixel inside the clip with a color, without blending.voidclipRect(double x, double y, double w, double h) Intersects the clip with the pixel bounds of a transformed rectangle.voiddraw(RasterPath path) Strokes a path with the current line width, caps and joins.voiddrawArc(double x, double y, double w, double h, double startDeg, double extentDeg) Strokes an open arc of the ellipse inscribed in a rectangle.voiddrawImage(RasterImage img, double x, double y) Draws an image at its own size.voiddrawImage(RasterImage img, double x, double y, double w, double h) Draws an image scaled to a rectangle, sampled bilinearly through the transform.voiddrawLine(double x1, double y1, double x2, double y2) Strokes a straight line.voiddrawOval(double x, double y, double w, double h) Strokes the ellipse inscribed in a rectangle.voiddrawPolygon(double[] xs, double[] ys, int n) Strokes a closed polygon.voiddrawPolyline(double[] xs, double[] ys, int n) Strokes an open chain of lines.voiddrawRect(double x, double y, double w, double h) Strokes a rectangle's outline.voiddrawRoundRect(double x, double y, double w, double h, double arcW, double arcH) Strokes a rectangle with elliptical corners.voiddrawString(String text, double x, double y) Draws text with the font set bysetFont(RasterFont).voidfill(RasterPath path) Fills a path with the current color or paint and winding rule.voidfillArc(double x, double y, double w, double h, double startDeg, double extentDeg) Fills a pie slice of the ellipse inscribed in a rectangle.voidfillOval(double x, double y, double w, double h) Fills the ellipse inscribed in a rectangle.voidfillPolygon(double[] xs, double[] ys, int n) Fills a polygon.voidfillRect(double x, double y, double w, double h) Fills a rectangle.voidfillRoundRect(double x, double y, double w, double h, double arcW, double arcH) Fills a rectangle with elliptical corners.floatgetAlpha()Returns the opacity set bysetAlpha(float).int[]Returns the clip as pixel bounds{x0, y0, x1, y1}, right and bottom exclusive.intgetColor()Returns the solid color.getFont()Returns the font, or null.getPaint()Returns the current paint, or null when drawing with the solid color.doubleReturns the line width.Returns the image this graphics draws into.double[]Returns the transform as{a, b, c, d, e, f}.voidRemoves the clip, so the whole image can be drawn.voidRestores the identity transform.voidrotate(double radians) Rotates user space about the origin.voidscale(double sx, double sy) Scales user space.voidsetAlpha(float alpha) Sets an opacity that multiplies everything drawn.voidsetAntiAlias(boolean aa) Turns antialiasing on (the default) or off.voidsetClip(double x, double y, double w, double h) Replaces the clip with the pixel bounds of a transformed rectangle.voidsetColor(int argb) Sets a solid ARGB color and clears any paint.voidsetFont(RasterFont font) Sets the fontdrawString(String, double, double)uses.voidsetLineCap(int cap) Sets how open line ends are drawn.voidsetLineJoin(int join) Sets how line corners are drawn.voidsetMiterLimit(double limit) Sets the longest a miter may be, as a multiple of half the line width; sharper corners are beveled.voidsetPaint(RasterPaint paint) Sets the paint used instead of the solid color; null returns to the color.voidsetStrokeWidth(double w) Sets the line width used by the draw methods, in user-space units.voidsetTransform(double a, double b, double c, double d, double e, double f) Replaces the transform: devicex = a * x + c * y + e,y = b * x + d * y + f.voidsetWindingRule(int rule) Sets the rule deciding what is inside a self-intersecting path.voidtranslate(double dx, double dy) Moves the origin.
-
Field Details
-
CAP_BUTT
public static final int CAP_BUTTStroke ends stop exactly at the end points.- See Also:
-
CAP_ROUND
public static final int CAP_ROUNDStroke ends are half circles.- See Also:
-
CAP_SQUARE
public static final int CAP_SQUAREStroke ends extend by half the line width.- See Also:
-
JOIN_MITER
public static final int JOIN_MITERStroke corners extend to a sharp point, up to the miter limit.- See Also:
-
JOIN_ROUND
public static final int JOIN_ROUNDStroke corners are rounded.- See Also:
-
JOIN_BEVEL
public static final int JOIN_BEVELStroke corners are cut flat.- See Also:
-
WIND_NON_ZERO
public static final int WIND_NON_ZEROA 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_ODDA point is inside when the path crosses a ray from it an odd number of times.- See Also:
-
-
Constructor Details
-
RasterGraphics
Creates a graphics that draws into
target; also available asRasterImage.createGraphics().Parameters
target: the image to draw into
-
-
Method Details
-
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
Sets the paint used instead of the solid color; null returns to the color.
Parameters
paint: a gradient or pattern
-
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 bysetAlpha(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_ROUNDorCAP_SQUARE
-
setLineJoin
public void setLineJoin(int join) Sets how line corners are drawn.
Parameters
join:JOIN_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
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_ZEROorWIND_EVEN_ODD
-
setAntiAlias
public void setAntiAlias(boolean aa) Turns antialiasing on (the default) or off.
Parameters
aa: true for smooth edges
-
setFont
-
getFont
Returns the font, or null. -
translate
public void translate(double dx, double dy) Moves the origin.
Parameters
dx: horizontal offsetdy: vertical offset
-
scale
public void scale(double sx, double sy) Scales user space.
Parameters
sx: horizontal factorsy: 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 scaleb: y shearc: x sheard: y scalee: x translationf: 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: lefty: topw: widthh: 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: lefty: topw: widthh: 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: lefty: topw: widthh: height
-
drawRect
public void drawRect(double x, double y, double w, double h) Strokes a rectangle's outline.
Parameters
x: lefty: topw: widthh: 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: lefty: topw: widthh: heightarcW: horizontal corner diameterarcH: 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: lefty: topw: widthh: heightarcW: horizontal corner diameterarcH: vertical corner diameter
-
fillOval
public void fillOval(double x, double y, double w, double h) Fills the ellipse inscribed in a rectangle.
Parameters
x: lefty: topw: widthh: height
-
drawOval
public void drawOval(double x, double y, double w, double h) Strokes the ellipse inscribed in a rectangle.
Parameters
x: lefty: topw: widthh: 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: lefty: topw: widthh: heightstartDeg: start angle, degrees counterclockwise from three o'clockextentDeg: 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: lefty: topw: widthh: heightstartDeg: start angle, degrees counterclockwise from three o'clockextentDeg: extent in degrees
-
drawLine
public void drawLine(double x1, double y1, double x2, double y2) Strokes a straight line.
Parameters
x1: start xy1: start yx2: end xy2: end y
-
fillPolygon
public void fillPolygon(double[] xs, double[] ys, int n) Fills a polygon.
Parameters
xs: vertex x valuesys: vertex y valuesn: vertex count
-
drawPolygon
public void drawPolygon(double[] xs, double[] ys, int n) Strokes a closed polygon.
Parameters
xs: vertex x valuesys: vertex y valuesn: vertex count
-
drawPolyline
public void drawPolyline(double[] xs, double[] ys, int n) Strokes an open chain of lines.
Parameters
xs: vertex x valuesys: vertex y valuesn: vertex count
-
drawString
Draws text with the font set by
setFont(RasterFont).Parameters
text: the stringx: left end of the baseliney: the baseline
-
drawImage
Draws an image at its own size.
Parameters
img: the imagex: lefty: top
-
drawImage
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 imagex: lefty: topw: widthh: height
-
fill
Fills a path with the current color or paint and winding rule.
Parameters
path: the shape
-
draw
Strokes a path with the current line width, caps and joins.
Parameters
path: the shape
-