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,
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]