Have you waited for the GVT tree to render? I don't *think* the CSS and
DOM are available until the GVT tree has rendered. You might want to
attach a listener:
public class GVTListener extends GVTTreeRendererAdapter {
public void gvtRenderingCompleted(GVTTreeRendererEvent gvttrE) {
// Everything should be ready now!
}
}
Just to see if that's the problem, you could call Thread.sleep(...)for a
few seconds before trying to retrieve the color. If that works, then
it's most likely that you need to wait for the GVT tree to render.
Michael Bishop
-----Original Message-----
From: Tom McCallum [mailto:[EMAIL PROTECTED]
Sent: Monday, April 09, 2007 12:05 PM
To: [email protected]
Subject: Re: Another Color Question
Thanks for your reply, I had actually missed the existence of the Wiki
so
thank you for the pointer. I am using a JSVGCanvas though, which is why
I
had also assumed a CSS view would have been initialised.
Currently I do the following (am just playing at the moment!) to create
my
rectangle:
DOMImplementation domImpl
=SVGDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document.
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
SVGDocument doc = (SVGDocument) domImpl.createDocument(svgNS,
"svg", null);
svgCanvas.setSVGDocument(doc); // set the document for the
JSVGCanvas
object
// Get the root element (the 'svg' element).
Element svgRoot = doc.getDocumentElement();
// Set the width and height attributes on the root 'svg'
element.
svgRoot.setAttributeNS(null, "width", "1000");
svgRoot.setAttributeNS(null, "height", "500");
// Create the rectangle.
Element rectangle = doc.createElementNS(svgNS,
SVGConstants.SVG_RECT_TAG );
rectangle.setAttributeNS(null, "x", "10");
rectangle.setAttributeNS(null, "y", "20");
rectangle.setAttributeNS(null,
SVGConstants.SVG_WIDTH_ATTRIBUTE,
"100");
rectangle.setAttributeNS(null,
SVGConstants.SVG_HEIGHT_ATTRIBUTE,
"50");
rectangle.setAttributeNS(null, "fill", "red");
svgRoot.appendChild(rectangle);
I then (sometime later) retrieve the color via:
String color = domElement.getAttributeNS(null,
SVGConstants.SVG_FILL_ATTRIBUTE);
which gives me back a string ("#FF0000").
Now I could assume that this would always be the hex representation as I
am building the tree in the first place, but I would rather know how to
do
this more cleanly taking into account the four color representations:
// fill="cornflowerblue"
// fill="rgb(100, 149, 237)"
// fill="#6495ED"
// fill="aliceblue"
Any ideas?
Tom
On Mon, 09 Apr 2007 16:52:13 +0100, Bishop, Michael W. CONTR J9C880
<[EMAIL PROTECTED]> wrote:
> 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]
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
---------------------------------------------------------------------
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]