You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by ml...@mherrn.de on 2012/06/18 15:57:51 UTC

Modify a loaded SVG document with Graphics2D?

Hi,

is it possible to load an SVG document from file and display it in an
SVGCanvas and additionally modify that document via a Graphics2D object?

For example, is the following possible:
------------------/------------------
final JFrame jFrame= new JFrame();
final SVGCanvas canvas= new SVGCanvas();
canvas.setURI(new File("mySVGFile.svg").toURI().toString());
jFrame.getContentPane().add(canvas);
jFrame.pack();
jFrame.setVisible(true);

[...]

final SVGGraphics2D g= new SVGGraphics2D(canvas.getSVGDocument());
final Rectangle r= new Rectangle(10,10,100,100);
g.setPaint(Color.RED);
g.setStroke(new BasicStroke(15f));
g.draw(r);

[...]
------------------/------------------
to modify the image in the canvas?
What I want to achieve is to paint on the loaded SVG File in the canvas.
The painted elements need to be visible additionally to the elements
loaded from the file.


Regards
Marco



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: Modify a loaded SVG document with Graphics2D?

Posted by ml...@mherrn.de.
> is it possible to load an SVG document from file and display it in an
> SVGCanvas and additionally modify that document via a Graphics2D object?
>
> For example, is the following possible:
> ------------------/------------------
> final JFrame jFrame= new JFrame();
> final SVGCanvas canvas= new SVGCanvas();
> canvas.setURI(new File("mySVGFile.svg").toURI().toString());
> jFrame.getContentPane().add(canvas);
> jFrame.pack();
> jFrame.setVisible(true);
>
> [...]
>
> final SVGGraphics2D g= new SVGGraphics2D(canvas.getSVGDocument());
> final Rectangle r= new Rectangle(10,10,100,100);
> g.setPaint(Color.RED);
> g.setStroke(new BasicStroke(15f));
> g.draw(r);
>
> [...]
> ------------------/------------------
> to modify the image in the canvas?

I found it out.
It is:

------------------/------------------
Element root = canvas.getDocument().getDocumentElement();
g.getRoot(root);
------------------/------------------

The rather confusingly named method getRoot(Element) does the writing by
appending the drawn elements to the end of the SVG document.
Unfortunately, batik always adds an (empty) <def> element with
id="genericDefs". So if modifying the document is done multiple times, you
end up with several <def> elements with the same id....

Regards
Marco


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org