xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From André Ávila <asan...@nextech.com.br>
Subject Re: Replace color in entire document
Date Thu, 02 Feb 2006 14:16:44 GMT
Hello Thomas, Cameron,

I think I didn't state my problem clearly, sorry for that. I really cannot
change the source, nor do I know exactly which elements have to be updated.
This method should work for any SVG currently displayed in an SVGCanvas. The
idea is a very simple 'replace color' feature: the user selects the original
color and the destination color, clicks 'replace' and the whole SVG gets

Here's the first code I wrote to replace black for white in the entire SVG:

NodeList nodes = document.getElementsByTagName("g");

for(int i = 0; i < nodes.getLength(); i++) {
    if (nodes.item(i) instanceof SVGElement) {
        SVGElement element = (SVGElement) nodes.item(i);
        if (element.getAttribute("stroke").equals("rgb(0,0,0)"));
            element.setAttribute("stroke", "rgb(255,255,255)");
        if (element.getAttribute("fill").equals("rgb(0,0,0)"));
            element.setAttribute("fill", "rgb(255,255,255)");

This is not nearly what one would call 'good code', but it produces the
exact effect that I want. However, it is very slow, so I went to the next
idea, which was to traverse the GVT tree updating each GraphicsNode.

GraphicsNode gvtRoot = svgCanvas.getGraphicsNode();

GVTTreeWalker walker = new GVTTreeWalker(gvtRoot);

GraphicsNode currNode = null;

while ((currNode = walker.nextGraphicsNode()) != null) {
    if (currNode instanceof ShapeNode) {
        ShapeNode sn = (ShapeNode) currNode;

        ShapePainter sp = sn.getShapePainter();

        if (sp instanceof StrokeShapePainter) {
            StrokeShapePainter painter = new
        else if (sp instanceof FillShapePainter) {
            FillShapePainter painter = new FillShapePainter(sn.getShape());

Well, this code doesn't really work, but it was the closest I could get.
Ideally, it should produce the same effect as the first one, but it's far
from it:

1) I couldn't find a way to read the color of the current GraphicsNode. I
didn't use Cameron's code (from the "How do I parse..." thread), because he
grabs the color from an SVGElement, and I don't have one - unless I could
build one from a GraphicsNode, is it possible?

2) I'm not sure that updating the ShapeNodes will cover all the elements in
the SVG. Should I try to cast to other kinds of nodes as well?

3) The Painter update is not working as I would expect. Instead of being
repainted, the nodes that have the Painter updated are disappearing from the
SVG. Odd.

I'm very new to Batik, SVG and most of its concepts, so I'm still confused
about how things are supposed to work. I really appreciate you guys help on

Thank you,


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

View raw message