博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SVG操作
阅读量:2100 次
发布时间:2019-04-29

本文共 5996 字,大约阅读时间需要 19 分钟。

package rxpe.svg;
import java.awt.Color;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import javax.swing.JFrame;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.anim.dom.SVGDOMImplementation;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.svggen.SVGGraphics2DIOException;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.util.XMLResourceDescriptor;
import org.apache.commons.io.FileUtils;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGDocument;
/*
 * java -jar batik-rasterizer.jar -m image/png test.svg
 */
public class Main {
 
public static void convert()  
{
File f = new File("E:\\bnwork\\荣信\\SVG相关\\元件库\\svg.svg");
File destFile = new File("src/jxt.png");
SVGUtil svgUtil = new SVGUtil();
try {
svgUtil.convertSVGFile2Png(f, destFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void display( String uri) {
Document doc2=null; 
if(uri==null || uri=="")uri = "http://tongxinmao.com/txm/svg.svg";
//SVG转DOM
try {
   String parser = XMLResourceDescriptor.getXMLParserClassName();
   SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
   
   doc2 = f.createDocument(uri);
} catch (Exception ex) {
   // ...
}
JSVGCanvas canvas = new JSVGCanvas();
JFrame f = new JFrame();
f.getContentPane().add(canvas);
 
//canvas.setSVGDocument(doc2);
 
canvas.setDocument(doc2);
f.pack();
f.setVisible(true);
}
public static void paint() throws UnsupportedEncodingException,
SVGGraphics2DIOException {
DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
String svgURI = "http://www.w3.org/2000/svg";
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
// 创建SVG文件
SVGDocument  doc = (SVGDocument) domImpl.createDocument(null, "svg", null);
 
 
//http://www.udel.edu/CIS/software/dist/batik-1.6/docs/svggen.html
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(doc);
ctx.setComment("Generated by TCM");
SVGGraphics2D svggener = new SVGGraphics2D(ctx, false);
//SVGGraphics2D svggener = new SVGGraphics2D(doc);
svggener.setPaint(Color.red);
// 画一个矩形
svggener.fill(new Rectangle(10, 10, 100, 100));
svggener.drawLine(0, 0, 100, 300);
boolean useCSS = false;
// 输出SVG文件
try {
FileOutputStream fo = new FileOutputStream("MyOut.svg");
Writer out = new OutputStreamWriter(fo, "UTF-8"); //System.out
svggener.stream(out, useCSS);
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//convert();
}
public  static void dbg(String msg){
System.out.println(msg);
}
public  static void loadVdrJson(){
File file = new File("vdr.json");
String JSONText="";
try {
JSONText = FileUtils.readFileToString(file);
       } catch (IOException e) {
           e.printStackTrace();
           return;
       }
 
try {  
       JSONObject json = new JSONObject(JSONText);   
       String filepath = json.getString("file");  
       dbg(filepath);
      
       JSONArray components = json.getJSONArray("components");
       JSONArray drawings = json.getJSONArray("drawings");
       
       for(int i=0;i<components.length();i++){
       
JSONObject c = (JSONObject)components.getJSONObject(i);
       
//dbg(c.getString("script"));
       
       }
       
       for(int i=0;i<drawings.length();i++){
       
JSONObject d = (JSONObject)drawings.getJSONObject(i);
       
String name = (d.getString("name"));
       
       
if(!d.isNull("time")){
       
       
}
       
       
       }
       
         
   } catch (JSONException e) {  
       e.printStackTrace();  
   }  
 
}
 /** 
     * Document 转换为 String 并且进行了格式化缩进 
     *  
     * @param doc XML的Document对象 
     * @return String 
     */  
    public static String doc2FormatString(Document doc) {         
        StringWriter stringWriter = null;  
        try {  
            stringWriter = new StringWriter();  
            if(doc != null){  
                OutputFormat format = new OutputFormat(doc,"UTF-8",true);  
                //format.setIndenting(true);//设置是否缩进,默认为true  
                //format.setIndent(4);//设置缩进字符数  
                //format.setPreserveSpace(false);//设置是否保持原来的格式,默认为 false  
                //format.setLineWidth(500);//设置行宽度  
                XMLSerializer serializer = new XMLSerializer(stringWriter,format);  
                serializer.asDOMSerializer();  
                serializer.serialize(doc);  
                return stringWriter.toString();  
            } else {  
                return null;  
            }  
        } catch (Exception e) {  
            return null;  
        } finally {  
            if(stringWriter != null){  
                try {  
                    stringWriter.close();  
                } catch (IOException e) {  
                    return null;  
                }  
            }  
        }  
    }  
      
    
  
public static void dom()
{
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
SVGDocument  doc = (SVGDocument) domImpl.createDocument(svgNS, "svg", null);
Element svgRoot = doc.getDocumentElement();
 
svgRoot.setAttributeNS(null, "width", "400");
svgRoot.setAttributeNS(null, "height", "450");
Element rect = doc.createElementNS(svgNS, "rect");
rect.setAttributeNS(null, "x", "0");
rect.setAttributeNS(null, "y", "0");
rect.setAttributeNS(null, "width", "100");
rect.setAttributeNS(null, "height", "50");
rect.setAttributeNS(null, "style", "fill:blue");
svgRoot.appendChild(rect);
Element circle = doc.createElementNS(svgNS, "circle");
circle.setAttributeNS(null, "cx", "225");
circle.setAttributeNS(null, "cy", "250");
circle.setAttributeNS(null, "r", "100");
circle.setAttributeNS(null, "style", "fill:green;fill-opacity:.5");
        svgRoot.appendChild(circle);
         
 
       dbg( doc2FormatString(doc));
 
 
JSVGCanvas canvas = new JSVGCanvas();
JFrame f = new JFrame();
f.getContentPane().add(canvas);
 
canvas.setDocument(doc);
 
f.pack();
f.setVisible(true);
}
public static void main(String[] args)
{
//loadVdrJson();
dom();
}
}

转载地址:http://kiuhf.baihongyu.com/

你可能感兴趣的文章
【Loadrunner】通过loadrunner录制时候有事件但是白页无法出来登录页怎么办?
查看>>
【English】【托业】【四六级】写译高频词汇
查看>>
【托业】【新东方全真模拟】01~02-----P5~6
查看>>
【托业】【新东方全真模拟】03~04-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST05~06-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST09~10-----P5~6
查看>>
【托业】【新东方托业全真模拟】TEST07~08-----P5~6
查看>>
solver及其配置
查看>>
JAVA多线程之volatile 与 synchronized 的比较
查看>>
Java集合框架知识梳理
查看>>
笔试题(一)—— java基础
查看>>
Redis学习笔记(三)—— 使用redis客户端连接windows和linux下的redis并解决无法连接redis的问题
查看>>
Intellij IDEA使用(一)—— 安装Intellij IDEA(ideaIU-2017.2.3)并完成Intellij IDEA的简单配置
查看>>
Intellij IDEA使用(二)—— 在Intellij IDEA中配置JDK(SDK)
查看>>
Intellij IDEA使用(三)——在Intellij IDEA中配置Tomcat服务器
查看>>
Intellij IDEA使用(四)—— 使用Intellij IDEA创建静态的web(HTML)项目
查看>>
Intellij IDEA使用(五)—— Intellij IDEA在使用中的一些其他常用功能或常用配置收集
查看>>
Intellij IDEA使用(六)—— 使用Intellij IDEA创建Java项目并配置jar包
查看>>
Eclipse使用(十)—— 使用Eclipse创建简单的Maven Java项目
查看>>
Eclipse使用(十一)—— 使用Eclipse创建简单的Maven JavaWeb项目
查看>>