Dan,
Attached are two older postings, which may help you.

Good luck,
Elias.



-----Original Message-----
From: Danilo Costa [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, August 24, 2004 11:23 AM
To: [EMAIL PROTECTED]
Subject: SVG to Java2D


Hi!
I 'm new here!
I need to convert SVG Elements to java2D element such as elipses,
rectangles, lines, how ca n I do that easily? Using GVT?

[]s
Dan

-- 
"...YOU CANNOT KNOW THE MEANING OF YOUR LIFE UNTIL YOU ARE CONNECTED
WITH THE POWER THAT CREATED YOU..." Shri Mataji Nirmala Devi

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


--- Begin Message ---
Hello:

Peter, thanks for the code, I am sure a lot of us will find it useful.

For the meanwhile, I am using Thomas suggestion (thanks!) to get access 
the the Java2D Shape object in the GVT items and from that i intend to 
build the DXF descriptions. It was really clarifing what the GVT stands 
for, before it was a little confusing.

Thanks you guys, and if i come with something useful for all, i will 
post it.

Andres.

On Tuesday, February 17, 2004, at 06:00  PM, Peter Becker wrote:

> Hi Andres,
>
> I did something similar, but the code is just a quick little hack for 
> a particular subset of SVG (only some elements, limited attributes, no 
> groups, no transforms). That was good enough to use little SVGs as 
> icons in Java. And it was also good enough to export some shapes from 
> OpenOffice into SVG and import them into our programs.
>
> Code is attached, feel free to use it. It is not much anyway.
>
>  Peter
>
>
>
> Andres Toussaint wrote:
>
>> Hello:
>>
>> I am interested to get some guideance in creating Java2D objects from 
>> my
>> JSVGCanvas SVGDocument. The purpose of this is to bridge it into a 
>> Java2D
>> to DXF (autoCad) converter that i am also working on.
>>
>> Ultimately I think I would need to have a SVGtoDXFTranscoder, but i 
>> think
>> having a SVGtoJava2DTranscoder is more useful in terms of using the 
>> Java2D
>> for other formats as well.
>>
>> Thanks in advance,
>>
>> Andres.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>>
>
> import java.awt.Shape;
> import java.awt.geom.*;
> import java.util.Iterator;
> import java.util.StringTokenizer;
>
> import org.jdom.Element;
>
> /**
>  * This class imports simple SVG images into AWT Shape objects.
>  *
>  * Supported are: <rect>, <circle>, <ellipse>, <line>, <polyline>, 
> <polygon>.
>  *
>  * Styles are ignored, transformations are ignored. Group elements are
>  * processed via recursion, but ignored otherwise, the result is the 
> same as
>  * if the group elements have been removed before. The methods work 
> for simple
>  * SVG, but have not been tested on anything complex.
>  *
>  * @todo adding path would greatly enhance this
>  * @todo add tests
>  */
> public class SVG2Shape {
>       public static Shape importShape(Element svgElement) {
>         GeneralPath shape = importShapeUncentered(svgElement);
>         return centerShape(shape);
>       }
>
>     private static GeneralPath importShapeUncentered(Element 
> svgElement) {
>         GeneralPath shape = new GeneralPath();
>         Iterator it = svgElement.getChildren().iterator();
>         while(it.hasNext()) {
>               Element cur = (Element) it.next();
>               if(cur.getName().equals("rect")) {
>                   double x = Double.parseDouble(cur.getAttributeValue("x"));
>                   double y = Double.parseDouble(cur.getAttributeValue("y"));
>                   double width = 
> Double.parseDouble(cur.getAttributeValue("width"));
>                   double height = 
> Double.parseDouble(cur.getAttributeValue("height"));
>                       shape.append(new Rectangle2D.Double(x, y, width, 
> height),false);
>             } else if(cur.getName().equals("circle")) {
>                   double cx = 
> Double.parseDouble(cur.getAttributeValue("cx"));
>                   double cy = 
> Double.parseDouble(cur.getAttributeValue("cy"));
>                   double r = Double.parseDouble(cur.getAttributeValue("r"));
>                   shape.append(new Ellipse2D.Double(cx - r, cy - r, 2*r, 
> 2*r),false);
>             } else if(cur.getName().equals("ellipse")) {
>                 double cx = 
> Double.parseDouble(cur.getAttributeValue("cx"));
>                 double cy = 
> Double.parseDouble(cur.getAttributeValue("cy"));
>                 double rx = 
> Double.parseDouble(cur.getAttributeValue("rx"));
>                 double ry = 
> Double.parseDouble(cur.getAttributeValue("ry"));
>                 shape.append(new Ellipse2D.Double(cx - rx, cy - ry, 
> 2*rx, 2*ry),false);
>             } else if(cur.getName().equals("line")) {
>                 double x1 = 
> Double.parseDouble(cur.getAttributeValue("x1"));
>                 double y1 = 
> Double.parseDouble(cur.getAttributeValue("y1"));
>                 double x2 = 
> Double.parseDouble(cur.getAttributeValue("x2"));
>                 double y2 = 
> Double.parseDouble(cur.getAttributeValue("y2"));
>                 shape.append(new Line2D.Double(x1, y1, x2, y2),false);
>             } else if(cur.getName().equals("polyline") || 
> cur.getName().equals("polygon")) {
>               StringTokenizer tokenizer = new 
> StringTokenizer(cur.getAttributeValue("points"), " ,");
>               boolean first = true;
>               while(tokenizer.hasMoreElements()) {
>                   float x = Float.parseFloat(tokenizer.nextToken());
>                   float y = Float.parseFloat(tokenizer.nextToken());
>                   if(first) {
>                       shape.moveTo(x,y);
>                       first = false;
>                   } else {
>                       shape.lineTo(x,y);
>                   }
>               }
>               if(cur.getName().equals("polygon")) {
>                       shape.closePath();
>               }
>             } else if(cur.getName().equals("g")) {
>               shape.append(importShapeUncentered(cur),false);
>               }
>         }
>         return shape;
>     }
>       
>     private static Shape centerShape(Shape shape) {
>       Rectangle2D bounds = shape.getBounds2D();
>         double xOffset = bounds.getX() - bounds.getWidth()/2;
>         double yOffset = bounds.getY() - bounds.getHeight()/2;
>         return AffineTransform.getTranslateInstance(xOffset, 
> yOffset).createTransformedShape(shape);
>     }
>
>     public static Shape importShape(Element svgElement, double width, 
> double height) {
>       Shape untransformedShape = importShape(svgElement);
>         float scaleX = (float) (width / 
> untransformedShape.getBounds2D().getWidth());
>         float scaleY = (float) (height / 
> untransformedShape.getBounds2D().getHeight());
>         AffineTransform transform;
>         if(scaleX > scaleY) {
>               transform = AffineTransform.getScaleInstance(scaleY, scaleY);
>         } else {
>             transform = AffineTransform.getScaleInstance(scaleX, 
> scaleX);
>         }
>         return transform.createTransformedShape(untransformedShape);
>     }
> }
>
> ---------------------------------------------------------------------
> 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]



--- End Message ---
--- Begin Message ---
Hi:

My suggestion is to read the Java Graphics 2D information you need from 
the GVT tree.

Once you load a SVGDocument into a JSCGComponent (you do not need a 
canvas to access the GVT), you can access the GVT by adding a 
GVTTreeBuilderListener to your SVGComponent, in the gvtBuildCompleted 
event.

Once you have the GVT tree, you can walk through it, and retrieve the 
Graphics2D representation of your SVG file.

I use this method to feed a DXF exporter Library that requires Graphics 
2D objects to transform into DXF and it works perfectly.

Basically:

public SomeMethod() {

             JSVGComponent  svg = new JSVGComponent();

             svg.addSVGDocumentLoaderListener(new 
SVGDocumentLoaderAdapter() {
                 public void 
documentLoadingStarted(SVGDocumentLoaderEvent e) {
                 }
                 public void 
documentLoadingCompleted(SVGDocumentLoaderEvent e) {
                 }
             });
             svg.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
                 public void gvtBuildStarted(GVTTreeBuilderEvent e) {
                 }
                 public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
                // Now the tree is ready, we have to walk through it
                     GVTTreeWalker tw = new 
GVTTreeWalker(e.getGVTRoot());
                     GraphicsNode node = tw.firstChild();

                ////////////////////////////////////////////////////////////////
                ////////////////////////////////////////////////////////////////
                     // Walk through the tree to locate and extract the 
Graphics info you want.
                ////////////////////////////////////////////////////////////////
                ////////////////////////////////////////////////////////////////

                 }
             });

             try {
                 svg.loadSVGDocument(f.toURL().toString());
             }
             catch(Exception e){
                 System.out.println("Could not load SVG file into 
Component.  "+e);

             }
}


On Jun 23, 2004, at 12:48 PM, Arek Stryjski wrote:

> Hi,
>
> I'm wondering that is the simplest way to change SVG into Java Image.
>
> I will need to change XML fragment (one of org.w3c.dom.svg.SVGElement 
> returned
> from org.w3c.dom.events.Event.getTarget()) into java.awt.Image
> In most cases the produced Image will be kind of Icon for element. I 
> don't need
> to save it in any format, I will use it in other swing components.
>
> I was looking at org.apache.batik.transcoder and one way I see, is to 
> implement
> my own ImageTranscoder, TranscoderInput and TranscoderOutput
> However maybe there are some ready classes that could help me to do 
> part of this
> job, or maybe there is some other place in Batik I should look for 
> solution for
> this.
>
> Best regards
> Arek
>
>
> ---------------------------------------------------------------------
> 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]




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

Reply via email to