Java/XML/SVG

Материал из Java эксперт
Версия от 07:17, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

SVG Canvas Demo

import org.apache.batik.swing.*;
import org.apache.batik.swing.svg.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Event.*;
public class SVGCanvasDemo {
  JSVGCanvas svgCanvas = new JSVGCanvas();
  public static void main(String args[]) {
    JFrame frame = new JFrame("JSVGCanvas Demo");
    frame.setSize(400, 400);
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent ev) {
        System.exit(0);
      }
    });
    new SVGCanvasDemo(frame);
  }
  public SVGCanvasDemo(JFrame frame) {
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add("Center", svgCanvas);
    frame.setVisible(true);
    svgCanvas.setURI("file:/c:/files/hungryminds/rectangles.svg");
  }
}





SVG Graphics DDemo

import java.awt.Rectangle;
import java.awt.Graphics2D;
import java.io.Writer;
import java.io.FileWriter;
import java.io.IOException; 
import org.apache.batik.svggen.SVGGraphics2D; 
import org.apache.batik.dom.GenericDOMImplementation; 
import org.w3c.dom.Document; 
import org.w3c.dom.DOMImplementation;
public class SVGGraphics2DDemo {
       public void paint(Graphics2D g){
              g.drawString("Java,XML and Web Services Bible",12,12);
       }
       public static void main(String args[]) throws IOException{
              SVGGraphics2DDemo sv2Demo = new SVGGraphics2DDemo();
              DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
              Document doc = domImpl.createDocument(null, "svg", null); 
              SVGGraphics2D svg = new SVGGraphics2D(doc);
              sv2Demo.paint(svg);
              svg.stream(new FileWriter("booktitle.svg"),false);
       }
}
//booktitle.svg
/*
<?xml version="1.0" encoding="Cp1252"?>
<svg fill-opacity="1" color-interpolation="sRGB" color-rendering="auto" text-rendering="auto" stroke="black" stroke-linecap="square" width="2147483647" stroke-miterlimit="10" stroke-opacity="1" shape-rendering="auto" fill="black" stroke-dasharray="none" font-weight="normal" stroke-width="1" height="2147483647" font-family="&apos;Arial&apos;" font-style="normal" stroke-linejoin="miter" font-size="12" image-rendering="auto" stroke-dashoffset="0">
  
  <defs id="genericDefs" />
  <g>
    <g>
      <text x="12" y="12" stroke="none">Java and XML Bible; WebServices
      </text>
    </g>
  </g>
</svg>

*/





SVG to JPEG

import java.io.*;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
public class SVGDOC2JPEG {
  public static void main(String[] args) throws Exception {
    JPEGTranscoder transcoder = new JPEGTranscoder();
    transcoder.addTranscodingHint(JPEGTranscoder.KEY_XML_PARSER_CLASSNAME,
        "org.apache.crimson.parser.XMLReaderImpl");
    transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
        new Float(1.0));
    TranscoderInput input = new TranscoderInput(new FileInputStream("rectangles.svg"));
    OutputStream ostream = new FileOutputStream("out.jpg");
    TranscoderOutput output = new TranscoderOutput(ostream);
    transcoder.transcode(input, output);
    ostream.close();
    System.exit(0);
  }
}