001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id$ */
019:
020: package java2d.ps;
021:
022: import java.awt.Color;
023: import java.awt.Font;
024: import java.awt.Graphics2D;
025: import java.io.File;
026: import java.io.IOException;
027: import java.io.OutputStream;
028:
029: import org.apache.commons.io.IOUtils;
030: import org.apache.xmlgraphics.java2d.ps.EPSDocumentGraphics2D;
031:
032: /**
033: * This example demonstrates how you can generate an EPS (Encapsulated PostScript) file in Java
034: * using the EPSDocumentGraphics2D class from Apache XML Graphics Commons.
035: */
036: public class EPSExample1 {
037:
038: /**
039: * Creates an EPS file. The contents are painted using a Graphics2D implementation that
040: * generates an EPS file.
041: * @param outputFile the target file
042: * @throws IOException In case of an I/O error
043: */
044: public static void generateEPSusingJava2D(File outputFile)
045: throws IOException {
046: OutputStream out = new java.io.FileOutputStream(outputFile);
047: out = new java.io.BufferedOutputStream(out);
048: try {
049: //Instantiate the EPSDocumentGraphics2D instance
050: EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false);
051: g2d
052: .setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
053:
054: //Set up the document size
055: g2d.setupDocument(out, 400, 200); //400pt x 200pt
056:
057: //Paint a bounding box
058: g2d.drawRect(0, 0, 400, 200);
059:
060: //A few rectangles rotated and with different color
061: Graphics2D copy = (Graphics2D) g2d.create();
062: int c = 12;
063: for (int i = 0; i < c; i++) {
064: float f = ((i + 1) / (float) c);
065: Color col = new Color(0.0f, 1 - f, 0.0f);
066: copy.setColor(col);
067: copy.fillRect(70, 90, 50, 50);
068: copy.rotate(-2 * Math.PI / (double) c, 70, 90);
069: }
070: copy.dispose();
071:
072: //Some text
073: g2d.rotate(-0.25);
074: g2d.setColor(Color.RED);
075: g2d.setFont(new Font("sans-serif", Font.PLAIN, 36));
076: g2d.drawString("Hello world!", 140, 140);
077: g2d.setColor(Color.RED.darker());
078: g2d.setFont(new Font("serif", Font.PLAIN, 36));
079: g2d.drawString("Hello world!", 140, 180);
080:
081: //Cleanup
082: g2d.finish();
083: } finally {
084: IOUtils.closeQuietly(out);
085: }
086: }
087:
088: /**
089: * Command-line interface
090: * @param args command-line arguments
091: */
092: public static void main(String[] args) {
093: try {
094: File targetDir;
095: if (args.length >= 1) {
096: targetDir = new File(args[0]);
097: } else {
098: targetDir = new File(".");
099: }
100: if (!targetDir.exists()) {
101: System.err.println("Target Directory does not exist: "
102: + targetDir);
103: }
104: generateEPSusingJava2D(new File(targetDir,
105: "eps-example1.eps"));
106: } catch (Exception e) {
107: e.printStackTrace();
108: }
109: }
110:
111: }
|