晚上好,
我目前正在 Java 中开发图形应用程序(处理 svg 文件),使用 batik 直接操作 DOM 文档 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”)的组。
我想检索与单击的项目相对应的项目“use”。
当我删除此代码时:
Element elt = doc.getElementById("nameZone1");
EventTarget t = (EventTarget)elt;
t.addEventListener(SVGConstants.SVG_MOUSEDOWN_EVENT_TYPE, new EObject(), false);
点击一个元素不会产生任何结果。
这可能就是为什么 getCurrentTarget() 我引用 g 匹配项。
问题肯定是事件添加项目。
再次,如果您有想法,欢迎..
感谢您的帮助。
最佳答案
定义,尤其是符号不是用 SVG 绘制的。
如果您想向 use 元素添加 eventListener,则必须为其指定一个 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");
问候
关于java - 使用 DOM/batik 进行事件点击 : Recovery the use tag which refers a symbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31271972/