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();
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/
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final intstatic final String -
Constructor Summary
ConstructorsModifierConstructorDescriptionConstructs a new EPS document that is initially empty and can be drawn on like a Graphics2D object.EpsGraphics2D(String title) Constructs a new EPS document that is initially empty and can be drawn on like a Graphics2D object.EpsGraphics2D(String title, File file, int minX, int minY, int maxX, int maxY) Constructs a new EPS document that is initially empty and can be drawn on like a Graphics2D object.EpsGraphics2D(String title, OutputStream outputStream, int minX, int minY, int maxX, int maxY) Constructs a new EPS document that is initially empty and can be drawn on like a Graphics2D object.protectedConstructs a new EpsGraphics2D instance that is a copy of the supplied argument and points at the same EpsDocument. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddRenderingHints(Map hints) Adds rendering hints.voidclearRect(int x, int y, int width, int height) Clears a rectangle with top-left corner placed at (x,y) using the current background color.voidIntersects the current clip with the interior of the specified Shape and sets the clip to the resulting intersection.voidclipRect(int x, int y, int width, int height) Intersects the current clip with the specified rectangle.voidclose()Closes the EPS file being output to the underlying OutputStream.voidcopyArea(int x, int y, int width, int height, int dx, int dy) Not implemented - performs no action.create()Returns a new Graphics object that is identical to this EpsGraphics2D.create(int x, int y, int width, int height) Returns an EpsGraphics2D object based on this Graphics object, but with a new translation and clip area.voiddispose()Disposes of all resources used by this EpsGraphics2D object.voidDraws a Shape on the EPS document.voiddraw3DRect(int x, int y, int width, int height, boolean raised) Draws a 3D rectangle outline.voiddrawArc(int x, int y, int width, int height, int startAngle, int arcAngle) Draws an arc.voiddrawBytes(byte[] data, int offset, int length, int x, int y) Draws the specified bytes, starting from (x,y)voiddrawChars(char[] data, int offset, int length, int x, int y) Draws the specified characters, starting from (x,y)voiddrawGlyphVector(GlyphVector g, float x, float y) Draws a GlyphVector at (x,y)voiddrawImage(BufferedImage img, BufferedImageOp op, int x, int y) Draws a BufferedImage on the EPS document.booleandrawImage(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.booleandrawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer) Draws an image.booleandrawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) Draws an image.booleandrawImage(Image img, int x, int y, int width, int height, ImageObserver observer) Draws an image.booleandrawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) Draws an image.booleandrawImage(Image img, int x, int y, ImageObserver observer) Draws an image.booleandrawImage(Image img, AffineTransform xform, ImageObserver obs) Draws an Image on the EPS document.voiddrawLine(int x1, int y1, int x2, int y2) Draws a straight line from (x1,y1) to (x2,y2).voiddrawOval(int x, int y, int width, int height) Draws an oval.voiddrawPolygon(int[] xPoints, int[] yPoints, int nPoints) Draws a polygon made with the specified points.voidDraws a polygon.voiddrawPolyline(int[] xPoints, int[] yPoints, int nPoints) Draws a polyline.voiddrawRect(int x, int y, int width, int height) Draws a rectangle with top-left corner placed at (x,y).voiddrawRenderableImage(RenderableImage img, AffineTransform xform) Draws a RenderableImage by invoking its createDefaultRendering method.voiddrawRenderedImage(RenderedImage img, AffineTransform xform) Draws a RenderedImage on the EPS document.voiddrawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) Draws a rounded rectangle.voiddrawString(String s, float x, float y) Draws a string at (x,y)voiddrawString(String str, int x, int y) Draws a string at (x,y)voiddrawString(AttributedCharacterIterator iterator, float x, float y) Draws the characters of an AttributedCharacterIterator, starting from (x,y).voiddrawString(AttributedCharacterIterator iterator, int x, int y) Draws the characters of an AttributedCharacterIterator, starting from (x,y).voidFills a Shape on the EPS document.voidfill3DRect(int x, int y, int width, int height, boolean raised) Fills a 3D rectangle.voidfillArc(int x, int y, int width, int height, int startAngle, int arcAngle) Fills an arc.voidfillOval(int x, int y, int width, int height) Fills an oval.voidfillPolygon(int[] xPoints, int[] yPoints, int nPoints) Fills a polygon made with the specified points.voidFills a polygon.voidfillRect(int x, int y, int width, int height) Fills a rectangle with top-left corner placed at (x,y).voidfillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) Fills a rounded rectangle.voidfinalize()Finalizes the object.voidflush()Flushes the buffered contents of this EPS document to the underlying OutputStream it is being written to.booleanReturns whether accurate text mode is being used.Gets the background color that is used by the clearRect method.getClip()Gets the current clipping area.Returns the bounding rectangle of the current clipping area.Returns the bounding rectangle of the current clipping area.getColor()Returns the current Color.intReturns the color depth used for all drawing operations.returns the current Composite of the EpsGraphics2D object.Returns the device configuration associated with this EpsGraphics2D object.getFont()Returns the Font currently being used.Gets the font metrics of the current font.Gets the font metrics for the specified font.Returns the FontRenderContext.getPaint()Returns the current Paint of the EpsGraphics2D object.getRenderingHint(RenderingHints.Key hintKey) Returns the value of a single preference for the rendering algorithms.Returns the preferences for the rendering algorithms.Returns the Stroke currently used.Gets the AffineTransform used by this EpsGraphics2D.booleanChecks whether or not the specified Shape intersects the specified Rectangle, which is in device space.booleanhitClip(int x, int y, int width, int height) Returns true if the specified rectangular area might intersect the current clipping area.voidrotate(double theta) Concatenates the current EpsGraphics2D Transform with a rotation transform.voidrotate(double theta, double x, double y) Concatenates the current EpsGraphics2D Transform with a translated rotation transform.voidscale(double sx, double sy) Concatenates the current EpsGraphics2D Transform with a scaling transformation.voidsetAccurateTextMode(boolean b) Sets whether to use accurate text mode when rendering text in EPS.voidsetBackground(Color color) Sets the background color to be used by the clearRect method.voidsetClip(int x, int y, int width, int height) Sets the current clip to the rectangle specified by the given coordinates.voidSets the current clipping area to an arbitrary clip shape.voidSets the Color to be used when drawing all future shapes, text, etc.voidsetColorDepth(int c) Sets the number of colours to use when drawing on the document.voidsetComposite(Composite comp) Sets the Composite to be used by this EpsGraphics2D.voidSets the Font to be used in future text.voidSets the Paint attribute for the EpsGraphics2D object.voidSets the paint mode of this EpsGraphics2D object to overwrite the destination EpsDocument with the current color.voidsetRenderingHint(RenderingHints.Key hintKey, Object hintValue) Sets a rendering hint.voidsetRenderingHints(Map hints) Sets the rendering hints.voidSets the stroke.voidSets the AffineTransform to be used by this EpsGraphics2D.voidsetXORMode(Color c1) Not implemented - performs no action.voidshear(double shx, double shy) Concatenates the current EpsGraphics2D Transform with a shearing transform.toString()Returns the entire contents of the EPS document, complete with headers and bounding box.voidComposes an AffineTransform object with the Transform in this EpsGraphics2D according to the rule last-specified-first-applied.voidtranslate(double tx, double ty) Concatenates the current EpsGraphics2D Transformation with a translation transform.voidtranslate(int x, int y) Translates the origin of the EpsGraphics2D context to the point (x,y) in the current coordinate system.Methods inherited from class java.awt.Graphics
getClipRect
-
Field Details
-
VERSION
- See Also:
-
BLACK_AND_WHITE
public static final int BLACK_AND_WHITE- See Also:
-
GRAYSCALE
public static final int GRAYSCALE- See Also:
-
RGB
public static final int RGB- See Also:
-
-
Constructor Details
-
EpsGraphics2D
public EpsGraphics2D()Constructs a new EPS document that is initially empty and can be drawn on like a Graphics2D object. The EPS document is stored in memory. -
EpsGraphics2D
Constructs a new EPS document that is initially empty and can be drawn on like a Graphics2D object. The EPS document is stored in memory. -
EpsGraphics2D
public EpsGraphics2D(String title, File file, int minX, int minY, int maxX, int maxY) throws IOException Constructs a new EPS document that is initially empty and can be drawn on like a Graphics2D object. The EPS document is written to the file as it goes, which reduces memory usage. The bounding box of the document is fixed and specified at construction time by minX,minY,maxX,maxY. The file is flushed and closed when the close() method is called.- Throws:
IOException
-
EpsGraphics2D
public EpsGraphics2D(String title, OutputStream outputStream, int minX, int minY, int maxX, int maxY) throws IOException Constructs a new EPS document that is initially empty and can be drawn on like a Graphics2D object. The EPS document is written to the output stream as it goes, which reduces memory usage. The bounding box of the document is fixed and specified at construction time by minX,minY,maxX,maxY. The output stream is flushed and closed when the close() method is called.- Throws:
IOException
-
EpsGraphics2D
Constructs a new EpsGraphics2D instance that is a copy of the supplied argument and points at the same EpsDocument.
-
-
Method Details
-
setAccurateTextMode
public void setAccurateTextMode(boolean b) Sets whether to use accurate text mode when rendering text in EPS. This is enabled (true) by default. When accurate text 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. With accurate text mode enabled, it is not necessary for the EPS viewer to have the required font installed.Turning off accurate text mode will require the EPS viewer to have the necessary fonts installed. If you are using a lot of text, you will find that this significantly reduces the file size of your EPS documents. AffineTransforms can only affect the starting point of text using this simpler text mode - all text will be horizontal.
-
getAccurateTextMode
public boolean getAccurateTextMode()Returns whether accurate text mode is being used. -
setColorDepth
public void setColorDepth(int c) Sets the number of colours to use when drawing on the document. Can be either EpsGraphics2D.RGB (default) or EpsGraphics2D.GREYSCALE. -
getColorDepth
public int getColorDepth()Returns the color depth used for all drawing operations. This can be either EpsGraphics2D.RGB (default) or EpsGraphics2D.GREYSCALE. -
flush
Flushes the buffered contents of this EPS document to the underlying OutputStream it is being written to.- Throws:
IOException
-
close
Closes the EPS file being output to the underlying OutputStream. The OutputStream is automatically flushed before being closed. If you forget to do this, the file may be incomplete.- Throws:
IOException
-
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:
draw3DRectin classGraphics2D
-
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:
fill3DRectin classGraphics2D
-
draw
Draws a Shape on the EPS document.- Specified by:
drawin classGraphics2D
-
drawImage
Draws an Image on the EPS document.- Specified by:
drawImagein classGraphics2D
-
drawImage
Draws a BufferedImage on the EPS document.- Specified by:
drawImagein classGraphics2D
-
drawRenderedImage
Draws a RenderedImage on the EPS document.- Specified by:
drawRenderedImagein classGraphics2D
-
drawRenderableImage
Draws a RenderableImage by invoking its createDefaultRendering method.- Specified by:
drawRenderableImagein classGraphics2D
-
drawString
Draws a string at (x,y)- Specified by:
drawStringin classGraphics2D
-
drawString
Draws a string at (x,y)- Specified by:
drawStringin classGraphics2D
-
drawString
Draws the characters of an AttributedCharacterIterator, starting from (x,y).- Specified by:
drawStringin classGraphics2D
-
drawString
Draws the characters of an AttributedCharacterIterator, starting from (x,y).- Specified by:
drawStringin classGraphics2D
-
drawGlyphVector
Draws a GlyphVector at (x,y)- Specified by:
drawGlyphVectorin classGraphics2D
-
fill
Fills a Shape on the EPS document.- Specified by:
fillin classGraphics2D
-
hit
Checks whether or not the specified Shape intersects the specified Rectangle, which is in device space.- Specified by:
hitin classGraphics2D
-
getDeviceConfiguration
Returns the device configuration associated with this EpsGraphics2D object.- Specified by:
getDeviceConfigurationin classGraphics2D
-
setComposite
Sets the Composite to be used by this EpsGraphics2D. EpsGraphics2D does not make use of these.- Specified by:
setCompositein classGraphics2D
-
setPaint
Sets the Paint attribute for the EpsGraphics2D object. Only Paint objects of type Color are respected by EpsGraphics2D.- Specified by:
setPaintin classGraphics2D
-
setStroke
Sets the stroke. Only accepts BasicStroke objects (or subclasses of BasicStroke).- Specified by:
setStrokein classGraphics2D
-
setRenderingHint
Sets a rendering hint. These are not used by EpsGraphics2D.- Specified by:
setRenderingHintin classGraphics2D
-
getRenderingHint
Returns the value of a single preference for the rendering algorithms. Rendering hints are not used by EpsGraphics2D.- Specified by:
getRenderingHintin classGraphics2D
-
setRenderingHints
Sets the rendering hints. These are ignored by EpsGraphics2D.- Specified by:
setRenderingHintsin classGraphics2D
-
addRenderingHints
Adds rendering hints. These are ignored by EpsGraphics2D.- Specified by:
addRenderingHintsin classGraphics2D
-
getRenderingHints
Returns the preferences for the rendering algorithms.- Specified by:
getRenderingHintsin classGraphics2D
-
translate
public void translate(int x, int y) Translates the origin of the EpsGraphics2D context to the point (x,y) in the current coordinate system.- Specified by:
translatein classGraphics2D
-
translate
public void translate(double tx, double ty) Concatenates the current EpsGraphics2D Transformation with a translation transform.- Specified by:
translatein classGraphics2D
-
rotate
public void rotate(double theta) Concatenates the current EpsGraphics2D Transform with a rotation transform.- Specified by:
rotatein classGraphics2D
-
rotate
public void rotate(double theta, double x, double y) Concatenates the current EpsGraphics2D Transform with a translated rotation transform.- Specified by:
rotatein classGraphics2D
-
scale
public void scale(double sx, double sy) Concatenates the current EpsGraphics2D Transform with a scaling transformation.- Specified by:
scalein classGraphics2D
-
shear
public void shear(double shx, double shy) Concatenates the current EpsGraphics2D Transform with a shearing transform.- Specified by:
shearin classGraphics2D
-
transform
Composes an AffineTransform object with the Transform in this EpsGraphics2D according to the rule last-specified-first-applied.- Specified by:
transformin classGraphics2D
-
setTransform
Sets the AffineTransform to be used by this EpsGraphics2D.- Specified by:
setTransformin classGraphics2D
-
getTransform
Gets the AffineTransform used by this EpsGraphics2D.- Specified by:
getTransformin classGraphics2D
-
getPaint
Returns the current Paint of the EpsGraphics2D object.- Specified by:
getPaintin classGraphics2D
-
getComposite
returns the current Composite of the EpsGraphics2D object.- Specified by:
getCompositein classGraphics2D
-
setBackground
Sets the background color to be used by the clearRect method.- Specified by:
setBackgroundin classGraphics2D
-
getBackground
Gets the background color that is used by the clearRect method.- Specified by:
getBackgroundin classGraphics2D
-
getStroke
Returns the Stroke currently used. Guaranteed to be an instance of BasicStroke.- Specified by:
getStrokein classGraphics2D
-
clip
Intersects the current clip with the interior of the specified Shape and sets the clip to the resulting intersection.- Specified by:
clipin classGraphics2D
-
getFontRenderContext
Returns the FontRenderContext.- Specified by:
getFontRenderContextin classGraphics2D
-
create
Returns a new Graphics object that is identical to this EpsGraphics2D. -
create
Returns an EpsGraphics2D object based on this Graphics object, but with a new translation and clip area. -
getColor
Returns the current Color. This will be a default value (black) until it is changed using the setColor method. -
setColor
Sets the Color to be used when drawing all future shapes, text, etc. -
setPaintMode
public void setPaintMode()Sets the paint mode of this EpsGraphics2D object to overwrite the destination EpsDocument with the current color.- Specified by:
setPaintModein classGraphics
-
setXORMode
Not implemented - performs no action.- Specified by:
setXORModein classGraphics
-
getFont
Returns the Font currently being used. -
setFont
Sets the Font to be used in future text. -
getFontMetrics
Gets the font metrics of the current font.- Overrides:
getFontMetricsin classGraphics
-
getFontMetrics
Gets the font metrics for the specified font.- Specified by:
getFontMetricsin classGraphics
-
getClipBounds
Returns the bounding rectangle of the current clipping area.- Specified by:
getClipBoundsin classGraphics
-
clipRect
public void clipRect(int x, int y, int width, int height) Intersects the current clip with the specified rectangle. -
setClip
public void setClip(int x, int y, int width, int height) Sets the current clip to the rectangle specified by the given coordinates. -
getClip
Gets the current clipping area. -
setClip
Sets the current clipping area to an arbitrary clip shape. -
copyArea
public void copyArea(int x, int y, int width, int height, int dx, int dy) Not implemented - performs no action. -
drawLine
public void drawLine(int x1, int y1, int x2, int y2) Draws a straight line from (x1,y1) to (x2,y2). -
fillRect
public void fillRect(int x, int y, int width, int height) Fills a rectangle with top-left corner placed at (x,y). -
drawRect
public void drawRect(int x, int y, int width, int height) Draws a rectangle with top-left corner placed at (x,y). -
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. -
drawRoundRect
public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) Draws a rounded rectangle.- Specified by:
drawRoundRectin classGraphics
-
fillRoundRect
public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) Fills a rounded rectangle.- Specified by:
fillRoundRectin classGraphics
-
drawOval
public void drawOval(int x, int y, int width, int height) Draws an oval. -
fillOval
public void fillOval(int x, int y, int width, int height) Fills an oval. -
drawArc
public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) Draws an arc. -
fillArc
public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) Fills an arc. -
drawPolyline
public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) Draws a polyline.- Specified by:
drawPolylinein classGraphics
-
drawPolygon
public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) Draws a polygon made with the specified points.- Specified by:
drawPolygonin classGraphics
-
drawPolygon
Draws a polygon.- Overrides:
drawPolygonin classGraphics
-
fillPolygon
public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) Fills a polygon made with the specified points.- Specified by:
fillPolygonin classGraphics
-
fillPolygon
Fills a polygon.- Overrides:
fillPolygonin classGraphics
-
drawChars
public void drawChars(char[] data, int offset, int length, int x, int y) Draws the specified characters, starting from (x,y) -
drawBytes
public void drawBytes(byte[] data, int offset, int length, int x, int y) Draws the specified bytes, starting from (x,y) -
drawImage
Draws an image. -
drawImage
Draws an image. -
drawImage
Draws an image. -
drawImage
public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) Draws an image. -
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. -
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. -
dispose
public void dispose()Disposes of all resources used by this EpsGraphics2D object. If this is the only remaining EpsGraphics2D instance pointing at a EpsDocument object, then the EpsDocument object shall become eligible for garbage collection. -
finalize
public void finalize()Finalizes the object. -
toString
Returns the entire contents of the EPS document, complete with headers and bounding box. The returned String is suitable for being written directly to disk as an EPS file. -
hitClip
public boolean hitClip(int x, int y, int width, int height) Returns true if the specified rectangular area might intersect the current clipping area. -
getClipBounds
Returns the bounding rectangle of the current clipping area.- Overrides:
getClipBoundsin classGraphics
-