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 kpu <ol...@gmail.com> on 2010/04/03 22:32:26 UTC

SVGGraphics2D how to group elements

Hi,
 I wish grouped elements "by component".

This code example below generate this svg content
...
<g>
 <g style="fill:blue; stroke:blue;">
  <circle r="25" style="stroke:none;" cx="25" cy="25"/>
  <rect x="0" y="50" width="50" style="fill:lime; stroke:none;"
height="50"/>
 </g>
</g>
</svg>

But i would like this (group by component) :
<g>
  <g style="fill:blue; stroke:blue;">
     <circle r="25" style="stroke:none;" cx="25" cy="25"/>
     ...
  </g>
  <g> 
     <rect x="0" y="50" width="50" style="fill:lime; stroke:none;"
height="50"/>
     ...
  </g>
</g> 

I try  create a <g> element and call
method  getDOMTreeManager().appendGroup(element_g, getDomGroupManager())
but element <g> is simply added, non become parent of future painting 
operations of the component.

Is this possible ? 
Thanks.

//example 

import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Graphics2D;
import java.awt.Color;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.dom.GenericDOMImplementation;

import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;

class MyForm1 {
   public void draw(Graphics2D g) {      
      g.setColor(Color.BLUE);
      g.fillOval(0, 0, 50, 50);
   }
}

class MyForm2{
   public void draw(Graphics2D g) {
      g.setColor(Color.GREEN);
      g.fillRect(0, 50, 50, 50);
   }
}

public class TestSVGGen {

   public static void main(String[] args) throws IOException {
      // Get a DOMImplementation.
      DOMImplementation domImpl = GenericDOMImplementation
            .getDOMImplementation();

      // Create an instance of org.w3c.dom.Document.
      String svgNS = "http://www.w3.org/2000/svg";
      Document document = domImpl.createDocument(svgNS, "svg", null);

      // Create an instance of the SVG Generator.
      SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
      
      MyForm1 m1 = new MyForm1();
      MyForm2 m2 = new MyForm2();
      
      m1.draw(svgGenerator);
      m2.draw(svgGenerator);
      
      // Finally, stream out SVG to the standard output using
      // UTF-8 encoding.
      boolean useCSS = true; // we want to use CSS style attributes
      Writer out = new OutputStreamWriter(System.out, "UTF-8");
      svgGenerator.stream(out, useCSS); 
   }
}
-- 
View this message in context: http://old.nabble.com/SVGGraphics2D-how-to-group-elements-tp28129357p28129357.html
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: SVGGraphics2D how to group elements

Posted by kpu <ol...@gmail.com>.
Hello,


Helder Magalhães wrote:
> 
> Hi kpu,
> 
> 
>>  I wish grouped elements "by component".
> [...]
>> This code example below generate this svg content
> [code snippet]
>> But i would like this (group by component) :
> [code snippet]
> 
> I'm not sure what you mean with "by component"... I didn't found a
> relevant enough difference in the code snippets which would help
> understanding the difference. Please clear it up a bit in order to get
> interesting feedback, as I feel this is either 1) not the usual
> keywords for what you mean or 2) is a highly specific concept (beyond
> the scope of graphics/SVG/Batik). Describing what you intend to do [1]
> generally helps as well. ;-)
> 
> 


I want to get the following general schema :
 
<svg>
<g>
  <g> svg instructions of m1  </g>
  <g> svg instructions of m2    </g>
   ...
  <g>   svg instructions of m n    </g>
<g>
</svg>


And NOT as the default ouput :

<svg>
<g>
 svg instructions of m1 
 svg instructions of m2 
   ...
 svg instructions of m n 
</g>
</svg> 

Thanks !
-- 
View this message in context: http://old.nabble.com/SVGGraphics2D-how-to-group-elements-tp28129357p28139682.html
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: SVGGraphics2D how to group elements

Posted by Helder Magalhães <he...@gmail.com>.
Hi kpu,


>  I wish grouped elements "by component".
[...]
> This code example below generate this svg content
[code snippet]
> But i would like this (group by component) :
[code snippet]

I'm not sure what you mean with "by component"... I didn't found a
relevant enough difference in the code snippets which would help
understanding the difference. Please clear it up a bit in order to get
interesting feedback, as I feel this is either 1) not the usual
keywords for what you mean or 2) is a highly specific concept (beyond
the scope of graphics/SVG/Batik). Describing what you intend to do [1]
generally helps as well. ;-)


Cheers,
 Helder


[1] http://catb.org/~esr/faqs/smart-questions.html#goal

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


Re: SVGGraphics2D how to group elements

Posted by kpu <ol...@gmail.com>.
Hi Thomas,


thomas.deweese wrote:
> 
> Hi Olivier,
> 
>> 
>> m1.draw(svgGenerator);
>> svgGenerator.getRoot(elt1_g);
>> 
>> m2.draw(svgGenerator);
>> svgGenerator.getRoot(elt2_g);
>> 
>> 
>> Result is : 
>> <svg><defs id="genericDefs"/><g/></svg>
> 
>    I assume that is from calling 'svgGenerator.stream'?
>    That is why I said:
> 
>> thomas.deweese wrote:
>> > 
>> > 
>> >>       // Finally, stream out SVG to the standard output using
>> >>       // UTF-8 encoding.
>> > 
>> > You will need to manage the streaming of the SVG yourself.
>> > There are many options for this but two from Batik are
>> > batik.svggen.XmlWriter or I think a little better is
>> > bati.dom.DOMUtilities.
> 
>     When you call 'getRoot' the content is removed from the
> SVGGraphics2D and put in the g element you give it.  It is
> then your responsibility to append that group to a document
> however you want and stream it out.  This gives you complete 
> control over the structure of your document.
> 
>     Good luck.
> 
> 

I understand better, so I try :
      
m1.draw(svgGenerator);
svgGenerator.getRoot(elt1_g);
         
m2.draw(svgGenerator);      
svgGenerator.getRoot(elt2_g);
            
Node node = documentGrouped.importNode(elt1_g, true);      
documentGrouped.getDocumentElement().appendChild(node);
      
node = documentGrouped.importNode(elt2_g, true);
documentGrouped.getDocumentElement().appendChild(node);
     
Element svgRoot = documentGrouped.getDocumentElement();
      
svgRoot.setAttributeNS(SVGSyntax.XMLNS_NAMESPACE_URI,
            SVGSyntax.XMLNS_PREFIX,
            SVGSyntax.SVG_NAMESPACE_URI);
      
svgRoot.setAttributeNS(SVGSyntax.XMLNS_NAMESPACE_URI,
            SVGSyntax.XMLNS_PREFIX + ":" + SVGSyntax.XLINK_PREFIX,
            SVGSyntax.SVG_NAMESPACE_URI);
      
System.out.println(DOMUtilities.getXML(documentGrouped.getFirstChild()));

output :

<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/2000/svg">
<g fill-opacity="1" color-rendering="auto" color-interpolation="auto"
text-rendering="auto" stroke="black" stroke-linecap="square"
stroke-miterlimit="10" shape-rendering="auto" stroke-opacity="1"
fill="black" stroke-dasharray="none" font-weight="normal" stroke-width="1"
font-family="&apos;Dialog&apos;" font-style="normal" stroke-linejoin="miter"
font-size="12" image-rendering="auto" stroke-dashoffset="0">
<!--Generated by the Batik Graphics2D SVG Generator-->
<defs id="genericDefs"/>
  <g>
    <g fill="blue" stroke="blue">
      <circle r="25" cx="25" cy="25" stroke="none"/>
    </g>
  </g>
</g>
<g fill-opacity="1" color-rendering="auto" color-interpolation="auto"
text-rendering="auto" stroke="black" stroke-linecap="square"
stroke-miterlimit="10" shape-rendering="auto" stroke-opacity="1"
fill="black" stroke-dasharray="none" font-weight="normal" stroke-width="1"
font-family="&apos;Dialog&apos;" font-style="normal" stroke-linejoin="miter"
font-size="12" image-rendering="auto" stroke-dashoffset="0">
<!--Generated by the Batik Graphics2D SVG Generator-->
<defs id="genericDefs"/>
  <g>
    <g fill="lime" stroke="lime">
      <rect x="0" width="50" y="50" height="50" stroke="none"/>
    </g>
  </g>
</g>
</svg>

Aside from the multiple statements (same id values, comments...) it's Ok !

Thanks !


-- 
View this message in context: http://old.nabble.com/SVGGraphics2D-how-to-group-elements-tp28129357p28144661.html
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: SVGGraphics2D how to group elements

Posted by th...@kodak.com.
Hi Olivier,

kpu <ol...@gmail.com> wrote on 04/05/2010 08:44:12 AM:

> thomas.deweese wrote:
> > 
> >  You need to call 'getRoot(element_g)' this will append everything
> > drawn since the graphics was constructed, or the last time getRoot was
> > called as children of 'element_g'.
> >>       m1.draw(svgGenerator);
> >         svgGenerator.getRoot(g1);
> >>       m2.draw(svgGenerator);
> >         svgGenerator.getRoot(g2);
> > 
> 
> I try : 
> 
> Element elt1_g = 
> 
svgGenerator.getDOMFactory().createElementNS(SVGGraphics2D.SVG_NAMESPACE_URI, 

> SVGGraphics2D.SVG_G_TAG);
> Element elt2_g = 
> 
> 
svgGenerator.getDOMFactory().createElementNS(SVGGraphics2D.SVG_NAMESPACE_URI,
> SVGGraphics2D.SVG_G_TAG);
> 
> m1.draw(svgGenerator);
> svgGenerator.getRoot(elt1_g);
> 
> m2.draw(svgGenerator);
> svgGenerator.getRoot(elt2_g);
> 
> 
> Result is : 
> <svg><defs id="genericDefs"/><g/></svg>

   I assume that is from calling 'svgGenerator.stream'?
   That is why I said:

> thomas.deweese wrote:
> > 
> > 
> >>       // Finally, stream out SVG to the standard output using
> >>       // UTF-8 encoding.
> > 
> > You will need to manage the streaming of the SVG yourself.
> > There are many options for this but two from Batik are
> > batik.svggen.XmlWriter or I think a little better is
> > bati.dom.DOMUtilities.

    When you call 'getRoot' the content is removed from the
SVGGraphics2D and put in the g element you give it.  It is
then your responsibility to append that group to a document
however you want and stream it out.  This gives you complete 
control over the structure of your document.

    Good luck.

Re: SVGGraphics2D how to group elements

Posted by kpu <ol...@gmail.com>.
Hi Thomas,


thomas.deweese wrote:
> 
>  You need to call 'getRoot(element_g)' this will append everything
> drawn since the graphics was constructed, or the last time getRoot was
> called as children of 'element_g'.
>>       m1.draw(svgGenerator);
>         svgGenerator.getRoot(g1);
>>       m2.draw(svgGenerator);
>         svgGenerator.getRoot(g2);
> 

I try : 

Element elt1_g = 
   
svgGenerator.getDOMFactory().createElementNS(SVGGraphics2D.SVG_NAMESPACE_URI, 
SVGGraphics2D.SVG_G_TAG);
Element elt2_g = 
   
svgGenerator.getDOMFactory().createElementNS(SVGGraphics2D.SVG_NAMESPACE_URI,
SVGGraphics2D.SVG_G_TAG);

m1.draw(svgGenerator);
svgGenerator.getRoot(elt1_g);
      
m2.draw(svgGenerator);
svgGenerator.getRoot(elt2_g);


Result is : 
<svg><defs id="genericDefs"/><g/></svg>


I try : 

svgGenerator.getRoot(elt1_g);
m1.draw(svgGenerator);

svgGenerator.getRoot(elt2_g);      
m2.draw(svgGenerator);

Result is : 
<svg><defs id="genericDefs"/><g> svg instructions of m2  </g></svg>

Normal, because the svg root change after m1 drawing, so svg instructions m1
are lost.


Helder Magalhães wrote:
> 
> I'm not sure what you mean with "by component"... I didn't found a
> relevant enough difference in the code snippets which would help
> understanding the difference. Please clear it up a bit in order to get
> interesting feedback, [...]
> 

I want to get the following general schema :
 
<svg>
<g>
  <g> svg instructions of m1  </g>
  <g> svg instructions of m2    </g>
   ...
  <g> svg instructions of m n    </g>
<g>
</svg>


And NOT as the default ouput :

<svg>
<g>
 svg instructions of m1 
 svg instructions of m2 
   ...
 svg instructions of m n 
<g>
</svg>



thomas.deweese wrote:
> 
> 
>>       // Finally, stream out SVG to the standard output using
>>       // UTF-8 encoding.
> 
> You will need to manage the streaming of the SVG yourself.
> There are many options for this but two from Batik are
> batik.svggen.XmlWriter or I think a little better is
> bati.dom.DOMUtilities.
> 

After treatment ?
But, how to distinguish the blocks of instructions between them (m1 and m2 )
?

Thanks for your help




-- 
View this message in context: http://old.nabble.com/SVGGraphics2D-how-to-group-elements-tp28129357p28139659.html
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: SVGGraphics2D how to group elements

Posted by th...@kodak.com.
Hi Olivier,

kpu <ol...@gmail.com> wrote on 04/03/2010 04:32:26 PM:

>  I wish grouped elements "by component".

> I try  create a <g> element and call
> method  getDOMTreeManager().appendGroup(element_g, getDomGroupManager())

   You need to call 'getRoot(element_g)' this will append everything
drawn since the graphics was constructed, or the last time getRoot was
called as children of 'element_g'.

>       m1.draw(svgGenerator);

        svgGenerator.getRoot(g1);

>       m2.draw(svgGenerator);

        svgGenerator.getRoot(g2);

>       // Finally, stream out SVG to the standard output using
>       // UTF-8 encoding.

    You will need to manage the streaming of the SVG yourself.
There are many options for this but two from Batik are
batik.svggen.XmlWriter or I think a little better is
bati.dom.DOMUtilities.