xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tarjei <tar...@bergfald.no>
Subject How can I use both the dom and 2D APIs at the same time?
Date Mon, 14 Apr 2008 17:18:21 GMT
Hi, I am a new Batik user.

I'm trying to use Batik to create a SVG document where I got an image in
the background and a set of boxes in front of the image designating
regions in the image.

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.

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,

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(),
    	svg.drawImage(image.getImage(),0,0, image.getWidth(),
image.getHeight(), null);

    public void addRegions(Collection<Region> regions, int colorInt) {
    	String style = "stroke:"+
    	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.

    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) {
		} catch (FileNotFoundException e) {
		} catch (SVGGraphics2DIOException e) {


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

View raw message