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 George Armhold <ar...@taproot.rutgers.edu> on 2001/08/16 17:11:03 UTC

trouble setting attributes with SVGGraphics2D

Hello,

I'm having trouble setting width and height attributes while using
SVGGraphics2D.  It seems that the default width and height get set to
"2147483647" when using SVGGraphics2D.  When I try to set these
attributes by manipulating the DOM I get strange results.  In the
following code, I attempt to set the attributes by hand.  However when
run (with Batik 1.0, JDK 1.3.0, RedHat Linux 7.1) my changes never
appear in the resulting XML output.



import java.io.*;
import java.awt.*;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.*;

public class SVGTest {

  public static void main(String[] args) {
    DOMImplementation impl =
SVGDOMImplementation.getDOMImplementation();
    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    Document doc = impl.createDocument(svgNS, "svg", null);

    // get the root element (the svg element)
    Element svgRoot = doc.getDocumentElement();

    // set the width and height attribute on the root svg element
    svgRoot.setAttributeNS(svgNS, "width", "1024");
    svgRoot.setAttributeNS(svgNS, "height", "768");


    // Create an instance of the SVG Generator
    SVGGraphics2D g = new SVGGraphics2D(doc);

    // set background to white
    g.setColor(Color.white);
    g.fill(new Rectangle(0, 0, 1024, 768));
    g.setColor(Color.red);
    g.fill(new Rectangle(100, 100, 100, 100));

    try {
      FileWriter writer = new FileWriter(new File("svgtest.xml"));
      g.stream(writer, false);
      writer.flush();
      writer.close();
    } catch (IOException e) {
      System.err.println(e.getMessage());
      System.exit(1);
    }

    System.exit(0);
  }

}


Also, I notice that I need to explicitly call System.exit(), else my
main never exits... are there lingering threads in the batik code?

Thanks for your help.

PS: I'd like to say thanks to all the Batik developers; this is a
great package!  I've been meaning to send a "thank you" email to you
guys ever since the JavaOne talk back in June.  Great job!



--
George Armhold
Rutgers University
Bioinformatics Initiative

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


Re: trouble setting attributes with SVGGraphics2D

Posted by "Gerard.Davison" <Ge...@oracle.com>.
You should be able to just use the following lines instead of playing with the
DOM:

      // Create an instance of the SVG Generator
      SVGGraphics2D svgGenerator = new SVGGraphics2D(document);


      //

      svgGenerator.setSVGCanvasSize(
         new Dimension((int)rect.width, (int)rect.height));

Hope this helps, don't know what the other bugs are about,

G.


George Armhold wrote:

> Hello,
>
> I'm having trouble setting width and height attributes while using
> SVGGraphics2D.  It seems that the default width and height get set to
> "2147483647" when using SVGGraphics2D.  When I try to set these
> attributes by manipulating the DOM I get strange results.  In the
> following code, I attempt to set the attributes by hand.  However when
> run (with Batik 1.0, JDK 1.3.0, RedHat Linux 7.1) my changes never
> appear in the resulting XML output.
>
> import java.io.*;
> import java.awt.*;
> import org.apache.batik.svggen.SVGGraphics2D;
> import org.apache.batik.dom.svg.SVGDOMImplementation;
> import org.w3c.dom.DOMImplementation;
> import org.w3c.dom.*;
>
> public class SVGTest {
>
>   public static void main(String[] args) {
>     DOMImplementation impl =
> SVGDOMImplementation.getDOMImplementation();
>     String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
>     Document doc = impl.createDocument(svgNS, "svg", null);
>
>     // get the root element (the svg element)
>     Element svgRoot = doc.getDocumentElement();
>
>     // set the width and height attribute on the root svg element
>     svgRoot.setAttributeNS(svgNS, "width", "1024");
>     svgRoot.setAttributeNS(svgNS, "height", "768");
>
>     // Create an instance of the SVG Generator
>     SVGGraphics2D g = new SVGGraphics2D(doc);
>
>     // set background to white
>     g.setColor(Color.white);
>     g.fill(new Rectangle(0, 0, 1024, 768));
>     g.setColor(Color.red);
>     g.fill(new Rectangle(100, 100, 100, 100));
>
>     try {
>       FileWriter writer = new FileWriter(new File("svgtest.xml"));
>       g.stream(writer, false);
>       writer.flush();
>       writer.close();
>     } catch (IOException e) {
>       System.err.println(e.getMessage());
>       System.exit(1);
>     }
>
>     System.exit(0);
>   }
>
> }
>
> Also, I notice that I need to explicitly call System.exit(), else my
> main never exits... are there lingering threads in the batik code?
>
> Thanks for your help.
>
> PS: I'd like to say thanks to all the Batik developers; this is a
> great package!  I've been meaning to send a "thank you" email to you
> guys ever since the JavaOne talk back in June.  Great job!
>
> --
> George Armhold
> Rutgers University
> Bioinformatics Initiative
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org

--

   I don't cause bugs, the surrounding code just fails to adapt quickly enough.
   GMDavison@iee.org


Re: trouble setting attributes with SVGGraphics2D

Posted by Thomas Patterson <tp...@telisphere.com>.
I had the same problem.

I also noticed something that I think explains why your originial code
solution does not work.  I don't think that
>     Element svgRoot = doc.getDocumentElement();
is what you want to modify.  If you instead use
g.getRoot(), then this returns the svg node element that you want,
doc.getDocumentElement() does not seem to return the svg node.

I also noticed the "hanging" problem that you mentioned.  My solution was to
get the root element using getRoot() and then writing it out using Xerces,
rather than using the .stream() operation.


-Tom

----- Original Message -----
From: "Dinkar Ganti" <di...@lifeconnect.com>
To: "Batik Users" <ba...@xml.apache.org>
Sent: Thursday, August 16, 2001 9:18 AM
Subject: RE: trouble setting attributes with SVGGraphics2D


> Hi,
>
> I had a similar problem. The fix is a very simple line:
>
>     SVGGraphics2D g = new SVGGraphics2D(doc);
>     g.setSVGCanvasSize(new Dimension(width,height));
>
> Hope this helps.
>
> Thanks
>
> Dinkar
> -----Original Message-----
> From: armhold [mailto:armhold]On Behalf Of George Armhold
> Sent: Thursday, August 16, 2001 11:11 AM
> To: batik-users@xml.apache.org
> Subject: trouble setting attributes with SVGGraphics2D
>
>
> Hello,
>
> I'm having trouble setting width and height attributes while using
> SVGGraphics2D.  It seems that the default width and height get set to
> "2147483647" when using SVGGraphics2D.  When I try to set these
> attributes by manipulating the DOM I get strange results.  In the
> following code, I attempt to set the attributes by hand.  However when
> run (with Batik 1.0, JDK 1.3.0, RedHat Linux 7.1) my changes never
> appear in the resulting XML output.
>
>
>
> import java.io.*;
> import java.awt.*;
> import org.apache.batik.svggen.SVGGraphics2D;
> import org.apache.batik.dom.svg.SVGDOMImplementation;
> import org.w3c.dom.DOMImplementation;
> import org.w3c.dom.*;
>
> public class SVGTest {
>
>   public static void main(String[] args) {
>     DOMImplementation impl =
> SVGDOMImplementation.getDOMImplementation();
>     String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
>     Document doc = impl.createDocument(svgNS, "svg", null);
>
>     // get the root element (the svg element)
>     Element svgRoot = doc.getDocumentElement();
>
>     // set the width and height attribute on the root svg element
>     svgRoot.setAttributeNS(svgNS, "width", "1024");
>     svgRoot.setAttributeNS(svgNS, "height", "768");
>
>
>     // Create an instance of the SVG Generator
>     SVGGraphics2D g = new SVGGraphics2D(doc);
>
>     // set background to white
>     g.setColor(Color.white);
>     g.fill(new Rectangle(0, 0, 1024, 768));
>     g.setColor(Color.red);
>     g.fill(new Rectangle(100, 100, 100, 100));
>
>     try {
>       FileWriter writer = new FileWriter(new File("svgtest.xml"));
>       g.stream(writer, false);
>       writer.flush();
>       writer.close();
>     } catch (IOException e) {
>       System.err.println(e.getMessage());
>       System.exit(1);
>     }
>
>     System.exit(0);
>   }
>
> }
>
>
> Also, I notice that I need to explicitly call System.exit(), else my
> main never exits... are there lingering threads in the batik code?
>
> Thanks for your help.
>
> PS: I'd like to say thanks to all the Batik developers; this is a
> great package!  I've been meaning to send a "thank you" email to you
> guys ever since the JavaOne talk back in June.  Great job!
>
>
>
> --
> George Armhold
> Rutgers University
> Bioinformatics Initiative
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org
>


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


RE: trouble setting attributes with SVGGraphics2D

Posted by Dinkar Ganti <di...@lifeconnect.com>.
Hi,

I had a similar problem. The fix is a very simple line:

    SVGGraphics2D g = new SVGGraphics2D(doc);
    g.setSVGCanvasSize(new Dimension(width,height));

Hope this helps.

Thanks

Dinkar
-----Original Message-----
From: armhold [mailto:armhold]On Behalf Of George Armhold
Sent: Thursday, August 16, 2001 11:11 AM
To: batik-users@xml.apache.org
Subject: trouble setting attributes with SVGGraphics2D


Hello,

I'm having trouble setting width and height attributes while using
SVGGraphics2D.  It seems that the default width and height get set to
"2147483647" when using SVGGraphics2D.  When I try to set these
attributes by manipulating the DOM I get strange results.  In the
following code, I attempt to set the attributes by hand.  However when
run (with Batik 1.0, JDK 1.3.0, RedHat Linux 7.1) my changes never
appear in the resulting XML output.



import java.io.*;
import java.awt.*;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.*;

public class SVGTest {

  public static void main(String[] args) {
    DOMImplementation impl =
SVGDOMImplementation.getDOMImplementation();
    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    Document doc = impl.createDocument(svgNS, "svg", null);

    // get the root element (the svg element)
    Element svgRoot = doc.getDocumentElement();

    // set the width and height attribute on the root svg element
    svgRoot.setAttributeNS(svgNS, "width", "1024");
    svgRoot.setAttributeNS(svgNS, "height", "768");


    // Create an instance of the SVG Generator
    SVGGraphics2D g = new SVGGraphics2D(doc);

    // set background to white
    g.setColor(Color.white);
    g.fill(new Rectangle(0, 0, 1024, 768));
    g.setColor(Color.red);
    g.fill(new Rectangle(100, 100, 100, 100));

    try {
      FileWriter writer = new FileWriter(new File("svgtest.xml"));
      g.stream(writer, false);
      writer.flush();
      writer.close();
    } catch (IOException e) {
      System.err.println(e.getMessage());
      System.exit(1);
    }

    System.exit(0);
  }

}


Also, I notice that I need to explicitly call System.exit(), else my
main never exits... are there lingering threads in the batik code?

Thanks for your help.

PS: I'd like to say thanks to all the Batik developers; this is a
great package!  I've been meaning to send a "thank you" email to you
guys ever since the JavaOne talk back in June.  Great job!



--
George Armhold
Rutgers University
Bioinformatics Initiative

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



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