Hi Tarjei,
tarjei <[EMAIL PROTECTED]> wrote on 04/14/2008 01:18:21 PM:

> My problem is that I cannot manage to create the rectangles using the
> DOM API and include the Image using the SVGGraphics2D API at the same
> time. I manage to write ou documents that do one of the two - but not 
both.

   The problem is that the SVGGraphics2D doesn't autmatically append it's
elements to the document it's using as a factor for elements.  So your
SVGGraphics2D content and your DOM content don't end up in the same 
document
tree.

   So at some point (perhaps at the end of addBackground) you will 
want to call:
        svg.getRoot(document.getDocumentElement());

   Then you will need to serialize the Document rather than the
SVGGraphics2D.  The simplest way to do that is probably with
the batik.dom.util.DOMUtilities.writeDocument static method.

> 
> If someone can manage to see what is wrong, I would be very greatfull. I
> am using batik-dom 1.6.1 and Java 1.6 on Ubuntu 7.10.
> 
> Kind regards,
> Tarjei
> 
> public class ModelWriter {
> 
>     protected final String NS = "http://www.w3.org/2000/svg";;
>     DOMImplementation domImpl;
>     Document document;
>     SVGGraphics2D svg;
>     ModelWriter() {
>         domImpl = GenericDOMImplementation.getDOMImplementation();
>         document =
> domImpl.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI, "svg", 
null);
>         // Create an instance of the SVG Generator.
>         svg = new SVGGraphics2D(document);
> 
>     }
> 
>     public void addBackground(ImagePlus image) {
>        svg.setSVGCanvasSize(new Dimension(image.getWidth(),
> image.getHeight()));
>        svg.drawImage(image.getImage(),0,0, image.getWidth(),
> image.getHeight(), null);
>     }
> 
>     public void addRegions(Collection<Region> regions, int colorInt) {
>        String style = "stroke:"+
> no.sysifos.imageLibrary.Color.toRGBHexString(colorInt)
> +";stroke-width:2;fill-opacity:0.5;";
>        for (Region r : regions) {
>          addRectangle(r, style);
>        }
>     }
> 
>     private void addRectangle(Region r, String style) {
>        //Element svgRoot = svg.getRoot();
>        Element svgRoot = document.getDocumentElement();
>        //Element rectangle =
> document.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, 
"rect");
>        //Element rectangle = svg.getDOMFactory().createElementNS(NS, 
"rect");
>        Element rectangle = document.createElementNS(NS, "rect");
> 
>        rectangle.setAttribute("x",""+ r.x);
>        rectangle.setAttribute("y",""+ r.y);
>        rectangle.setAttribute("width", "" + r.getWidth());
>        rectangle.setAttribute("height","" + r.getHeight());
>        rectangle.setAttribute("style", style);
> 
>        // Attach the rectangle to the root 'svg' element.
>        svgRoot.appendChild(rectangle);
>     }
> 
>     public void write(String fileName) {
>        // Finally, stream out SVG to the standard output using
>        // UTF-8 encoding.
>        boolean useCSS = true; // we want to use CSS style attributes
>        Writer out;
>       try {
>          out = new OutputStreamWriter(new FileOutputStream(new 
File(fileName))
> , "UTF-8");
>          svg.stream(out, useCSS);
>       } catch (UnsupportedEncodingException e) {
>          e.printStackTrace();
>       } catch (FileNotFoundException e) {
>          e.printStackTrace();
>       } catch (SVGGraphics2DIOException e) {
>          e.printStackTrace();
>       }
>     }
> 
> }
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

Reply via email to