晚上好,
我目前正在使用蜡染来直接操纵DOM文档svg,从而在Java中处理图形应用程序(处理svg文件).
我的各种元素都在“ symbol”标签中声明,标签由“ use”使用和/或显示.
这是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>
我在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);
在一个区域上(与组元素g匹配)
Element elt = doc.getElementById("nameZone1");
EventTarget t = (EventTarget)elt;
t.addEventListener(SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE, new EObject(), false);
EObject类实现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();
}
}
当单击对象(理论上是节点使用)时,handleEvent函数将返回元素组g(id =“ nameZone1).
我想检索与单击的项目相对应的项目“使用”.
当我删除此代码时:
Element elt = doc.getElementById("nameZone1");
EventTarget t = (EventTarget)elt;
t.addEventListener(SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE, new EObject(), false);
单击某个元素不会产生任何效果.
这可能就是为什么getCurrentTarget()引用了g个匹配项.
问题一定是肯定是Event添加项.
再次,如果您有一个想法,这是受欢迎的..
谢谢您的帮助.
解决方法:
定义(尤其是符号)不是使用SVG绘制的.
如果您想将eventListener添加到use元素,则必须为其指定一个ID.
在SVG文件中:
<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" />
然后,您可以获取它doc.getElementById(“ use3430”);
问候