2

我需要使用 Batik 修改和使用 SVG 地图文件,但我无法修改它。我正在努力做的是修改一个国家的填充颜色值(我可以在解析 SVG 文件后从路径元素中获得)。为此,我需要确定单击哪个国家/地区以获取其其他标识符的 id 以修改正确的填充参数。

这是我的代码:

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
             id = el.getAttribute("id");
             // Get path data
             String path = el.getAttribute("d");
             if(id.equals("ro"))
                 System.out.println(path);
             addPoint(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));

         }
     }
 }

这是 SVG 文件: http: //upload.wikimedia.org/wikipedia/commons/b/b3/Blank_map_of_Europe.svgeditFile() ,您可以在方法 中直接使用此链接。

我希望很容易理解,我想要做的是使用此解决方案或您提供的其他解决方案使用单击事件来查找在文档上修改填充颜色值的位置。

4

3 回答 3

0

我在您的代码中有一些更改。

    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?
于 2014-12-02T13:57:16.070 回答
0

为了获取您单击的国家/地区 ID,您应该注册mousedown事件侦听器。(click不适用于我的蜡染版本)

如果您想了解您点击的是哪个国家/地区:

@Override
public void handleEvent(Event e) {
    Element element = (Element) e.getCurrentTarget();

    String countryISO = element.getAttribute("id");
}
于 2013-01-09T10:57:26.013 回答
0

我也为这个问题苦苦挣扎了几个小时。我终于找到了!事实上,我认为你只缺少这个声明:

svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);

我将我的解决方案上传到 github,可以在这里找到:https ://github.com/fmarot/miscPOC/tree/master/batik-poc1 顺便说一句,使用 Batik 1.7(我尝试过的),“点击”确实有效;)

于 2013-07-10T14:18:21.650 回答