Event clic with DOM / batik : Recovery the use tag

2020-04-11 09:37发布

Good evening,

I am currently working on a graphics application (handling a svg file) in Java with the use of batik to directly manipulate DOM document svg.

My various elements are declared in "symbol" tags are used and / displayed by the tags' "use ". Here the document svg:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentScriptType="text/ecmascript" width="100%" zoomAndPan="magnify" contentStyleType="text/css" height="100%" preserveAspectRatio="xMidYMid meet" version="1.0">

    <defs>
        <g id="module-list">
            <symbol preserveAspectRatio="xMidYMid meet" id="chaise_1.1.2" version="1.1.2" viewBox="0 0 200 256" module="chaine">
                <polygon fill="inherit" clip-rule="evenodd" fill-rule="evenodd" points="184.055,256.09 184.055,256.09 184.055,148.195 199.135,148.195 199.135,256.09  "/>
                <polygon fill="inherit" clip-rule="evenodd" fill-rule="evenodd" points="83.006,201.214 83.006,201.214 83.006,187.532 182.656,187.532       182.656,201.214  "/>
                <polygon fill="inherit" clip-rule="evenodd" fill-rule="evenodd" points="83.006,169.963 83.006,169.963 83.006,149.286 182.656,149.286       182.656,169.963  "/>
                <path fill="inherit" clip-rule="evenodd" d="m94.664,133.266L94.664,133.266c8.183-2.792,23.189-5.077,45.008-6.836      c21.818-1.76,38.142-1.219,48.972,1.631c10.831,2.85,16.246,9.305,16.246,19.354H82.382      C82.382,140.779,86.473,136.071,94.664,133.266z" fill-rule="evenodd"/>
                <path fill="inherit" clip-rule="evenodd" d="m55.951,25.838c-5.393-15.133-5.964-23.633-1.714-25.497      c7.672-1.866,13.17,6.633,16.486,25.497c7.25,35.553,10.885,69.858,10.885,102.921v127.33H66.369l0.308-126.706      C66.677,96.004,63.104,61.497,55.951,25.838z" fill-rule="evenodd"/>
            </symbol>
        </g>
    </defs>
    <g id="plan-list">
        <g id="nameZone1">
            <rect fill="#000000" x="0" width="500" height="500" y="0"/>
            <use x="50" y="20" fill="#F5A9D0" width="20" xlink:href="#chaise_1.1.2" xlink:type="simple" xlink:actuate="onLoad" height="200" xlink:show="embed"/>
            <use x="50" y="60" width="20" xlink:href="#chaise_1.1.2" xlink:type="simple" xlink:actuate="onLoad" height="200" xlink:show="embed"/>
        </g>
        <g id="nameZone2">
            <rect fill="#0000FF" x="500" width="500" height="500" y="0"/>
            <use x="550" y="20" width="20" xlink:href="#chaise_1.1.2" xlink:type="simple" xlink:actuate="onLoad" height="200" xlink:show="embed"/>
            <use x="550" y="60" width="20" xlink:href="#chaise_1.1.2" xlink:type="simple" xlink:actuate="onLoad" height="200" xlink:show="embed"/>
        </g>
    </g>
</svg>

I added an event on an element of the svg:

((EventTarget) objAdd.getNodeUse()).addEventListener( SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE, new EObject(), false);
((EventTarget) objAdd.getNodeDefs()).addEventListener( SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE, new EObject(), false);

and on a zone (match with group elements g)

Element elt = doc.getElementById("nameZone1");
EventTarget t = (EventTarget)elt;
t.addEventListener(SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE, new EObject(), false);

The class EObject implements org.w3c.dom.events.EventListener:

import org.w3c.dom.Element;
import org.w3c.dom.events.Event;

public class EObject implements org.w3c.dom.events.EventListener
{
    public void handleEvent(Event evt) 
    {
        System.out.println("YOUPIIII JE SUIS CLIQUE");  
        Element e = (Element) evt.getCurrentTarget();
    }
}

When a click on an object (theoretically a node use), the handleEvent function return the group of element g (id="nameZone1).

I would like to retrieve the item "use" corresponding to the clicked item.

When I remove this code :

Element elt = doc.getElementById("nameZone1");
    EventTarget t = (EventTarget)elt;
    t.addEventListener(SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE, new EObject(), false);

a click on an element engenders nothing.

This is probably why getCurrentTarget () me the references g matching items.

The problem must certainly be definitely Event add the item.

Again if you have an idea, it is welcome ..

Thank you for your help.

1条回答
混吃等死
2楼-- · 2020-04-11 10:09

Defs and especially symbols are not drawn with SVG.
If you like to add an eventListener to an use element you have to give it an id.
In SVG file:

<use id="use3430" xlink:show="embed" height="200" xlink:actuate="onLoad" xlink:type="simple" xlink:href="#chaise_1.1.2"
            width="20" y="20" x="550" />

Then you can get it doc.getElementById("use3430");

Greetings

查看更多
登录 后发表回答