我在您的代码中有一些更改。
public class SVGEditor {
public File svgEditFile;
public SVGDocument svgDoc;
public JSVGCanvas svgCanvas;
public SAXSVGDocumentFactory svgDocumentFactory;
public Document doc;
public Vector<String> colors = new Vector<String>();
public SVGEditor(File f, JSVGCanvas canvas){
svgEditFile = f;
svgCanvas = canvas;
svgCanvas.getInteractors().add(this);
}
public void editFile() throws IOException{
String parser = XMLResourceDescriptor.getXMLParserClassName();
svgDocumentFactory = new SAXSVGDocumentFactory(parser);
doc = svgDocumentFactory.createDocument(svgEditFile.toURI().toString());
}
public void pathParser(){
doc.getDocumentElement().normalize();
NodeList listOfPathNodes = doc.getElementsByTagName("path");
int totalPaths = listOfPathNodes.getLength();
System.out.println("Total number of paths: " + totalPaths);
for(int i = 0; i < totalPaths; i++){
Element el = (Element) listOfPathNodes.item(i);
EventTarget target = (EventTarget) el;
target.addEventListener("click",
new org.w3c.dom.events.EventListener() {
public void handleEvent(org.w3c.dom.events.Event evt) {
if(evt.getType().equals("click")){
//Here is where I want to get the clicked path from the interaction with the DOM document
Element el = (Element)evt.getTarget();
System.out.println("Clicked Path:" + el.getElementsByTagName("path"));
}
}
}, false);
// Get id data
String id = el.getAttribute("id");
// Get path data
String path = el.getAttribute("d");
if(id.equals("ro"))
System.out.println(path);
// Get color data
String style = el.getAttribute("style");
int index1 = style.indexOf("fill:#");
String color = style.substring((index1+5),(index1 + 12));
colors.add((id + ":" +color));
}
}
public static void main(String[] args) throws IOException {
System.out.println("SVG start");
JSVGCanvas canvas = new JSVGCanvas();
canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
SVGEditor svgEditor = new SVGEditor(new File(
"C:\\Users\\neda.danilovic\\Desktop\\batik 1.7\\miscPOC-master\\batik-poc1\\src\\main\\java\\glasspane\\Blank_map_of_Europe.svg"), canvas);
svgEditor.editFile();
svgEditor.pathParser();
System.out.println("SVG end");
}
}
package glasspane;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.dom.svg.SVGOMSVGElement;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.svg.AbstractJSVGComponent;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.svg.SVGSVGElement;
public class SvgGlassPaneExample {
private JSVGCanvas canvas;
private JLabel target;
private SAXSVGDocumentFactory svgDocumentFactory;
private Document doc;
private File svgEditFile;
private Vector<String> colors = new Vector<String>();
/** Creates a new instance of SvgGlassPaneExample
* @throws IOException */
public SvgGlassPaneExample(JPanel panel) throws IOException {
panel.removeAll();
canvas = new JSVGCanvas();
canvas.setDocumentState(AbstractJSVGComponent.ALWAYS_DYNAMIC);
File file = new File("C:\\Users\\neda.danilovic\\Desktop\\batik 1.7\\miscPOC-master\\batik-poc1\\src\\main\\java\\glasspane\\Blank_map_of_Europe.svg");
String parser = XMLResourceDescriptor.getXMLParserClassName();
svgDocumentFactory = new SAXSVGDocumentFactory(parser);
canvas.setDocument(svgDocumentFactory.createDocument(file.toURI().toString()));
panel.add(canvas);
panel.repaint();
}
public void addGlassPane() {
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
Document doc = canvas.getSVGDocument();
Element rectangle = doc.createElementNS(svgNS, "rect");
rectangle.setAttributeNS(null, "x", "0");
rectangle.setAttributeNS(null, "y", "0");
rectangle.setAttributeNS(null, "width", "700");
rectangle.setAttributeNS(null, "height", "700");
rectangle.setAttributeNS(null, "style", "fill:none;pointer-events:fill");
rectangle.setAttributeNS(null, "id", "glasspane");
Element svgRoot = doc.getDocumentElement();
svgRoot.insertBefore(rectangle, doc.getElementById("rectangles"));
}
public void registerListeners(JLabel target) {
//this label provides feedback on the selected item
this.target = target;
// Gets an element from the loaded document.
// document is your SVGDocument
NodeList listOfPathNodes = canvas.getSVGDocument().getElementsByTagName("path");
int totalPaths = listOfPathNodes.getLength();
System.out.println("Total number of paths: " + totalPaths);
for(int i = 0; i < totalPaths; i++){
Element el = (Element) listOfPathNodes.item(i);
EventTarget eventTarget = (EventTarget) el;
eventTarget.addEventListener("click",
new EventListener() {
public void handleEvent(Event evt) {
System.out.println("click happend!!!");
}
}, false);
// Get id data
String id = el.getAttribute("id");
// Get path data
String path = el.getAttribute("d");
if(id.equals("ro"))
System.out.println(path);
// Get color data
String style = el.getAttribute("style");
int index1 = style.indexOf("fill:#");
String color = style.substring((index1+5),(index1 + 12));
colors.add((id + ":" +color));
}
}
public class GlassPaneClick implements EventListener {
@Override
public void handleEvent(Event evt) {
target.setText("Glasspane event " + ((Element) evt.getTarget()).getAttribute("id"));
target.repaint();
}
}
public class ObjectClick implements EventListener {
@Override
public void handleEvent(Event evt) {
target.setText("Rectangles event " + ((Element) evt.getTarget()).getAttribute("id"));
target.repaint();
}
}
public void makeDocument() throws IOException{
String parser = XMLResourceDescriptor.getXMLParserClassName();
svgDocumentFactory = new SAXSVGDocumentFactory(parser);
doc = svgDocumentFactory.createDocument(svgEditFile.toURI().toString());
}
}
How to make that click on SVG works?