Class WPainterPath
A painter path represents a (complex) path that may be composed of lines, arcs and bezier
curve segments, and painted onto a paint device using WPainter#drawPath().
The path that is composed in a painter path may consist of multiple closed sub-paths. Only the last sub-path can be left open.
To compose a path, this class maintains a current position, which is the starting point for
the next drawing operation. An operation may draw a line (see lineTo()), arc (see arcTo()), or bezier curve (see quadTo() and cubicTo()) from the current
position to a new position. A new sub path may be started by moving the current position to a new
location (see moveTo()), which automatically closes
the previous sub path.
When sub paths overlap, the result is undefined (it is dependent on the underlying painting device).
Usage example:
WPainter painter = new WPainter();
WPainterPath path = new WPainterPath(new WPointF(10, 10));
path.lineTo(10, 20);
path.lineTo(30, 20);
path.closeSubPath();
painter.setPen(new WPen(WColor.red));
painter.setBrush(new WBrush(WColor.blue));
painter.drawPath(path);
JavaScript exposability
A WPainterPath is JavaScript exposable. If a WPainterPath is JavaScript bound, it can be accessed in your
custom JavaScript code through its handle's jsRef().
A WPainterPath is represented in JavaScript as an array of segments, where each segment is defined by a three element array: [x,y,type], where type is the integer representation of the type of a segment.
For example, a 10 by 10 square with the top left at (10,10) is represented as:
[
[10,10,0], // move to (10,10)
[20,10,1], // line to (20,10)
[20,20,1], // line to (20,20)
[10,20,1], // line to (10,20)
[10,10,1] // line to (10,10)
]
Warning: A WPainterPath that is JavaScript exposed should be modified only through
its handle. Any attempt at modifying it will cause an exception to be
thrown.
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.WPainterPath(WPainterPath path) Copy constructor.WPainterPath(WPointF startPoint) Construct a new path, and set the initial position. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddEllipse(double x, double y, double width, double height) Draws an ellipse.voidaddEllipse(WRectF rect) Draws an ellipse.voidaddPath(WPainterPath path) Adds a path.voidaddPolygon(List<WPointF> points) Adds a polygon.voidaddRect(double x, double y, double width, double height) Draws a rectangle.voidDraws a rectangle.voidarcMoveTo(double cx, double cy, double radius, double angle) Moves to a point on an arc.voidarcMoveTo(double x, double y, double width, double height, double angle) Move to a point on an arc.voidarcTo(double cx, double cy, double radius, double startAngle, double sweepLength) Draws an arc.assign(WPainterPath path) Assignment method.protected voidassignFromJSON(com.google.gson.JsonElement value) clone()voidCloses the last sub path.voidconnectPath(WPainterPath path) Adds a path, connecting.voidcubicTo(double c1x, double c1y, double c2x, double c2y, double endPointx, double endPointy) Draws a cubic bezier curve.voidDraws a cubic bezier curve.booleanequals(WPainterPath path) Indicates whether some other object is "equal to" this one.final WRectFReturns the bounding box of the control points.getControlPointRect(WTransform transform) Returns the bounding box of the control points.getCrisp()Returns a copy of the path where straight lines are moved to be rendered crisply.Returns the current position.Returns a JavaScript representation of the value of this object.booleanisEmpty()Returns whether the path is empty.booleanReturns whether open subpaths are enabled.voidlineTo(double x, double y) Draws a straight line.voidDraws a straight line.voidmoveTo(double x, double y) Moves the current position to a new location.voidMoves the current position to a new location.voidquadTo(double cx, double cy, double endPointX, double endPointY) Draws a quadratic bezier curve.voidDraws a quadratic bezier curve.final voidDisables automatically closing subpaths on moveTo.voidsetOpenSubPathsEnabled(boolean enabled) Disables automatically closing subpaths on moveTo.Methods inherited from class eu.webtoolkit.jwt.WJavaScriptExposableObject
assignBinding, assignBinding, checkModifiable, closeTo, getJsRef, isJavaScriptBound, sameBindingAs
-
Constructor Details
-
WPainterPath
public WPainterPath()Default constructor.Creates an empty path, and sets the current position to (0, 0).
-
WPainterPath
Construct a new path, and set the initial position.Creates an empty path, and sets the current position to
startPoint. -
WPainterPath
Copy constructor.
-
-
Method Details
-
assign
Assignment method. -
clone
- Specified by:
clonein classWJavaScriptExposableObject
-
getCurrentPosition
Returns the current position.Returns the current position, which is the end point of the last move or draw operation, and which well be the start point of the next draw operation.
-
isEmpty
public boolean isEmpty()Returns whether the path is empty.Returns
trueif the path contains no drawing operations. Note that move operations are not considered drawing operations. -
equals
Indicates whether some other object is "equal to" this one.Returns
trueif the paths are exactly the same. -
closeSubPath
public void closeSubPath()Closes the last sub path.Draws a line from the current position to the start position of the last sub path (which is the end point of the last move operation), and sets the current position to (0, 0).
-
moveTo
Moves the current position to a new location.Moves the current position to a new point, implicitly closing the last sub path, unless
open subpaths are enabled. -
moveTo
public void moveTo(double x, double y) Moves the current position to a new location.Moves the current position to a new point, implicitly closing the last sub path, unless
open subpaths are enabled. -
lineTo
Draws a straight line.Draws a straight line from the current position to
point, which becomes the new current position.- See Also:
-
lineTo
public void lineTo(double x, double y) Draws a straight line.Draws a straight line from the current position to (
x,y), which becomes the new current position.- See Also:
-
cubicTo
Draws a cubic bezier curve.Draws a cubic bezier curve from the current position to
endPoint, which becomes the new current position. The bezier curve uses the two control points c1 andc2. -
cubicTo
public void cubicTo(double c1x, double c1y, double c2x, double c2y, double endPointx, double endPointy) Draws a cubic bezier curve.This is an overloaded method provided for convenience.
-
arcTo
public void arcTo(double cx, double cy, double radius, double startAngle, double sweepLength) Draws an arc.Draws an arc which is a segment of a circle. The circle is defined with center (cx, cy) and
radius. The segment starts atstartAngle, and spans an angle given byspanAngle. These angles are expressed in degrees, and are measured counter-clockwise starting from the 3 o'clock position.Implicitly draws a line from the current position to the start of the arc, if the current position is different from the start.
-
arcMoveTo
public void arcMoveTo(double cx, double cy, double radius, double angle) Moves to a point on an arc.Moves to a point on a circle. The circle is defined with center (cx, cy) and
radius, and the point is atangledegrees measured counter-clockwise starting from the 3 o'clock position. -
arcMoveTo
public void arcMoveTo(double x, double y, double width, double height, double angle) Move to a point on an arc.Moves to a point on an ellipse. The ellipse fits in the rectangle defined by top left position (
x, y), and size width xheight, and the point is atangledegrees measured counter-clockwise starting from the 3 o'clock position. -
quadTo
Draws a quadratic bezier curve.Draws a quadratic bezier curve from the current position to
endPoint, which becomes the new current position. The bezier curve uses the single control pointc. -
quadTo
public void quadTo(double cx, double cy, double endPointX, double endPointY) Draws a quadratic bezier curve.This is an overloaded method provided for convenience.
- See Also:
-
addEllipse
Draws an ellipse.This method closes the current sub path, and adds an ellipse that is bounded by the rectangle
boundingRectangle.Note:some renderers only support circles (width == height) -
addEllipse
public void addEllipse(double x, double y, double width, double height) Draws an ellipse.This method closes the current sub path, and adds an ellipse that is bounded by the rectangle defined by top left position (
x, y), and size width xheight.Note: Some renderers only support circles (width == height)
-
addRect
Draws a rectangle.This method closes the current sub path, unless
open subpaths are enabled, and adds a rectangle that is defined byrectangle. -
addRect
public void addRect(double x, double y, double width, double height) Draws a rectangle.This method closes the current sub path, unless
open subpaths are enabled, and adds a rectangle that is defined by top left position (x,y), and size width xheight.- See Also:
-
addPolygon
Adds a polygon.If the first point is different from the current position, the last sub path is first closed, unless
open subpaths are enabled, otherwise the last sub path is extended with the polygon.- See Also:
-
addPath
Adds a path.Adds an entire
pathto the current path. If the path's begin position is different from the current position, the last sub path is first closed, unlessopen subpaths are enabled, otherwise the last sub path is extended with the path's first sub path.- See Also:
-
connectPath
Adds a path, connecting.Adds an entire
pathto the current path. If the path's begin position is different from the current position, the last sub path is first closed, unlessopen subpaths are enabled, otherwise the last sub path is extended with the path's first sub path.- See Also:
-
getSegments
-
getControlPointRect
Returns the bounding box of the control points.Returns the bounding box of all control points. This is guaranteed to be a superset of the actual bounding box.
The
transformis applied to the path first. -
getControlPointRect
Returns the bounding box of the control points. -
getCrisp
Returns a copy of the path where straight lines are moved to be rendered crisply.This is intended to be used on rectangles, or other paths consisting of only straight line, and will nudge every edge a little bit, so that 1px straight lines are rendered as a crisp line.
This will also work if the path
is JavaScript bound. -
setOpenSubPathsEnabled
public void setOpenSubPathsEnabled(boolean enabled) Disables automatically closing subpaths on moveTo.By default, open sub paths are disabled, and moveTo and any operation that relies on moveTo will automatically close the last subpath. Enabling this option disables that feature.
-
setOpenSubPathsEnabled
public final void setOpenSubPathsEnabled()Disables automatically closing subpaths on moveTo. -
isOpenSubPathsEnabled
public boolean isOpenSubPathsEnabled()Returns whether open subpaths are enabled.- See Also:
-
getJsValue
Description copied from class:WJavaScriptExposableObjectReturns a JavaScript representation of the value of this object.Note: The value returned will reflect the current server side value of the object. If this object is JavaScript bound, this value may not reflect the actual client side value. If you need access to the client side value, use
getJsRef()intead.- Specified by:
getJsValuein classWJavaScriptExposableObject
-
assignFromJSON
protected void assignFromJSON(com.google.gson.JsonElement value) - Specified by:
assignFromJSONin classWJavaScriptExposableObject
-