public class EpsGraphics2D
extends java.awt.Graphics2D
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/ Adapted by Werner Goebl, mdw
Modifier and Type | Field and Description |
---|---|
static int |
BLACK_AND_WHITE |
static int |
GRAYSCALE |
static int |
RGB |
static java.lang.String |
VERSION |
Modifier | Constructor and Description |
---|---|
|
EpsGraphics2D()
Constructs a new EPS document that is initially empty and can be drawn on
like a Graphics2D object.
|
protected |
EpsGraphics2D(EpsGraphics2D g)
Constructs a new EpsGraphics2D instance that is a copy of the supplied
argument and points at the same EpsDocument.
|
|
EpsGraphics2D(java.lang.String title)
Constructs a new EPS document that is initially empty and can be drawn on
like a Graphics2D object.
|
|
EpsGraphics2D(java.lang.String title,
java.io.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(java.lang.String title,
java.io.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.
|
Modifier and Type | Method and Description |
---|---|
void |
addRenderingHints(java.util.Map hints)
Adds rendering hints.
|
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(java.awt.Shape s)
Intersects the current clip with the interior of the specified Shape and
sets the clip to the resulting intersection.
|
void |
clipRect(int x,
int y,
int width,
int height)
Intersects the current clip with the specified rectangle.
|
void |
close()
Closes the EPS file being output to the underlying OutputStream.
|
void |
copyArea(int x,
int y,
int width,
int height,
int dx,
int dy)
Not implemented - performs no
action.
|
java.awt.Graphics |
create()
Returns a new Graphics object that is identical to this EpsGraphics2D.
|
java.awt.Graphics |
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.
|
void |
dispose()
Disposes of all resources used by this EpsGraphics2D object.
|
void |
draw(java.awt.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(java.awt.font.GlyphVector g,
float x,
float y)
Draws a GlyphVector at (x,y)
|
void |
drawImage(java.awt.image.BufferedImage img,
java.awt.image.BufferedImageOp op,
int x,
int y)
Draws a BufferedImage on the EPS document.
|
boolean |
drawImage(java.awt.Image img,
java.awt.geom.AffineTransform xform,
java.awt.image.ImageObserver obs)
Draws an Image on the EPS document.
|
boolean |
drawImage(java.awt.Image img,
int x,
int y,
java.awt.Color bgcolor,
java.awt.image.ImageObserver observer)
Draws an image.
|
boolean |
drawImage(java.awt.Image img,
int x,
int y,
java.awt.image.ImageObserver observer)
Draws an image.
|
boolean |
drawImage(java.awt.Image img,
int x,
int y,
int width,
int height,
java.awt.Color bgcolor,
java.awt.image.ImageObserver observer)
Draws an image.
|
boolean |
drawImage(java.awt.Image img,
int x,
int y,
int width,
int height,
java.awt.image.ImageObserver observer)
Draws an image.
|
boolean |
drawImage(java.awt.Image img,
int dx1,
int dy1,
int dx2,
int dy2,
int sx1,
int sy1,
int sx2,
int sy2,
java.awt.Color bgcolor,
java.awt.image.ImageObserver observer)
Draws an image.
|
boolean |
drawImage(java.awt.Image img,
int dx1,
int dy1,
int dx2,
int dy2,
int sx1,
int sy1,
int sx2,
int sy2,
java.awt.image.ImageObserver observer)
Draws an image.
|
void |
drawLine(double x1,
double y1,
double x2,
double y2)
Draws a straight line from (x1,y1) to (x2,y2).
|
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(java.awt.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(java.awt.image.renderable.RenderableImage img,
java.awt.geom.AffineTransform xform)
Draws a RenderableImage by invoking its createDefaultRendering method.
|
void |
drawRenderedImage(java.awt.image.RenderedImage img,
java.awt.geom.AffineTransform xform)
Draws a RenderedImage on the EPS document.
|
void |
drawRoundRect(int x,
int y,
int width,
int height,
int arcWidth,
int arcHeight)
Draws a rounded rectangle.
|
void |
drawString(java.text.AttributedCharacterIterator iterator,
float x,
float y)
Draws the characters of an AttributedCharacterIterator, starting from
(x,y).
|
void |
drawString(java.text.AttributedCharacterIterator iterator,
int x,
int y)
Draws the characters of an AttributedCharacterIterator, starting from
(x,y).
|
void |
drawString(java.lang.String s,
float x,
float y)
Draws a string at (x,y)
|
void |
drawString(java.lang.String str,
int x,
int y)
Draws a string at (x,y)
|
void |
fill(java.awt.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(java.awt.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()
Finalizes the object.
|
void |
flush()
Flushes the buffered contents of this EPS document to the underlying
OutputStream it is being written to.
|
boolean |
getAccurateTextMode()
Returns whether accurate text mode is being used.
|
java.awt.Color |
getBackground()
Gets the background color that is used by the clearRect method.
|
java.awt.Shape |
getClip()
Gets the current clipping area.
|
java.awt.Rectangle |
getClipBounds()
Returns the bounding rectangle of the current clipping area.
|
java.awt.Rectangle |
getClipBounds(java.awt.Rectangle r)
Returns the bounding rectangle of the current clipping area.
|
java.awt.Color |
getColor()
Returns the current Color.
|
int |
getColorDepth()
Returns the color depth used for all drawing operations.
|
java.awt.Composite |
getComposite()
returns the current Composite of the EpsGraphics2D object.
|
java.awt.GraphicsConfiguration |
getDeviceConfiguration()
Returns the device configuration associated with this EpsGraphics2D
object.
|
java.awt.Font |
getFont()
Returns the Font currently being used.
|
java.awt.FontMetrics |
getFontMetrics()
Gets the font metrics of the current font.
|
java.awt.FontMetrics |
getFontMetrics(java.awt.Font f)
Gets the font metrics for the specified font.
|
java.awt.font.FontRenderContext |
getFontRenderContext()
Returns the FontRenderContext.
|
java.awt.Paint |
getPaint()
Returns the current Paint of the EpsGraphics2D object.
|
java.lang.Object |
getRenderingHint(java.awt.RenderingHints.Key hintKey)
Returns the value of a single preference for the rendering algorithms.
|
java.awt.RenderingHints |
getRenderingHints()
Returns the preferences for the rendering algorithms.
|
java.awt.Stroke |
getStroke()
Returns the Stroke currently used.
|
java.awt.geom.AffineTransform |
getTransform()
Gets the AffineTransform used by this EpsGraphics2D.
|
boolean |
hit(java.awt.Rectangle rect,
java.awt.Shape s,
boolean onStroke)
Checks whether or not the specified Shape intersects the specified
Rectangle, which is in device space.
|
boolean |
hitClip(int x,
int y,
int width,
int height)
Returns true if the specified rectangular area might intersect the
current clipping area.
|
void |
rotate(double theta)
Concatenates the current EpsGraphics2D Transform with a rotation
transform.
|
void |
rotate(double theta,
double x,
double y)
Concatenates the current EpsGraphics2D Transform with a translated
rotation transform.
|
void |
scale(double sx,
double sy)
Concatenates the current EpsGraphics2D Transform with a scaling
transformation.
|
void |
setAccurateTextMode(boolean b)
Sets whether to use accurate text mode when rendering text in EPS.
|
void |
setBackground(java.awt.Color color)
Sets the background color to be used by the clearRect method.
|
void |
setClip(int x,
int y,
int width,
int height)
Sets the current clip to the rectangle specified by the given
coordinates.
|
void |
setClip(java.awt.Shape clip)
Sets the current clipping area to an arbitrary clip shape.
|
void |
setColor(java.awt.Color c)
Sets the Color to be used when drawing all future shapes, text, etc.
|
void |
setColorDepth(int c)
Sets the number of colours to use when drawing on the document.
|
void |
setComposite(java.awt.Composite comp)
Sets the Composite to be used by this EpsGraphics2D.
|
void |
setFont(java.awt.Font font)
Sets the Font to be used in future text.
|
void |
setPaint(java.awt.Paint paint)
Sets the Paint attribute for the EpsGraphics2D object.
|
void |
setPaintMode()
Sets the paint mode of this EpsGraphics2D object to overwrite the
destination EpsDocument with the current color.
|
void |
setRenderingHint(java.awt.RenderingHints.Key hintKey,
java.lang.Object hintValue)
Sets a rendering hint.
|
void |
setRenderingHints(java.util.Map hints)
Sets the rendering hints.
|
void |
setStroke(java.awt.Stroke s)
Sets the stroke.
|
void |
setTransform(java.awt.geom.AffineTransform Tx)
Sets the AffineTransform to be used by this EpsGraphics2D.
|
void |
setXORMode(java.awt.Color c1)
Not implemented - performs no
action.
|
void |
shear(double shx,
double shy)
Concatenates the current EpsGraphics2D Transform with a shearing
transform.
|
java.lang.String |
toString()
Returns the entire contents of the EPS document, complete with headers
and bounding box.
|
void |
transform(java.awt.geom.AffineTransform Tx)
Composes an AffineTransform object with the Transform in this
EpsGraphics2D according to the rule last-specified-first-applied.
|
void |
translate(double tx,
double ty)
Concatenates the current EpsGraphics2D Transformation with a translation
transform.
|
void |
translate(int x,
int y)
Translates the origin of the EpsGraphics2D context to the point (x,y) in
the current coordinate system.
|
public static final java.lang.String VERSION
public static final int BLACK_AND_WHITE
public static final int GRAYSCALE
public static final int RGB
public EpsGraphics2D()
public EpsGraphics2D(java.lang.String title)
public EpsGraphics2D(java.lang.String title, java.io.File file, int minX, int minY, int maxX, int maxY) throws java.io.IOException
java.io.IOException
public EpsGraphics2D(java.lang.String title, java.io.OutputStream outputStream, int minX, int minY, int maxX, int maxY) throws java.io.IOException
java.io.IOException
protected EpsGraphics2D(EpsGraphics2D g)
public void setAccurateTextMode(boolean b)
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.
public boolean getAccurateTextMode()
public void setColorDepth(int c)
public int getColorDepth()
public void flush() throws java.io.IOException
java.io.IOException
public void close() throws java.io.IOException
java.io.IOException
public void draw3DRect(int x, int y, int width, int height, boolean raised)
draw3DRect
in class java.awt.Graphics2D
public void fill3DRect(int x, int y, int width, int height, boolean raised)
fill3DRect
in class java.awt.Graphics2D
public void draw(java.awt.Shape s)
draw
in class java.awt.Graphics2D
public boolean drawImage(java.awt.Image img, java.awt.geom.AffineTransform xform, java.awt.image.ImageObserver obs)
drawImage
in class java.awt.Graphics2D
public void drawImage(java.awt.image.BufferedImage img, java.awt.image.BufferedImageOp op, int x, int y)
drawImage
in class java.awt.Graphics2D
public void drawRenderedImage(java.awt.image.RenderedImage img, java.awt.geom.AffineTransform xform)
drawRenderedImage
in class java.awt.Graphics2D
public void drawRenderableImage(java.awt.image.renderable.RenderableImage img, java.awt.geom.AffineTransform xform)
drawRenderableImage
in class java.awt.Graphics2D
public void drawString(java.lang.String str, int x, int y)
drawString
in class java.awt.Graphics2D
public void drawString(java.lang.String s, float x, float y)
drawString
in class java.awt.Graphics2D
public void drawString(java.text.AttributedCharacterIterator iterator, int x, int y)
drawString
in class java.awt.Graphics2D
public void drawString(java.text.AttributedCharacterIterator iterator, float x, float y)
drawString
in class java.awt.Graphics2D
public void drawGlyphVector(java.awt.font.GlyphVector g, float x, float y)
drawGlyphVector
in class java.awt.Graphics2D
public void fill(java.awt.Shape s)
fill
in class java.awt.Graphics2D
public boolean hit(java.awt.Rectangle rect, java.awt.Shape s, boolean onStroke)
hit
in class java.awt.Graphics2D
public java.awt.GraphicsConfiguration getDeviceConfiguration()
getDeviceConfiguration
in class java.awt.Graphics2D
public final void setComposite(java.awt.Composite comp)
setComposite
in class java.awt.Graphics2D
public final void setPaint(java.awt.Paint paint)
setPaint
in class java.awt.Graphics2D
public final void setStroke(java.awt.Stroke s)
setStroke
in class java.awt.Graphics2D
public void setRenderingHint(java.awt.RenderingHints.Key hintKey, java.lang.Object hintValue)
setRenderingHint
in class java.awt.Graphics2D
public java.lang.Object getRenderingHint(java.awt.RenderingHints.Key hintKey)
getRenderingHint
in class java.awt.Graphics2D
public void setRenderingHints(java.util.Map hints)
setRenderingHints
in class java.awt.Graphics2D
public void addRenderingHints(java.util.Map hints)
addRenderingHints
in class java.awt.Graphics2D
public java.awt.RenderingHints getRenderingHints()
getRenderingHints
in class java.awt.Graphics2D
public void translate(int x, int y)
translate
in class java.awt.Graphics2D
public void translate(double tx, double ty)
translate
in class java.awt.Graphics2D
public void rotate(double theta)
rotate
in class java.awt.Graphics2D
public void rotate(double theta, double x, double y)
rotate
in class java.awt.Graphics2D
public void scale(double sx, double sy)
scale
in class java.awt.Graphics2D
public void shear(double shx, double shy)
shear
in class java.awt.Graphics2D
public void transform(java.awt.geom.AffineTransform Tx)
transform
in class java.awt.Graphics2D
public void setTransform(java.awt.geom.AffineTransform Tx)
setTransform
in class java.awt.Graphics2D
public java.awt.geom.AffineTransform getTransform()
getTransform
in class java.awt.Graphics2D
public java.awt.Paint getPaint()
getPaint
in class java.awt.Graphics2D
public java.awt.Composite getComposite()
getComposite
in class java.awt.Graphics2D
public void setBackground(java.awt.Color color)
setBackground
in class java.awt.Graphics2D
public java.awt.Color getBackground()
getBackground
in class java.awt.Graphics2D
public java.awt.Stroke getStroke()
getStroke
in class java.awt.Graphics2D
public void clip(java.awt.Shape s)
clip
in class java.awt.Graphics2D
public java.awt.font.FontRenderContext getFontRenderContext()
getFontRenderContext
in class java.awt.Graphics2D
public java.awt.Graphics create()
create
in class java.awt.Graphics
public java.awt.Graphics create(int x, int y, int width, int height)
create
in class java.awt.Graphics
public java.awt.Color getColor()
getColor
in class java.awt.Graphics
public final void setColor(java.awt.Color c)
setColor
in class java.awt.Graphics
public void setPaintMode()
setPaintMode
in class java.awt.Graphics
public void setXORMode(java.awt.Color c1)
setXORMode
in class java.awt.Graphics
public java.awt.Font getFont()
getFont
in class java.awt.Graphics
public final void setFont(java.awt.Font font)
setFont
in class java.awt.Graphics
public java.awt.FontMetrics getFontMetrics()
getFontMetrics
in class java.awt.Graphics
public java.awt.FontMetrics getFontMetrics(java.awt.Font f)
getFontMetrics
in class java.awt.Graphics
public java.awt.Rectangle getClipBounds()
getClipBounds
in class java.awt.Graphics
public void clipRect(int x, int y, int width, int height)
clipRect
in class java.awt.Graphics
public void setClip(int x, int y, int width, int height)
setClip
in class java.awt.Graphics
public java.awt.Shape getClip()
getClip
in class java.awt.Graphics
public void setClip(java.awt.Shape clip)
setClip
in class java.awt.Graphics
public void copyArea(int x, int y, int width, int height, int dx, int dy)
copyArea
in class java.awt.Graphics
public void drawLine(double x1, double y1, double x2, double y2)
public void drawLine(int x1, int y1, int x2, int y2)
drawLine
in class java.awt.Graphics
public void fillRect(int x, int y, int width, int height)
fillRect
in class java.awt.Graphics
public void drawRect(int x, int y, int width, int height)
drawRect
in class java.awt.Graphics
public void clearRect(int x, int y, int width, int height)
clearRect
in class java.awt.Graphics
public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
drawRoundRect
in class java.awt.Graphics
public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
fillRoundRect
in class java.awt.Graphics
public void drawOval(int x, int y, int width, int height)
drawOval
in class java.awt.Graphics
public void fillOval(int x, int y, int width, int height)
fillOval
in class java.awt.Graphics
public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
drawArc
in class java.awt.Graphics
public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
fillArc
in class java.awt.Graphics
public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
drawPolyline
in class java.awt.Graphics
public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
drawPolygon
in class java.awt.Graphics
public void drawPolygon(java.awt.Polygon p)
drawPolygon
in class java.awt.Graphics
public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
fillPolygon
in class java.awt.Graphics
public void fillPolygon(java.awt.Polygon p)
fillPolygon
in class java.awt.Graphics
public void drawChars(char[] data, int offset, int length, int x, int y)
drawChars
in class java.awt.Graphics
public void drawBytes(byte[] data, int offset, int length, int x, int y)
drawBytes
in class java.awt.Graphics
public boolean drawImage(java.awt.Image img, int x, int y, java.awt.image.ImageObserver observer)
drawImage
in class java.awt.Graphics
public boolean drawImage(java.awt.Image img, int x, int y, int width, int height, java.awt.image.ImageObserver observer)
drawImage
in class java.awt.Graphics
public boolean drawImage(java.awt.Image img, int x, int y, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)
drawImage
in class java.awt.Graphics
public boolean drawImage(java.awt.Image img, int x, int y, int width, int height, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)
drawImage
in class java.awt.Graphics
public boolean drawImage(java.awt.Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, java.awt.image.ImageObserver observer)
drawImage
in class java.awt.Graphics
public boolean drawImage(java.awt.Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, java.awt.Color bgcolor, java.awt.image.ImageObserver observer)
drawImage
in class java.awt.Graphics
public void dispose()
dispose
in class java.awt.Graphics
public void finalize()
finalize
in class java.awt.Graphics
public java.lang.String toString()
toString
in class java.awt.Graphics
public boolean hitClip(int x, int y, int width, int height)
hitClip
in class java.awt.Graphics
public java.awt.Rectangle getClipBounds(java.awt.Rectangle r)
getClipBounds
in class java.awt.Graphics