Java EPS Graphics2D package
Create Encapsulated Postscript from a Java program
EpsGraphics2D is suitable for creating high quality EPS graphics for use in documents and papers, and can be used just like a standard Graphics2D object.
Easy EPS output
Many Java programs use Graphics2D to draw stuff on the screen, and while it is easy to save the output as a png or jpeg file, it is a little harder to export it as an EPS for including in a document or paper.
EPS does not suffer pixellation when zooming
This package makes the whole process extremely easy, because you can use the EpsGraphics2D object as if it's a Graphics2D object. The only difference is that all of the implemented methods create EPS output, which means the diagrams you draw can be resized without leading to any of the jagged edges you may see when resizing pixel-based images, such as JEPG and PNG files. This makes them ideal for including in publications and papers with LaTex or suchlike.
Example usage to create EPS output
import org.jibble.epsgraphics.*; .... Graphics2D g = new EpsGraphics2D(); g.setColor(Color.black); // Line thickness 2. g.setStroke(new BasicStroke(2.0f)); // Draw a line. g.drawLine(10, 10, 50, 10); // Fill a rectangle in blue g.setColor(Color.blue); g.fillRect(10, 0, 20, 20); // Get the EPS output. String output = g.toString();
Unrestricted EPS canvas
You do not need to worry about the size of the canvas when drawing on an EpsGraphics2D object. The bounding box of the EPS document will automatically resize to accomodate new items that you draw. If you are using GSView to view the output, you should select the "EPS Clip" option if the drawing does not fit on the first page.
Example EPS output
The jar file includes some example output and the program that was used to create the output. The example drawing was made by TestMain.java and resulted in the creation of output.png and output.eps. The PNG file was created with a standard Graphics2D object, while the EPS file was created with an EpsGraphics2D object. Notice that the EPS file can be resized better than the PNG version. Also notice that the EPS file has automatically set its bounding box so that the large pale blue circle fits completely, unlike the png file where it has the edges clipped.
This sections contains some useful tips for those who wish to go beyond the basic features of EpsGraphics2D.
Pass through directly to a file or OutputStream
EpsGraphics2D stores documents in memory by default. If you are working with huge EPS documents, it may be beneficial to write them directly to disk (or even a zip file). In order to do this, you will need to manually specify the bounding box of your document before it is drawn. One of the constructors lets you specify the OutputStream and bounding box. Anything drawn on this EpsGraphics2D object will be written straight to the OutputStream, which saves memory.
// Save this document to example.eps FileOutputStream outputStream = new FileOutputStream("example.eps"); // Create a new document with bounding box 0 <= x <= 100 and 0 <= y <= 100. EpsGraphics2D g = new EpsGraphics2D("Example", outputStream, 0, 0, 100, 100); // Draw some stuff.... // .... // Flush and close the document (don't forget to do this!) g.flush(); g.close();
Using lots of text
EpsGraphic2D defaults to using "accurate text mode". When this mode is used, all text will be rendered in EPS to appear exactly the same as it would do when drawn with a Graphics2D context (e.g. on a Swing component). With accurate text mode enabled, it is not necessary for the EPS viewer to have the required font installed.
// Turn off accurate text mode g.setAccurateTextMode(false);
Turning off accurate text mode will require the EPS viewer to have the necessary fonts installed, otherwise it will pick its own default font. If you are using a lot of text, you will find that this significantly reduces the file size of your EPS documents. One drawback to turning off accurate text mode is that AffineTransforms will only affect the starting point of text using this simpler text mode - all text will be horizontal.
Creating 300 dpi output
The default resolution used by postscript is 72 dpi (dots per inch). It is possible to increase this to 300 dpi (or indeed any other resolution) by scaling the instance of EpsGraphics2D before it is drawn on.
// Set resolution to 300 dpi (0.24 = 72/300) g.scale(0.24, 0.24);
Likewise, to obtain 600 dpi you should scale the image by a factor of 0.12 and for 1200 dpi you should scale by 0.06. These higher dpi settings are useful for print and typesetters or graphics rips that are incapable of performing their own scaling.
Grayscale and black & white
EpsGraphics2D generates full color RGB output by default. This can be changed via the setColorDepth method.
// Draw everything in 256-color grayscale // (does not affect existing contents of the EPS document) g.setColorDepth(EpsGraphics2D.GRAYSCALE); // Black & white g.setColorDepth(EpsGraphics2D.BLACK_AND_WHITE); // 24-bit RGB again g.setColorDepth(EpsGraphics2D.RGB);
When the color depth is set to grayscale or black & white, EpsGraphics2D will automatically convert color images to 256-color grayscale or 1-bit black & white as appropriate.
Search this site
Copyright Paul Mutton 2001-2013