Are you using a JSVGCanvas?  You might want to check the wiki on the
Batik site about booting CSS and DOM.  JSVGCanvas does this for you.  If
you don't use one, you have to do it manually.  Even though you're not
setting CSS values for color, a CSS is "computed" through the magic of
Batik (I'm sure someone can explain this much better) and you need these
pieces loaded to use that functionality:

http://wiki.apache.org/xmlgraphics-batik/BootSvgAndCssDom

Michael Bishop



-----Original Message-----
From: Tom McCallum [mailto:[EMAIL PROTECTED] 
Sent: Monday, April 09, 2007 10:12 AM
To: [email protected]
Subject: Another Color Question

 From a previous post I found I have a function to get the colour of an

element as follows:

    public static Color getColor(Element element, String attribute) {
         Color returnColor = null;
         final Document document = element.getOwnerDocument();
         if ( document != null ) {
             //System.out.println("doc.getDocumentElement:  
"+document.getDocumentElement().getClass().toString());
             final ViewCSS viewCSS = (ViewCSS)  
document.getDocumentElement();
             if ( viewCSS != null ) {
                 final CSSStyleDeclaration computedStyle =
                         viewCSS.getComputedStyle(element, null);
                 if ( computedStyle != null ) {
                     System.out.println("CSS Text:  
"+computedStyle.getCssText());
                     final SVGPaint svgPaint =
                             (SVGPaint)  
computedStyle.getPropertyCSSValue(attribute);
                     if ( svgPaint != null ) {
                         if (svgPaint.getPaintType() ==  
SVGPaint.SVG_PAINTTYPE_RGBCOLOR) {
                             final RGBColor rgb =
svgPaint.getRGBColor();
                             final float red =  
rgb.getRed().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE);
                             final float green =  
rgb.getGreen().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE);
                             final float blue =  
rgb.getBlue().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE);
                             returnColor = new Color(red / 255, green /

255, blue / 255);
                         }
                     }
                 }
             }
         }
         return returnColor;
     }

I have a simple SVG which creates a coloured rectangle:

<?xml version="1.0" encoding="UTF-8"?><svg  
xmlns:xlink="http://www.w3.org/1999/xlink";  
xmlns="http://www.w3.org/2000/svg"; contentScriptType="text/ecmascript"  
width="1000" zoomAndPan="magnify" contentStyleType="text/css"
height="500"  
preserveAspectRatio="xMidYMid meet" version="1.0"><rect fill="#ff0000"  
x="100" width="100" y="100" height="50"/></svg>

Unfortunately I cannot get the colour of the rectangle back to a  
java.awt.Color object, as I get the following exception:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
         at
org.apache.batik.css.dom.CSSOMComputedStyle.getCssText(Unknown  
Source)
         at ataglancedemo.SVGUtils.getColor(SVGUtils.java:56)
         at ataglancedemo.GlanceRect.getBackground(GlanceRect.java:93)
         at ataglancedemo.MainFrame.test4(MainFrame.java:84)
         at ataglancedemo.MainFrame.<init>(MainFrame.java:213)
         at ataglancedemo.Main$1.run(Main.java:29)
         at  
java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
         at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
         at  
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThrea
d.java:242)
         at  
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.
java:163)
         at  
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
         at  
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
         at
java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Can anyone tell me how to get the background color in all circumstances,

i.e. when it is just an attribute or if as a CSS?

Many thanks

Tom

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

Reply via email to