You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Cameron McCormack <ca...@mcc.id.au> on 2011/01/14 01:15:11 UTC

Re: Batik and Rhino

Hi Jyoti.

Jyoti Gandhe:
> I am using Batik for rendering SVG in my java application. The SVG
> is interactive and responds to mouse events. I have programmed
> mouse listeners in JavaScript using Rhino) to execute . I am using
> JavaScript for listener code instead of Java so that I can reuse the
> SVG file in an HTML page without having to rewrite anything. My SVG
> files looks like -
>
> <svg onload="init(evt)">
> 
> 	<script>
>     		var svgDocument;
>     
>    		function init(evt) {
>         		svgDocument = evt.target.ownerDocument;
>     		}
>             
> 	</script>
> 
> .....
> 
> </svg>
> 
> I have a few questions about using JavaScript with Batik - 
> 
> 1. My JavaScript code declares functions. Is it possible to invoke
> JavaScript functions from my java class? For instance, can I invoke
> the function 'init' from a Java class.

Yes.  You can do this by evaluating some JavaScript in the context of
the document that is loaded in your JSVGCanvas.  You will have to
subclass the JSVGCanvas to get access to it, though, and that’s a bit
convoluted.

See this thread:

  http://web.archiveorange.com/archive/v/FI71MNKfN7TAqvWbnHcx

You’re not working with an applet, but the discussion of how to extend
JSVGCanvas (and various other classes) to get access to the right
information should get you there.

(It should be easier to evaluate some script using a method directly on
JSVGCanvas, I agree…)

> 2. Occasionally, I need to pass data from my JavaScript functions to
> my java class. Is it possible to invoke methods of my Java object from
> my JavaScript?

Yes, if you have a reference to the Java object in your JavaScript, then
you can just call the method on it.  See in the example I linked to the
bindObject() call – you could use this to create a global variable in
your JavaScript that is a reference to your Java object.

Or you could pass it in as an argument to a JavaScript function that you
call from Java.

> 3. I have a version of my application that uses java for mouse
> listeners. It is much faster than the JavaScript prototype. I suspect
> performance issues with Rhino. Are there tips and tricks to improve
> performance when using Rhino and batik together.

The only thing I can think of is whether Rhino is running in intrepreted
or compiled mode.  If you look in script/rhino/RhinoInterpreter.java,
there is:

            if (rhinoClassLoader == null) {
                cx.setOptimizationLevel(-1);
            }

which should mean it uses interpreted mode if you’re in an applet or
some other context where it can’t define classes with byte code
directly.  I don’t know what the default optimization level is, but I
would hope that it would be >= 0, which means compiled.

Nothing else comes to mind.

> 4. I understand that Rhino ships with JDK 6 as the default JavaScript
> execution engine for javax.Script . Is it possible to use the Rhino
> that ships with JDK instead of js.jar that ships with Batik.

Perhaps!  I’ve never tried.  You would need to write your own
org.apache.batik.script.Interpreter{,Factory} implementations and
register them using the Service Provider Interface.  I guess they would
look pretty much the same as the ones in org.apache.batik.script.rhino
but using the JDK class names (which I can’t remember off the top of my
head – something like javax.script.rhino).

> 5. Is it common to use Batik + Rhino for creating interactive SVG in
> a java application. If so, can you share your experience with me. Are
> there any tutorials or sample application I can look at for help.

Some people do use it this way.  I don’t think I have an easy example
application to show you, though, sorry.

-- 
Cameron McCormack ≝ http://mcc.id.au/

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


RE: Batik and Rhino

Posted by Lucan1d <lu...@hotmail.com>.
>> 4. I understand that Rhino ships with JDK 6 as the default JavaScript
>> execution engine for javax.Script . Is it possible to use the Rhino
>> that ships with JDK instead of js.jar that ships with Batik.

> Perhaps!  I’ve never tried.  You would need to write your own
> org.apache.batik.script.Interpreter{,Factory} implementations and
> register them using the Service Provider Interface.  I guess they would
> look pretty much the same as the ones in org.apache.batik.script.rhino
> but using the JDK class names (which I can’t remember off the top of my
> head – something like javax.script.rhino).

Yes, it certainly is possible - I have done it.  I wanted to reduce the JAR size for use of Batik in an applet and was successful at this by eliminating the need for a separate Rhino JAR.

-JCT


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