Java EPS Graphics2D package

Create Encapsulated Postscript from a Java program

Download EpsGraphics2D

View javadoc documentation

EPS Graphics2D

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.

A comparision of png and eps
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();
    // Line thickness 2.
    g.setStroke(new BasicStroke(2.0f));
    // Draw a line.
    g.drawLine(10, 10, 50, 10);

    // Fill a rectangle in 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 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.

Another good example, this time taken from a real piece of software, is this constraint diagram: constraint.png constraint.eps

Here's another layout of the constraint diagram with Venn 3 shown: venn3.png venn3.eps

Advanced Tips

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!)

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

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)

// Black & white

// 24-bit RGB again

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.

Download EpsGraphics2D

39.99 GBP
EpsGraphics2D single-user non-commercial license with source.
For non-commercial use by a single end user on a single computer.

499.99 GBP
EpsGraphics2D unlimited-user commercial license with source.
For unlimited commercial use within companies, distribution within proprietary applications, world wide web and internet usage.

* Download instructions will be delivered via email once your purchase has been approved.
Contact me if there are any problems downloading your purchase.


Search this site



Copyright Paul Mutton 2001-2013
Feedback welcomed

Web Hosting

Web Hosting