|
Transform contexts
The JCanvas roots a tree of graphical items, starting with a
CanvasPane, then layers, figures, and then additional figures and
panes to arbitrary depth. Some nodes of this tree define a new
transform context or coordinate context. These nodes
contain an instance of the TransformContext class, which in turn
contains an instance of java.awt.geom.AffineTransform . A
canvas pane always defines a new transform context; canvas layers
never do, as they use they use the transform context of their
containing pane; individual figure classes can choose whether or not
to define a new context.
The CanvasComponent interface defines the getTransformContext()
method. Any component which defines a transform context should return
its instance of TransformContext; components that do not define a new
context must call their parent's getTransformContext() method.
The transform context defines the coordinate system in which
operations on figures take place. If a figure is contained directly in
a layer, geometric operations on the figure are in the transform
context of that layer. (Although we are talking about layers, the
transform context is in fact defined by the layer's enclosing pane.)
Most interactive applications will use this transform context by
default, as it can be thought of as the "world coordinates" of the
application. If the figure is a descendent of another figure that has
a transform context, geometric operations on the figure are in the
enclosing transform context -- that is, the transform context of its
parent -- and must therefore be transformed to and from layer
coordinates when necessary.
The diagram below illustrates a sample of the display tree. If a
client takes the bounds of figures A, B, C or
F, then the result will be in layer coordinates -- that is,
transform context X. However, if the client takes the bounds of
D or E, the result will be in the transform context
Y, and will need to be transformed by the client to layer
coordinates if necessary.
The primary function of a TransformContext is to keep an instance
of an AffineTransform and to provide translation between its
coordinate space and the containing layer and the screen. In
addition, because Java2D does not implement a transform stack, this
class provides push() and pop() methods. Components that define a
transform context must call these methods when entering and leaving
their context during a paint() operation.
A secondary function of TransformContext objects is to cache
computed transforms to reduce the overhead of computing new
transforms. To support caching, the transform context contains a
flag, cacheValid, that indicates the state of cached transforms. If a
request for a layer or screen transform is made, and the cache is not
valid. the TransformContext recomputes the transform and caches
it. There is also a version number in the context, which is
incremented whenever the transform changes. This can be used by client
components to figure out when to update cached data that depends on
the transform.
|