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 Gary Frederick <ga...@jsoft.com> on 2004/02/13 15:29:16 UTC

Example code for Batik

Howdy,

I'm finally starting to work with Batik and Java. I got a version from 
CVS, built it and ran the tests. I copied an example and built and ran 
that. Now I am ready to take over the world
   or at least my part ;-)

I have some SVG that uses JavaScript to move shapes around. I want to do 
the same with Java. My Java is rusty. I would like a complete example or 
two that included events like mousedown. Any pointers?

Thanks,

Gary

Here is a JavaScript version of a program that adds shapes when you 
click the mouse.

<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
   "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="800" height="400" onload="init(evt)"
      xmlns="http://www.w3.org/2000/svg"
      xmlns:xlink="http://www.w3.org/1999/xlink">
   <title>test adding blocks with mouse</title>
   <desc>copy the shape where the mouse is clicked
         mousedown - get location, add shape</desc>
   <script type="text/ecmascript">
     <![CDATA[
       var svgns = "http://www.w3.org/2000/svg";
       var SVGDoc;
       var mousetrap; // used to trap some mouse events
       var firstShape;

       function init(evt) {
         // Get the Document
         SVGDoc = evt.getTarget().getOwnerDocument();
         firstShape = SVGDoc.getElementById("firstShape");
         mousetrap = SVGDoc.getElementById("trap");
         mousetrap.addEventListener("mousedown", mousedown, false);
       }

       function mousedown(evt){
         var x =  parseInt(evt.clientX);
         var y = parseInt(evt.clientY);
         var aRect = SVGDoc.createElementNS(svgns, "rect");
         aRect.setAttributeNS(null, "width", "42");
         aRect.setAttributeNS(null, "height", "24");
         aRect.setAttributeNS(null, "x", x);
         aRect.setAttributeNS(null, "y", y);
         aRect.setAttributeNS(null, "fill", "lightgreen");
         firstShape.parentNode.appendChild(aRect);
       }
      ]]>
    </script>
   <rect id="trap" x="0" y="0" width="100%" height="100%"
         fill-opacity="0.2" fill="lightblue"/>
   <g id="firstShape" transform="translate(100 100)">
     <title>first rect</title>
     <rect x="0" y="0" rx="5" width="60" height="28" fill="lightgreen"/>
     <text x="4" y="14">first rect</text>
   </g>
</svg>


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


onclick in Java Re: Example code for Batik

Posted by Gary Frederick <ga...@jsoft.com>.
Here is what I have so far:
   http://www.jsoft.com/ts/tools/jscode/simpleEditor/MouseEvent2.java
Note: The location is temporary.

I took the SVG below and removed the <script> and the init() in the 
<svg> tag to test. It works!!!

Now to start adding to it.

Uhhh... My Java is rusty. It took about a day for the memory to start 
coming back ;-) Any comments about what I did wrong or could have done 
better are gratefully accepted.

Gary

 From the comments in the file.
==============
    This adds a SVG shape where you click the mouse. It is based on the
    Batik JSVGCanvas tutorial
      http://xml.apache.org/batik/svgcanvas.html
    combined with the event code from the Scripting with Java tutorial
      http://xml.apache.org/batik/javaScripting.html

    I also got info from here:
      http://xml.apache.org/batik/domapi.html
 
http://xml.apache.org/batik/javadoc/org/apache/batik/dom/events/DOMMouseEvent.html
    There was some good info from YAGE: Yet Another Graphical Editor
      http://www.hta-bi.bfh.ch/Projects/gedit1/docbkx_html/book1.html
      http://www.hta-bi.bfh.ch/Projects/gedit1/docbkx_html/x3649.html

    I got the latest from CVS
    put this file in <batik>/source/com/jsoft/jscode/test
    and ran ./build.sh runtest com.jsoft.jscode.test.MouseEvent2
    Woo Hoo!!!
    The application runs and I can load my SVG file and begin
    adding shapes where I click.
==============



Gary Frederick wrote:
> Howdy,
> 
> I'm finally starting to work with Batik and Java. I got a version from 
> CVS, built it and ran the tests. I copied an example and built and ran 
> that. Now I am ready to take over the world
>   or at least my part ;-)
> 
> I have some SVG that uses JavaScript to move shapes around. I want to do 
> the same with Java. My Java is rusty. I would like a complete example or 
> two that included events like mousedown. Any pointers?
> 
> Thanks,
> 
> Gary
> 
> Here is a JavaScript version of a program that adds shapes when you 
> click the mouse.
> 
> <?xml version="1.0"?>
> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
>   "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
> <svg width="800" height="400" onload="init(evt)"
>      xmlns="http://www.w3.org/2000/svg"
>      xmlns:xlink="http://www.w3.org/1999/xlink">
>   <title>test adding blocks with mouse</title>
>   <desc>copy the shape where the mouse is clicked
>         mousedown - get location, add shape</desc>
>   <script type="text/ecmascript">
>     <![CDATA[
>       var svgns = "http://www.w3.org/2000/svg";
>       var SVGDoc;
>       var mousetrap; // used to trap some mouse events
>       var firstShape;
> 
>       function init(evt) {
>         // Get the Document
>         SVGDoc = evt.getTarget().getOwnerDocument();
>         firstShape = SVGDoc.getElementById("firstShape");
>         mousetrap = SVGDoc.getElementById("trap");
>         mousetrap.addEventListener("mousedown", mousedown, false);
>       }
> 
>       function mousedown(evt){
>         var x =  parseInt(evt.clientX);
>         var y = parseInt(evt.clientY);
>         var aRect = SVGDoc.createElementNS(svgns, "rect");
>         aRect.setAttributeNS(null, "width", "42");
>         aRect.setAttributeNS(null, "height", "24");
>         aRect.setAttributeNS(null, "x", x);
>         aRect.setAttributeNS(null, "y", y);
>         aRect.setAttributeNS(null, "fill", "lightgreen");
>         firstShape.parentNode.appendChild(aRect);
>       }
>      ]]>
>    </script>
>   <rect id="trap" x="0" y="0" width="100%" height="100%"
>         fill-opacity="0.2" fill="lightblue"/>
>   <g id="firstShape" transform="translate(100 100)">
>     <title>first rect</title>
>     <rect x="0" y="0" rx="5" width="60" height="28" fill="lightgreen"/>
>     <text x="4" y="14">first rect</text>
>   </g>
> </svg>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org

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