Class MeshGradient.Builder
- java.lang.Object
-
- org.faceless.pdf2.MeshGradient.Builder
-
- Enclosing class:
- MeshGradient
public static class MeshGradient.Builder extends Object
A Builder used for creating aMeshGradient
. See theMeshGradient
class API docs for a worked example of using this class.
-
-
Constructor Summary
Constructors Constructor Description Builder()
Create a new MeshGradient Builder
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addFirst(Shape shape, Color c0, Color c1, Color c2, Color c3)
Add a patch.void
addNext(Shape shape, Color... colors)
Add a secondary patch which is partially derived from the previous patch(es).MeshGradient
createMeshGradient()
Return a newMeshGradient
.Color
getColor(int column, int row, int corner)
Return the specified color of the specified Patchint
getNumColumns()
Return the number of columns in this patchint
getNumRows()
Return the number of rows in this patchString
getPath(int column, int row, int edge)
Return the specified edge of the specified Patch as an SVG path segmentAffineTransform
getTransform()
Return the AffineTransform set on this meshvoid
nextRow()
Start the next row of patches in the mesh array.void
setTransform(AffineTransform transform)
Set the AffineTransform to use on this meshString
toString()
-
-
-
Method Detail
-
addFirst
public void addFirst(Shape shape, Color c0, Color c1, Color c2, Color c3)
Add a patch. A PathIterator will be created from the supplied shape to define the four sides of the patch. As this method is intended for the first patch in the mesh, all four sides and corners must be defined.
The first patch is typically the top-left corner of the mesh. Its shape must have four segments, drawn clockwise from (typically) the top-left, and there must be four colors describing each corner starting from the start point of the shape.
Later patches can be added with
addNext(java.awt.Shape, java.awt.Color...)
, or repeated calls to this method can be made if preferred, although care must be taken to ensure the edges are matched.All patches should be in the same ColorSpace. If they are not, we'll attempt to resolve this internally, typically falling back to Lab if the colorspaces cannot be reconciled. This may not give the correct results, not least because Lab Coons Patch gradients are poorly supported in PDF viewers (as of 2021).
- Parameters:
shape
- the shape, which should have a PathIterator describing a move followed by 4 line or curve segmentsc0
- the color for the first edgec1
- the color for the second edgec2
- the color for the third edgec3
- the color for the fourth edge
-
addNext
public void addNext(Shape shape, Color... colors)
Add a secondary patch which is partially derived from the previous patch(es). The number of sides and colors that must be specified depend on where in the mesh this patch is located:- Patches on the first row (after the first one) must have three segments which start at the top-left of the patch and describe the top, right and bottom segment. The shape will be translated so the start point matches the patch to the left, and the translated end point must match the corresponding point in that patch. There must be two colors describing the top-right and bottom-right corners
- The first patch on any row after the first must have three segments which start at the top-right of the patch, and describe the right, bottom and left segment. The shape will be translated so the start point matches the patch on the previous row, and the end point must match the corresponding point on that patch. There must be two colors describing the bottom-right and bottom-left corners
- Any other patches must have two segments which start at the top-right of the patch and descibe the right and bottom segment. The shape will be translated so the start point matches the patch on the previous row, and the end point must match the patch in the previous column. There must be one color, describing the bottom right corner.
All patches should be in the same ColorSpace. If they are not, we'll attempt to resolve this internally, typically falling back to Lab if the colorspaces cannot be reconciled. This may not give the correct results, not least because Lab Coons Patch gradients are poorly supported in PDF viewers (as of 2021).
If the end points of a patch to not align with the correponding points in the previous row and/or column, an
IllegalArgumentException
is thrown.- Parameters:
shape
- the shape, which should have a PathIterator describing 1 or 2 line or curve segmentscolors
- the list of 1 or 2 colors as described above- Throws:
IllegalArgumentException
- if the points don't match the previous patch
-
nextRow
public void nextRow()
Start the next row of patches in the mesh array. The first time this is called, the number of patches added so far determines the number of columns in the patch. Later calls to nextRow must be called so that each row has the same number of columns
-
createMeshGradient
public MeshGradient createMeshGradient()
Return a newMeshGradient
. A MeshGradient should not be shared across PDF objects; if it is to be reused, a new MeshGradient should be created.
-
setTransform
public void setTransform(AffineTransform transform)
Set the AffineTransform to use on this mesh
-
getTransform
public AffineTransform getTransform()
Return the AffineTransform set on this mesh
-
getNumColumns
public int getNumColumns()
Return the number of columns in this patch
-
getNumRows
public int getNumRows()
Return the number of rows in this patch
-
getPath
public String getPath(int column, int row, int edge)
Return the specified edge of the specified Patch as an SVG path segment- Parameters:
column
- the column number, from 0..getNumColumns()
row
- the row number, from 0..getNumRows()
edge
- the edge, from 0 to 1,2 or 3 inclusive, depending on which patch it is
-
getColor
public Color getColor(int column, int row, int corner)
Return the specified color of the specified Patch- Parameters:
column
- the column number, from 0..getNumColumns()
row
- the row number, from 0..getNumRows()
edge
- the corner, from 0 to 1,2 or 3 inclusive, depending on which patch it is
-
-