Re: drawing SVG on a Canvas

2005-02-15 Thread Andres Toussaint
Hi Tim,

Yes. you can parse XML and fetch either the BufferedImage, or the array of Graphics2D objects that represent the SVG.

Basically, you need to create a JSVGComponent (you do not need a JSVGCanvas since you are displaying in a AWT) and add the build and render listeners, so you are notified when the data is ready to be fetched. The important one is GVTRenderListener. When the image is fully rendered into a BufferedImage, a GVTRenderingCompleted is triggered and you can fetch the image directly from the Event.

Once the listeners are in place, you need to load the document to your JSVGComponent, you can do so from a String (probably recuperated from a Database), from the filesystem, or from a prebuild org.w3c.dom.Document object. Once you assign the document to the JSVGComponent, simply wait until the event is triggered, and that is it.

Here is an abstract on how to do it.

---Start of code
svg = new JSVGComponent();
svg.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
}
public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
//System.out.println("SaveAsDXF-ExportCache succesfully loaded");
}
});
svg.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
public void gvtBuildStarted(GVTTreeBuilderEvent e) {

//System.out.println("SaveAsDXF-ExportCache-GVTBuilder Starting.");
}
public void gvtBuildCompleted(GVTTreeBuilderEvent event) {

 If you want access to the G2D elements (generalPath, shapes, rectangles2D, you can
 get the GVTTree in this place and walk through it. The GVTTree has all your SVG 
 converted into Graphics2D objects.

}
});

svg.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
}
public void gvtRenderingCompleted(GVTTreeRendererEvent e) {


// In here you can access your image, once the JSVGComponent has loaded
// the XMl and parsed it as SVG and into Graphics2D objects, and finally
// painted them into a BufferedImage.


BufferedImage e.getImage();


}
});

 And now, to load your XML into the JSVGComponent you need something like this:
 Please note that this example has it that the XML is already in memory in a String.
 But you can have a FileReader to read from the filesystem also.
try {
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
//data is the XML that defines the SVG. Usually a string.
Document doc = f.createSVGDocument(null, new java.io.StringReader((String)data));

"svg" is the JSVGComponent object.			
svg.setDocument(doc);


} catch (IOException ex) {
System.out.println("Error de svg."+ex);
}	

//-end of code

On Feb 15, 2005, at 8:57 AM, Tim Clerckx wrote:

Hi,

I want to parse an SVG document and then show it in AWT. Is it possible to build the GVT tree from an XML document, and then call the paint method on the root?

Thomas DeWeese wrote:

Hi Tim,

Tim Clerckx wrote:

Is it somehow possible to draw SVG on a java.awt.Canvas instead of using the JSVGCanvas swing component?


Yes, internally Batik builds something called the GVT tree the
tree is made up of 'GraphicsNodes' which have a paint method
that takes a standard Graphics2D.

The main purpose of the JSVGCanvas is to make drawing async,
manage an offscreen buffer, panning/zooming, and updates to the
screen.  If you just want to draw a simple static SVG you can
just call the 'paint' method with a graphics2D.  If you want all/most
of the above then you are probably best off using the JSVGCanvas.

Search the list for GVTBuilder for example code on how to
build your own GVT tree.


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: drawing SVG on a Canvas

2005-02-15 Thread Tim Clerckx
Hi,
I want to parse an SVG document and then show it in AWT. Is it possible 
to build the GVT tree from an XML document, and then call the paint 
method on the root?

Thomas DeWeese wrote:
Hi Tim,
Tim Clerckx wrote:
Is it somehow possible to draw SVG on a java.awt.Canvas instead of 
using the JSVGCanvas swing component?

   Yes, internally Batik builds something called the GVT tree the
tree is made up of 'GraphicsNodes' which have a paint method
that takes a standard Graphics2D.
   The main purpose of the JSVGCanvas is to make drawing async,
manage an offscreen buffer, panning/zooming, and updates to the
screen.  If you just want to draw a simple static SVG you can
just call the 'paint' method with a graphics2D.  If you want all/most
of the above then you are probably best off using the JSVGCanvas.
   Search the list for GVTBuilder for example code on how to
build your own GVT tree.
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Re: drawing SVG on a Canvas

2005-02-15 Thread Thomas DeWeese
Hi Tim,
Tim Clerckx wrote:
Is it somehow possible to draw SVG on a java.awt.Canvas instead of using 
the JSVGCanvas swing component?
   Yes, internally Batik builds something called the GVT tree the
tree is made up of 'GraphicsNodes' which have a paint method
that takes a standard Graphics2D.
   The main purpose of the JSVGCanvas is to make drawing async,
manage an offscreen buffer, panning/zooming, and updates to the
screen.  If you just want to draw a simple static SVG you can
just call the 'paint' method with a graphics2D.  If you want all/most
of the above then you are probably best off using the JSVGCanvas.
   Search the list for GVTBuilder for example code on how to
build your own GVT tree.
-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]