简体   繁体   English

JavaGraphics用蜡染画成svg

[英]JavaGraphics to svg with batik

Can somebody help me with exporting Java graphics to SVG? 有人可以帮助我将Java图形导出到SVG吗? I know how to export Java graphics to SVG, but need to define a box in which symbol is placed so I can use it for styling in geotools. 我知道如何将Java图形导出到SVG,但是需要定义一个放置符号的框,以便可以在geotools中使用它进行样式设置。 A problem with function is that I get symbol placed on the top left (0,0) so only one-quarter of the sign is shown in geotools, while in QGIS its shown perfectly. 函数存在的问题是,我将符号放置在左上角(0,0)上,因此在geotools中仅显示符号的四分之一,而在QGIS中则完美地显示了该符号。 I am posting some example that I used for this 我发布了一些我为此使用的示例

draw function 绘图功能

  public void draw(Graphics g){
    Graphics2D g2 = (Graphics2D) g;
    float r = 5*_f;
    // circle
    crtajPodlogu(g2,new Ellipse2D.Float(_x-r,_y-r,2*r,2*r));
    //point in middle 
    g2.fill(new Ellipse2D.Float(_x-r/5,_y-r/5,2*r/5,2*r/5));
    g2.draw(new Ellipse2D.Float(_x-r/5,_y-r/5,2*r/5,2*r/5));
    // vert. line
    g2.draw(new Line2D.Float(_x,_y+r,_x,_y-5*r/2));
    //hor. line
    g2.draw(new Line2D.Float(_x,_y-2*r,_x+r,_y-2*r));
    //arrow
    g2.draw(new Line2D.Float(_x+r,_y-2*r,_x+r/2,_y-5*r/2));
    g2.draw(new Line2D.Float(_x+r,_y-2*r,_x+r/2,_y-3*r/2));
  }

convert function 转换功能

        // Get a DOMImplementation.
        DOMImplementation domImpl =
          GenericDOMImplementation.getDOMImplementation();

        // Create an instance of org.w3c.dom.Document.
        String svgNS = "http://www.w3.org/2000/svg";
        Document document = domImpl.createDocument(svgNS, "svg", null);
        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
        String[] myStrings = new String[] {
                "symbolForSvg",
                };
        List mylist = Arrays.asList(myStrings );
        Iterator itr= mylist.iterator();
        while(itr.hasNext()){
            try {
                Class clazz = Class.forName("key."+itr.next().toString());
                java.lang.reflect.Method method = clazz.getMethod("draw", Graphics.class);
                method.invoke(clazz.newInstance(), svgGenerator);
                // Finally, stream out SVG to the standard output using
                // UTF-8 encoding.
                //boolean useCSS = true; // we want to use CSS style attributes
                Writer out = null;
                System.out.println(clazz.getSimpleName());
                out =  new BufferedWriter(new OutputStreamWriter(
                          new FileOutputStream("tkey/"+clazz.getSimpleName()+".svg"), "utf-8"));
                svgGenerator.stream(out, false);
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException 
                    | SecurityException | IllegalArgumentException | InvocationTargetException | UnsupportedEncodingException | FileNotFoundException | SVGGraphics2DIOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();

        }
        }

the svg that I get with this 我得到的这个SVG

<?xml version="1.0"?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
          'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg xmlns:xlink="http://www.w3.org/1999/xlink" style="fill-opacity:1; color-rendering:auto; color-interpolation:auto; text-rendering:auto; stroke:black; stroke-linecap:square; stroke-miterlimit:10; shape-rendering:auto; stroke-opacity:1; fill:black; stroke-dasharray:none; font-weight:normal; stroke-width:1; font-family:'Dialog'; font-style:normal; stroke-linejoin:miter; font-size:12px; stroke-dashoffset:0; image-rendering:auto;" xmlns="http://www.w3.org/2000/svg"
><!--Generated by the Batik Graphics2D SVG Generator--><defs id="genericDefs"
  /><g
  ><g
    ><circle r="5" style="fill:none;" cx="0" cy="0"
      /><circle r="1" style="stroke:none;" cx="0" cy="0"
      /><circle r="1" style="fill:none;" cx="0" cy="0"
      /><line y2="-12.5" style="fill:none;" x1="0" x2="0" y1="5"
      /><line y2="-10" style="fill:none;" x1="0" x2="5" y1="-10"
      /><line y2="-12.5" style="fill:none;" x1="5" x2="2.5" y1="-10"
      /><line y2="-7.5" style="fill:none;" x1="5" x2="2.5" y1="-10"
    /></g
  ></g
></svg>

I am not familiar with GeoTools, but I suspect you need to add a viewBox to your generated SVG. 我对GeoTools不熟悉,但是我怀疑您需要在生成的SVG中添加viewBox。

I haven't tested the following, but it should be something like: 我还没有测试以下内容,但是应该是这样的:

SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
// Get the root SVG element
Element root = svgGenerator.getRoot();
// Add a viewBox attribute
root.setAttributeNS(null, "viewBox", "0 0 800 600"); 

You'll need to calculate the correct viewBox values yourself based on your shape geometry. 您需要根据自己的形状几何计算正确的viewBox值。 The four numbers in the value are: minX, minY, width, and height respectively. 值中的四个数字分别是:minX,minY,宽度和高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM