The goal of the Batik Swing component module is to provide a Swing
component that can used to display SVG documents.
With the
JSVGCanvas
class, you can easily display an SVG document (from a URI or a DOM tree)
and allow the user to manipulate it, such as rotating, zooming, panning,
selecting text or activating hyperlinks. First this document explains how
to create a JSVGCanvas and integrate it in to a Swing
application. Then, it descibes some advanced features such as the listener
mechanism used to track all events that occurred while displaying or
manipulating an SVG document.
The following example illustrates how to create a
JSVGCanvas, which is a Swing component that follows the
Swing design rule. This means that the component is not thread safe and
all operations on the component or the document it is displaying must
be performed as described in
the
Swing tutorial. The JSVGCanvas is also a
JavaBean, so it
can be used in visual application builders.
CLASSPATH so that it contains the Batik classes and
resources, as well as Xerces (lib/xerces_2_5_0.jar)
and the XML APIs jars (lib/xml-apis.jar and
lib/xml-apis-ext.jar).
Each time you set a URI or an SVG DOM tree to be displayed in a
JSVGCanvas (using the setURI or
setSVGDocument method), the specified document is first
parsed (in case of a URI), built, rendered and optionally updated. The
proper way to be notified of these different phases is to implement a
listener and attach it to the component. There are five types
of listener:
SVGDocumentLoaderListenerThe SVGDocumentLoaderListener provides a set of methods that can be used to track SVGDocumentLoaderEvent events. It describes the loading phase: contructing an SVG DOM tree using an SVG file.
GVTTreeBuilderListenerThe GVTTreeBuilderListener provides a set of methods that can be used to track GVTTreeBuilderEvent events. It describes the building phase: contructing a GVT (Graphics Vector Toolkit) tree using an SVG DOM tree. The GVT tree will then be used to render the document.
SVGLoadEventDispatcherListener
The
SVGLoadEventDispatcherListener
provides a set of methods that can be used to track
SVGLoadEventDispatcherEvent
events. It describes the DOM SVGLoad event dispatch phase.
This event is triggered only in dynamic documents.
GVTTreeRendererListenerThe GVTTreeRendererListener provides a set of methods that can be used to track GVTTreeRendererEvent events. It describes the rendering phase: constructing an image using a GVT tree.
In dynamic documents this event is fired only once for the initial rendering.
UpdateManagerListenerThe UpdateManagerListener provides a set of methods that can be used to track UpdateManagerEvent events. It describes the running phase: the update manager is started and then it can be suspended, resumed or stopped, and graphics updates can be tracked.
This event is triggered only in dynamic documents.
These listeners give a complete description of the different steps of the five phases (including error states). Adapter classes are available to ease the creation of new listener implementation—SVGDocumentLoaderAdapter, GVTTreeBuilderListenerAdapter, SVGLoadEventDispatcherAdapter, GVTTreeRendererAdapter and UpdateManagerAdapter,
For static documents, you can assume that the JSVGCanvas has completed
its job (parsing, building and rendering) when the
gvtRenderingCompleted method call is called, following a
setURI or a setSVGDocument method call.
In the case of dynamic documents, the end of the computation (parsing,
building, SVGLoad dispatch, initial rendering and updates) is marked
by a call to the updateManagerStopped method.
The JSVGCanvas provides a set of build-in interactors that
let the users manipulate the displayed document, including ones for
zooming, panning and rotating. Interactors catch user input to the
JSVGCanvas component and translate them into behaviour.
If you want to add new behaviors to the JSVGCanvas, you
can implement the
Interactor
interface. Then, you can register this new interactor to the component
by adding an element to the List returned by the
getInteractors() method of the canvas.