|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
ObjectAffineTransform
TransformedShape
@NotThreadSafe public final class TransformedShape
Applies an arbitrary AffineTransform on a Shape. A TransformedShape
instance is a view over a shape, i.e. the shape coordinates are transformed on the
fly, never copied.
The shape to be transformed is specified by setOriginalShape(Shape). The transform
to apply is specified by the inherited AffineTransform, which can be modified at any
time using all its usual methods. Every changes to the original shape or to the inherited
transform take immediate effect since this class is only a view.
Note: This class is final because extending directlyAffineTransformis not a good example of object-oriented programming, since a transformed shape is not a special kind of affine transform. We did that as a convenience for allowing frequent modifications of the transform by direct access to theAffineTransformmethods, but this is not an example that shoud be replicated.
Performance cost
When iterating over the shape boundary, the only
performance cost is a matrix multiplication applied
before the iterator is created - the cost of the iteration itself usually stay
unchanged.
Example
The Arrow2D class is unconditionally oriented toward 0° arithmetic.
In order to draw a field of arrows in arbitrary directions, the code below can be used:
protected void paint(Graphics2D graphics) {
Arrow2D arrow = new Arrow2D(...);
TransformedShape orientedArrow = new TransformedShape(arrow);
for (int i=0; i<numArrows; i++) {
double scale = ...
double orientation = Math.toRadians(...);
orientedArrow.setToScale(scale, scale);
orientedArrow.rotate(orientation);
graphics.fill(orientedArrow);
}
}
An alternative to this TransformedShape class would be to change directly the
Graphics2D transform. The main difference between those alternatives
is that when using TransformedShape, the transform does not apply to the line
thickness or paint texture. The preferred alternative depends on the desired rendering
effect, for example if line thickness needs to be specified in pixel units or in
"real world" units.
ProjectedShape,
Serialized Form
| referencing/geotk-referencing (download) | View source code for this class |
| Field Summary |
|---|
| Fields inherited from class AffineTransform |
|---|
TYPE_FLIP, TYPE_GENERAL_ROTATION, TYPE_GENERAL_SCALE, TYPE_GENERAL_TRANSFORM, TYPE_IDENTITY, TYPE_MASK_ROTATION, TYPE_MASK_SCALE, TYPE_QUADRANT_ROTATION, TYPE_TRANSLATION, TYPE_UNIFORM_SCALE |
| Constructor Summary | |
|---|---|
TransformedShape()
Constructs a TransformedShape initialized to the identity transform
with no original shape. |
|
TransformedShape(AffineTransform transform)
Constructs a TransformedShape initialized to the given transform with no
original shape. |
|
TransformedShape(Shape shape)
Constructs a view of the given shape initialized to the identity transform. |
|
TransformedShape(Shape shape,
AffineTransform transform)
Constructs a TransformedShape initialized to the given transform and shape. |
|
| Method Summary | |
|---|---|
boolean |
contains(double x,
double y)
Tests if the specified coordinate is inside the boundary of this shape. |
boolean |
contains(double x,
double y,
double width,
double height)
Tests if the interior of this shape entirely contains the specified rectangular area. |
boolean |
contains(Point2D p)
Tests if a specified Point2D is inside the boundary of this shape. |
boolean |
contains(Rectangle2D r)
Tests if the interior of this shape entirely contains the specified rectangle. |
boolean |
equals(Object object)
Compares this shape with the given object for equality. |
Rectangle |
getBounds()
Returns an integer rectangle that completely encloses this shape. |
Rectangle2D |
getBounds2D()
Returns a high precision and more accurate bounding box of the shape than the getBounds() method. |
void |
getMatrix(float[] matrix,
int offset)
Returns the 6 coefficients values as float numbers. |
Shape |
getOriginalShape()
Returns the original shape for which this TransformedShape is a view. |
PathIterator |
getPathIterator(AffineTransform at)
Returns an iterator object that iterates along the shape boundary and provides access to the geometry of the shape outline. |
PathIterator |
getPathIterator(AffineTransform at,
double flatness)
Returns an iterator object that iterates along the shape boundary and provides access to a flattened view of the shape outline geometry. |
int |
hashCode()
Returns a hash code value for this shape. |
boolean |
intersects(double x,
double y,
double width,
double height)
Tests if the interior of this shape intersects the interior of a specified rectangular area. |
boolean |
intersects(Rectangle2D r)
Tests if the interior of this shape intersects the interior of a specified rectangle. |
void |
setOriginalShape(Shape shape)
Sets the shape for which this TransformedShape will be a view. |
void |
setTransform(float[] matrix,
int offset)
Sets the transform from a flat matrix. |
| Methods inherited from class Object |
|---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public TransformedShape()
TransformedShape initialized to the identity transform
with no original shape. The setOriginalShape(Shape) method must be
invoked at least once before this object can be used.
public TransformedShape(AffineTransform transform)
TransformedShape initialized to the given transform with no
original shape. The setOriginalShape(Shape) method must be invoked at
least once before this object can be used.
transform - The initial affine transform.public TransformedShape(Shape shape)
shape - The shape to wrap in the new TransformedShape instance.
public TransformedShape(Shape shape,
AffineTransform transform)
TransformedShape initialized to the given transform and shape.
shape - The shape to wrap in the new TransformedShape instance.transform - The initial affine transform.| Method Detail |
|---|
public Shape getOriginalShape()
TransformedShape is a view.
TransformedShape instance.public void setOriginalShape(Shape shape)
TransformedShape will be a view.
shape - The shape to wrap in this TransformedShape instance.
public void getMatrix(float[] matrix,
int offset)
float numbers. The coefficients are stored
in the same order than AffineTransform.getMatrix(double[]), with the first coefficient stored
in matrix[offset] and the last coefficient stored in matrix[offset+5]
matrix - The matrix where to store the coefficient values.offset - Index of the first element where to write in the matrix array.
public void setTransform(float[] matrix,
int offset)
getMatrix(float[], int).
matrix - The flat matrix.offset - Index of the first element to use in matrix array.
public boolean contains(double x,
double y)
false if the transform is not invertible.
The default implementation delegates to contains(Point2D).
contains in interface Shapex - The x ordinate of the point to be tested.y - The y ordinate of the point to be tested.
true if this shape contains the given point.public boolean contains(Point2D p)
Point2D is inside the boundary of this shape.
This method might conservatively return false if the transform is not invertible.
contains in interface Shapep - The point to be tested.
true if this shape contains the given point.
public boolean contains(double x,
double y,
double width,
double height)
false if the transform is not invertible
or if the calculation of originalShape.contains(...) is too expensive.
The default implementation delegates to contains(Rectangle2D).
contains in interface Shapex - The minimal x ordinate of the rectangle to be tested.y - The minimal y ordinate of the rectangle to be tested.width - The width of the rectangle to be tested.height - The height of the rectangle to be tested.
true if this shape contains the given rectangle.public boolean contains(Rectangle2D r)
false if the transform is not
invertible or if the calculation of originalShape.contains(...) is too
expensive.
contains in interface Shaper - The rectangle to be tested.
true if this shape contains the given rectangle.
public boolean intersects(double x,
double y,
double width,
double height)
true if the transform is not invertible or
if the calculation of originalShape.intersects(...) is too expensive.
The default implementation delegates to intersects(Rectangle2D).
intersects in interface Shapex - The minimal x ordinate of the rectangle to be tested.y - The minimal y ordinate of the rectangle to be tested.width - The width of the rectangle to be tested.height - The height of the rectangle to be tested.
true if this shape intersects the given rectangle.public boolean intersects(Rectangle2D r)
true if the transform is not invertible
or if the calculation of originalShape.intersects(...) is too expensive.
intersects in interface Shaper - The rectangle to be tested.
true if this shape intersects the given rectangle.public Rectangle getBounds()
getBounds in interface Shapepublic Rectangle2D getBounds2D()
getBounds() method.
getBounds2D in interface Shapepublic PathIterator getPathIterator(AffineTransform at)
getPathIterator in interface Shape
public PathIterator getPathIterator(AffineTransform at,
double flatness)
getPathIterator in interface Shapepublic int hashCode()
hashCode in class AffineTransformpublic boolean equals(Object object)
TransformedShape instances with plain AffineTransform instances,
because the "be symmetric" part of the Object.equals(Object) contract
can not be enforced.
equals in class AffineTransformobject - The object to compare with this shape.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||