EPS Graphics2D

org.jibble.epsgraphics
Class EpsGraphics2D

java.lang.Object
  |
  +--java.awt.Graphics
        |
        +--java.awt.Graphics2D
              |
              +--org.jibble.epsgraphics.EpsGraphics2D

public class EpsGraphics2D
extends 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.

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.

This class makes the whole process extremely easy, because you can use it 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 jpeg and png files.

Example usage:

    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();
    
    // Free up resources.
    g.dispose();

You do not need to worry about the size of the canvas when drawing on a EpsGraphics2D object. The bounding box of the EPS document will automatically resize to accomodate new items that you draw.

Not all methods are implemented yet. Those that are not are clearly labelled.

Copyright Paul Mutton, http://www.jibble.org/

Version:
$Id: EpsGraphics2D.java,v 1.10 2003/06/30 22:58:03 pjm2 Exp $
Author:
$Author: pjm2 $

Field Summary
static String VERSION
           
 
Constructor Summary
EpsGraphics2D()
          Constructs a new EPS document that is initially empty and can be drawn on like a Graphics2D object.
 
Method Summary
 void addRenderingHints(Map hints)
          Not implemented
 void clear()
          Clears the contents of the current EPS document.
 void clearRect(int x, int y, int width, int height)
          Clears a rectangle with top-left corner placed at (x,y) using the current background color.
 void clip(Shape s)
          Not implemented
 void clipRect(int x, int y, int width, int height)
          Not implemented
 void copyArea(int x, int y, int width, int height, int dx, int dy)
          Not implemented
 Graphics create()
          Not implemented
 Graphics create(int x, int y, int width, int height)
          Not implemented
 void dispose()
          Disposes of all resources used by this EpsGraphics2D object.
 void draw(Shape s)
          Draws a Shape on the EPS document.
 void draw3DRect(int x, int y, int width, int height, boolean raised)
          Draws a 3D rectangle outline.
 void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
          Draws an arc.
 void drawBytes(byte[] data, int offset, int length, int x, int y)
          Draws the specified bytes, starting from (x,y)
 void drawChars(char[] data, int offset, int length, int x, int y)
          Draws the specified characters, starting from (x,y)
 void drawGlyphVector(GlyphVector g, float x, float y)
          Not implemented
 void drawImage(BufferedImage img, BufferedImageOp op, int x, int y)
          Not implemented
 boolean drawImage(Image img, AffineTransform xform, ImageObserver obs)
          Not implemented
 boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
          Draws an image.
 boolean drawImage(Image img, int x, int y, ImageObserver observer)
          Draws an image.
 boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
          Draws an image.
 boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
          Draws an image.
 boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer)
          Draws an image.
 boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
          Draws an image.
 void drawLine(int x1, int y1, int x2, int y2)
          Draws a straight line from (x1,y1) to (x2,y2).
 void drawOval(int x, int y, int width, int height)
          Draws an oval.
 void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
          Draws a polygon made with the specified points.
 void drawPolygon(Polygon p)
          Draws a polygon.
 void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
          Draws a polyline.
 void drawRect(int x, int y, int width, int height)
          Draws a rectangle with top-left corner placed at (x,y).
 void drawRenderableImage(RenderableImage img, AffineTransform xform)
          Not implemented
 void drawRenderedImage(RenderedImage img, AffineTransform xform)
          Not implemented
 void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
          Draws a rounded rectangle.
 void drawString(AttributedCharacterIterator iterator, float x, float y)
          Draws the characters of an AttributedCharacterIterator, starting from (x,y)
 void drawString(AttributedCharacterIterator iterator, int x, int y)
          Draws the characters of an AttributedCharacterIterator, starting from (x,y)
 void drawString(String s, float x, float y)
          Draws a string at (x,y)
 void drawString(String str, int x, int y)
          Draws a string at (x,y)
 void fill(Shape s)
          Fills a Shape on the EPS document.
 void fill3DRect(int x, int y, int width, int height, boolean raised)
          Fills a 3D rectangle.
 void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
          Fills an arc.
 void fillOval(int x, int y, int width, int height)
          Fills an oval.
 void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
          Fills a polygon made with the specified points.
 void fillPolygon(Polygon p)
          Fills a polygon.
 void fillRect(int x, int y, int width, int height)
          Fills a rectangle with top-left corner placed at (x,y).
 void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
          Fills a rounded rectangle.
 void finalize()
          Not implemented
 Color getBackground()
          Gets the background color that is used by the clearRect method.
 Shape getClip()
          Not implemented
 Rectangle getClipBounds()
          Not implemented
 Rectangle getClipBounds(Rectangle r)
          Not implemented
 Color getColor()
          Returns the current Color.
 Composite getComposite()
          Not implemented
 GraphicsConfiguration getDeviceConfiguration()
          Not implemented
 Font getFont()
          Returns the Font currently being used.
 FontMetrics getFontMetrics()
          Not implemented
 FontMetrics getFontMetrics(Font f)
          Not implemented
 FontRenderContext getFontRenderContext()
          Not implemented
 Paint getPaint()
          Not implemented
 Object getRenderingHint(RenderingHints.Key hintKey)
          Not implemented
 RenderingHints getRenderingHints()
          Not implemented
 Stroke getStroke()
          Returns the Stroke currently used.
 String getTitle()
          Returns the title of the EPS document.
 AffineTransform getTransform()
          Gets the AffineTransform used by this EpsGraphics2D.
 boolean hit(Rectangle rect, Shape s, boolean onStroke)
          Not implemented
 boolean hitClip(int x, int y, int width, int height)
          Not implemented
 void rotate(double theta)
          Not implemented
 void rotate(double theta, double x, double y)
          Not implemented
 void scale(double sx, double sy)
          Not implemented
 void setBackground(Color color)
          Sets the background color to be used by the clearRect method.
 void setClip(int x, int y, int width, int height)
          Not implemented
 void setClip(Shape clip)
          Not implemented
 void setColor(Color c)
          Sets the Color to be used when drawing all future shapes, text, etc.
 void setComposite(Composite comp)
          Not implemented
 void setFont(Font font)
          Sets the Font to be used in future text.
 void setPaint(Paint paint)
          Not implemented
 void setPaintMode()
          Not implemented
 void setRenderingHint(RenderingHints.Key hintKey, Object hintValue)
          Not implemented
 void setRenderingHints(Map hints)
          Not implemented
 void setStroke(Stroke s)
          Sets the stroke.
 void setTitle(String title)
          Sets the title of the EPS document.
 void setTransform(AffineTransform Tx)
          Sets the AffineTransform to be used by this EpsGraphics2D.
 void setXORMode(Color c1)
          Not implemented
 void shear(double shx, double shy)
          Not implemented
 String toString()
          Not implemented
 void transform(AffineTransform Tx)
          Not implemented
 void translate(double tx, double ty)
          Not implemented
 void translate(int x, int y)
          Not implemented
 
Methods inherited from class java.awt.Graphics
getClipRect
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

VERSION

public static final String VERSION
See Also:
Constant Field Values
Constructor Detail

EpsGraphics2D

public EpsGraphics2D()
Constructs a new EPS document that is initially empty and can be drawn on like a Graphics2D object.

Method Detail

clear

public void clear()
Clears the contents of the current EPS document.


getTitle

public String getTitle()
Returns the title of the EPS document.


setTitle

public void setTitle(String title)
Sets the title of the EPS document.


draw3DRect

public void draw3DRect(int x,
                       int y,
                       int width,
                       int height,
                       boolean raised)
Draws a 3D rectangle outline. If it is raised, light appears to come from the top left.

Overrides:
draw3DRect in class Graphics2D

fill3DRect

public void fill3DRect(int x,
                       int y,
                       int width,
                       int height,
                       boolean raised)
Fills a 3D rectangle. If raised, it has bright fill and light appears to come from the top left.

Overrides:
fill3DRect in class Graphics2D

draw

public void draw(Shape s)
Draws a Shape on the EPS document.

Specified by:
draw in class Graphics2D

drawImage

public boolean drawImage(Image img,
                         AffineTransform xform,
                         ImageObserver obs)
Not implemented

Specified by:
drawImage in class Graphics2D

drawImage

public void drawImage(BufferedImage img,
                      BufferedImageOp op,
                      int x,
                      int y)
Not implemented

Specified by:
drawImage in class Graphics2D

drawRenderedImage

public void drawRenderedImage(RenderedImage img,
                              AffineTransform xform)
Not implemented

Specified by:
drawRenderedImage in class Graphics2D

drawRenderableImage

public void drawRenderableImage(RenderableImage img,
                                AffineTransform xform)
Not implemented

Specified by:
drawRenderableImage in class Graphics2D

drawString

public void drawString(String str,
                       int x,
                       int y)
Draws a string at (x,y)

Specified by:
drawString in class Graphics2D

drawString

public void drawString(String s,
                       float x,
                       float y)
Draws a string at (x,y)

Specified by:
drawString in class Graphics2D

drawString

public void drawString(AttributedCharacterIterator iterator,
                       int x,
                       int y)
Draws the characters of an AttributedCharacterIterator, starting from (x,y)

Specified by:
drawString in class Graphics2D

drawString

public void drawString(AttributedCharacterIterator iterator,
                       float x,
                       float y)
Draws the characters of an AttributedCharacterIterator, starting from (x,y)

Specified by:
drawString in class Graphics2D

drawGlyphVector

public void drawGlyphVector(GlyphVector g,
                            float x,
                            float y)
Not implemented

Specified by:
drawGlyphVector in class Graphics2D

fill

public void fill(Shape s)
Fills a Shape on the EPS document.

Specified by:
fill in class Graphics2D

hit

public boolean hit(Rectangle rect,
                   Shape s,
                   boolean onStroke)
Not implemented

Specified by:
hit in class Graphics2D

getDeviceConfiguration

public GraphicsConfiguration getDeviceConfiguration()
Not implemented

Specified by:
getDeviceConfiguration in class Graphics2D

setComposite

public void setComposite(Composite comp)
Not implemented

Specified by:
setComposite in class Graphics2D

setPaint

public void setPaint(Paint paint)
Not implemented

Specified by:
setPaint in class Graphics2D

setStroke

public void setStroke(Stroke s)
Sets the stroke. Only accepts BasicStroke objects (or subclasses of BasicStroke).

Specified by:
setStroke in class Graphics2D

setRenderingHint

public void setRenderingHint(RenderingHints.Key hintKey,
                             Object hintValue)
Not implemented

Specified by:
setRenderingHint in class Graphics2D

getRenderingHint

public Object getRenderingHint(RenderingHints.Key hintKey)
Not implemented

Specified by:
getRenderingHint in class Graphics2D

setRenderingHints

public void setRenderingHints(Map hints)
Not implemented

Specified by:
setRenderingHints in class Graphics2D

addRenderingHints

public void addRenderingHints(Map hints)
Not implemented

Specified by:
addRenderingHints in class Graphics2D

getRenderingHints

public RenderingHints getRenderingHints()
Not implemented

Specified by:
getRenderingHints in class Graphics2D

translate

public void translate(int x,
                      int y)
Not implemented

Specified by:
translate in class Graphics2D

translate

public void translate(double tx,
                      double ty)
Not implemented

Specified by:
translate in class Graphics2D

rotate

public void rotate(double theta)
Not implemented

Specified by:
rotate in class Graphics2D

rotate

public void rotate(double theta,
                   double x,
                   double y)
Not implemented

Specified by:
rotate in class Graphics2D

scale

public void scale(double sx,
                  double sy)
Not implemented

Specified by:
scale in class Graphics2D

shear

public void shear(double shx,
                  double shy)
Not implemented

Specified by:
shear in class Graphics2D

transform

public void transform(AffineTransform Tx)
Not implemented

Specified by:
transform in class Graphics2D

setTransform

public void setTransform(AffineTransform Tx)
Sets the AffineTransform to be used by this EpsGraphics2D.

Specified by:
setTransform in class Graphics2D

getTransform

public AffineTransform getTransform()
Gets the AffineTransform used by this EpsGraphics2D.

Specified by:
getTransform in class Graphics2D

getPaint

public Paint getPaint()
Not implemented

Specified by:
getPaint in class Graphics2D

getComposite

public Composite getComposite()
Not implemented

Specified by:
getComposite in class Graphics2D

setBackground

public void setBackground(Color color)
Sets the background color to be used by the clearRect method.

Specified by:
setBackground in class Graphics2D

getBackground

public Color getBackground()
Gets the background color that is used by the clearRect method.

Specified by:
getBackground in class Graphics2D

getStroke

public Stroke getStroke()
Returns the Stroke currently used. Guaranteed to be an instance of BasicStroke.

Specified by:
getStroke in class Graphics2D

clip

public void clip(Shape s)
Not implemented

Specified by:
clip in class Graphics2D

getFontRenderContext

public FontRenderContext getFontRenderContext()
Not implemented

Specified by:
getFontRenderContext in class Graphics2D

create

public Graphics create()
Not implemented

Specified by:
create in class Graphics

create

public Graphics create(int x,
                       int y,
                       int width,
                       int height)
Not implemented

Overrides:
create in class Graphics

getColor

public Color getColor()
Returns the current Color. This will be a default value (black) until it is changed using the setColor method.

Specified by:
getColor in class Graphics

setColor

public void setColor(Color c)
Sets the Color to be used when drawing all future shapes, text, etc.

Specified by:
setColor in class Graphics

setPaintMode

public void setPaintMode()
Not implemented

Specified by:
setPaintMode in class Graphics

setXORMode

public void setXORMode(Color c1)
Not implemented

Specified by:
setXORMode in class Graphics

getFont

public Font getFont()
Returns the Font currently being used.

Specified by:
getFont in class Graphics

setFont

public void setFont(Font font)
Sets the Font to be used in future text.

Specified by:
setFont in class Graphics

getFontMetrics

public FontMetrics getFontMetrics()
Not implemented

Overrides:
getFontMetrics in class Graphics

getFontMetrics

public FontMetrics getFontMetrics(Font f)
Not implemented

Specified by:
getFontMetrics in class Graphics

getClipBounds

public Rectangle getClipBounds()
Not implemented

Specified by:
getClipBounds in class Graphics

clipRect

public void clipRect(int x,
                     int y,
                     int width,
                     int height)
Not implemented

Specified by:
clipRect in class Graphics

setClip

public void setClip(int x,
                    int y,
                    int width,
                    int height)
Not implemented

Specified by:
setClip in class Graphics

getClip

public Shape getClip()
Not implemented

Specified by:
getClip in class Graphics

setClip

public void setClip(Shape clip)
Not implemented

Specified by:
setClip in class Graphics

copyArea

public void copyArea(int x,
                     int y,
                     int width,
                     int height,
                     int dx,
                     int dy)
Not implemented

Specified by:
copyArea in class Graphics

drawLine

public void drawLine(int x1,
                     int y1,
                     int x2,
                     int y2)
Draws a straight line from (x1,y1) to (x2,y2).

Specified by:
drawLine in class Graphics

fillRect

public void fillRect(int x,
                     int y,
                     int width,
                     int height)
Fills a rectangle with top-left corner placed at (x,y).

Specified by:
fillRect in class Graphics

drawRect

public void drawRect(int x,
                     int y,
                     int width,
                     int height)
Draws a rectangle with top-left corner placed at (x,y).

Overrides:
drawRect in class Graphics

clearRect

public void clearRect(int x,
                      int y,
                      int width,
                      int height)
Clears a rectangle with top-left corner placed at (x,y) using the current background color.

Specified by:
clearRect in class Graphics

drawRoundRect

public void drawRoundRect(int x,
                          int y,
                          int width,
                          int height,
                          int arcWidth,
                          int arcHeight)
Draws a rounded rectangle.

Specified by:
drawRoundRect in class Graphics

fillRoundRect

public void fillRoundRect(int x,
                          int y,
                          int width,
                          int height,
                          int arcWidth,
                          int arcHeight)
Fills a rounded rectangle.

Specified by:
fillRoundRect in class Graphics

drawOval

public void drawOval(int x,
                     int y,
                     int width,
                     int height)
Draws an oval.

Specified by:
drawOval in class Graphics

fillOval

public void fillOval(int x,
                     int y,
                     int width,
                     int height)
Fills an oval.

Specified by:
fillOval in class Graphics

drawArc

public void drawArc(int x,
                    int y,
                    int width,
                    int height,
                    int startAngle,
                    int arcAngle)
Draws an arc.

Specified by:
drawArc in class Graphics

fillArc

public void fillArc(int x,
                    int y,
                    int width,
                    int height,
                    int startAngle,
                    int arcAngle)
Fills an arc.

Specified by:
fillArc in class Graphics

drawPolyline

public void drawPolyline(int[] xPoints,
                         int[] yPoints,
                         int nPoints)
Draws a polyline.

Specified by:
drawPolyline in class Graphics

drawPolygon

public void drawPolygon(int[] xPoints,
                        int[] yPoints,
                        int nPoints)
Draws a polygon made with the specified points.

Specified by:
drawPolygon in class Graphics

drawPolygon

public void drawPolygon(Polygon p)
Draws a polygon.

Overrides:
drawPolygon in class Graphics

fillPolygon

public void fillPolygon(int[] xPoints,
                        int[] yPoints,
                        int nPoints)
Fills a polygon made with the specified points.

Specified by:
fillPolygon in class Graphics

fillPolygon

public void fillPolygon(Polygon p)
Fills a polygon.

Overrides:
fillPolygon in class Graphics

drawChars

public void drawChars(char[] data,
                      int offset,
                      int length,
                      int x,
                      int y)
Draws the specified characters, starting from (x,y)

Overrides:
drawChars in class Graphics

drawBytes

public void drawBytes(byte[] data,
                      int offset,
                      int length,
                      int x,
                      int y)
Draws the specified bytes, starting from (x,y)

Overrides:
drawBytes in class Graphics

drawImage

public boolean drawImage(Image img,
                         int x,
                         int y,
                         ImageObserver observer)
Draws an image.

Specified by:
drawImage in class Graphics

drawImage

public boolean drawImage(Image img,
                         int x,
                         int y,
                         int width,
                         int height,
                         ImageObserver observer)
Draws an image.

Specified by:
drawImage in class Graphics

drawImage

public boolean drawImage(Image img,
                         int x,
                         int y,
                         Color bgcolor,
                         ImageObserver observer)
Draws an image.

Specified by:
drawImage in class Graphics

drawImage

public boolean drawImage(Image img,
                         int x,
                         int y,
                         int width,
                         int height,
                         Color bgcolor,
                         ImageObserver observer)
Draws an image.

Specified by:
drawImage in class Graphics

drawImage

public boolean drawImage(Image img,
                         int dx1,
                         int dy1,
                         int dx2,
                         int dy2,
                         int sx1,
                         int sy1,
                         int sx2,
                         int sy2,
                         ImageObserver observer)
Draws an image.

Specified by:
drawImage in class Graphics

drawImage

public boolean drawImage(Image img,
                         int dx1,
                         int dy1,
                         int dx2,
                         int dy2,
                         int sx1,
                         int sy1,
                         int sx2,
                         int sy2,
                         Color bgcolor,
                         ImageObserver observer)
Draws an image.

Specified by:
drawImage in class Graphics

dispose

public void dispose()
Disposes of all resources used by this EpsGraphics2D object.

Specified by:
dispose in class Graphics

finalize

public void finalize()
Not implemented

Overrides:
finalize in class Graphics

toString

public String toString()
Not implemented

Overrides:
toString in class Graphics

hitClip

public boolean hitClip(int x,
                       int y,
                       int width,
                       int height)
Not implemented

Overrides:
hitClip in class Graphics

getClipBounds

public Rectangle getClipBounds(Rectangle r)
Not implemented

Overrides:
getClipBounds in class Graphics

EPS Graphics2D