-
Notifications
You must be signed in to change notification settings - Fork 1
SVG & JPG
Bond198 edited this page Nov 6, 2013
·
2 revisions
== Monday November 04, 2013 == http://xmlgraphics.apache.org/batik/
public void saveComponentAsJPEG(Component myComponent, String filename) { Dimension size = myComponent.getSize(); BufferedImage myImage = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = myImage.createGraphics(); myComponent.paint(g2); try { OutputStream out = new FileOutputStream(filename); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(myImage); out.close(); } catch (Exception e) { System.out.println(e); } }
package org.csiro.svg.parser; import org.csiro.svg.dom.*; import org.w3c.dom.svg.*; import java.io.*; // this is a sample file that shows how to create an svg document and // write it to a file public class CreateSVG { public static void main(String[] args) { if (args.length != 1) { System.out.println("usage: java org.csiro.svg.parser.CreateSVG out.svg"); System.exit(1); } try { SVGDocument svgDoc = new SVGDocumentImpl(true); // the "true" indicates that this is to be a stylable svg doc // create root element and add it to doc SVGSVGElement root = (SVGSVGElement)svgDoc.createElement("svg"); svgDoc.appendChild(root); // set width and height of element SVGLength width = root.createSVGLength(); width.setValueAsString("15cm"); SVGLength height = root.createSVGLength(); height.setValue(600); root.setWidth(width); root.setHeight(height); // add a circle SVGCircleElement circle = (SVGCircleElement)svgDoc.createElement("circle"); SVGLength cx = root.createSVGLength(); cx.setValue(100); circle.setCx(cx); SVGLength cy = root.createSVGLength(); cy.setValue(100); circle.setCy(cy); circle.setAttribute("r", "50"); circle.setAttribute("style", "fill:red; stroke:black; stroke-width:5"); root.appendChild(circle); // add a rectangle SVGRectElement rect = (SVGRectElement)svgDoc.createElement("rect"); SVGLength rectX = root.createSVGLength(); rectX.setValueAsString("200px"); rect.setX(rectX); SVGLength rectY = root.createSVGLength(); rectY.setValueAsString("250px"); rect.setY(rectY); rect.setAttribute("width", "200"); rect.setAttribute("height", "150"); rect.setAttribute("style", "fill:none; stroke:blue; stroke-width:10"); root.appendChild(rect); // write the svg doc to a file FileOutputStream out = new FileOutputStream(new File(args[0])); XmlWriter writer = new XmlWriter(out); // need to pass in the doctype declaration so that it can be put at the top of the xml file writer.print(svgDoc, ""); out.close(); } catch (IOException e) { System.out.println("IOException: " + e.getMessage()); System.exit(1); } System.out.println("finished writing svg file: " + args[0]); System.exit(0); } }