public class EpsGraphics2D extends 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 accommodate new items that you draw.
Not all methods are implemented yet. Those that are not are clearly labelled.
Copyright 2001-2006 Paul James Mutton, http://www.jibble.org/
Copyright 2007 Arnaud Blouin
08/09/07
| Modifier and Type | Field | Description |
|---|---|---|
private boolean |
_accurateTextMode |
|
private Color |
_backgroundColor |
|
private Shape |
_clip |
|
private AffineTransform |
_clipTransform |
|
private Color |
_color |
|
private int |
_colorDepth |
|
private Composite |
_composite |
|
private EpsDocument |
_document |
|
private Font |
_font |
|
private static FontRenderContext |
_fontRenderContext |
|
private Paint |
_paint |
|
private BasicStroke |
_stroke |
|
private AffineTransform |
_transform |
|
static int |
BLACK_AND_WHITE |
|
static int |
GRAYSCALE |
|
static int |
RGB |
|
static String |
VERSION |
| Modifier | Constructor | Description |
|---|---|---|
|
EpsGraphics2D() |
Constructs 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.
|
protected |
EpsGraphics2D(EpsGraphics2D g) |
Constructs a new EpsGraphics2D instance that is a copy of the supplied
argument and points at the same EpsDocument.
|
| Modifier and Type | Method | Description |
|---|---|---|
void |
addRenderingHints(Map hints) |
Adds rendering hints.
|
private void |
append(String line) |
Appends a line to the EpsDocument.
|
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) |
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.
|
Graphics |
create() |
Returns a new Graphics object that is identical to this EpsGraphics2D.
|
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(Shape s) |
Draws a Shape on the EPS document.
|
private void |
draw(Shape s,
String action) |
Appends the commands required to draw 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) |
Draws a GlyphVector at (x,y).
|
void |
drawImage(BufferedImage img,
BufferedImageOp op,
int x,
int y) |
Draws a BufferedImage on the EPS document.
|
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.
|
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 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,
AffineTransform xform,
ImageObserver obs) |
Draws an Image on the EPS document.
|
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) |
Draws a RenderableImage by invoking its createDefaultRendering method.
|
void |
drawRenderedImage(RenderedImage img,
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(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 |
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 |
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() |
Finalises 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.
|
Color |
getBackground() |
Gets the background colour that is used by the clearRect method.
|
Shape |
getClip() |
Gets the current clipping area.
|
Rectangle |
getClipBounds() |
Returns the bounding rectangle of the current clipping area.
|
Rectangle |
getClipBounds(Rectangle r) |
Returns the bounding rectangle of the current clipping area.
|
Color |
getColor() |
Returns the current Color.
|
int |
getColorDepth() |
Returns the colour depth used for all drawing operations.
|
Composite |
getComposite() |
returns the current Composite of the EpsGraphics2D object.
|
GraphicsConfiguration |
getDeviceConfiguration() |
Returns the device configuration associated with this EpsGraphics2D object.
|
Font |
getFont() |
Returns the Font currently being used.
|
FontMetrics |
getFontMetrics() |
Gets the font metrics of the current font.
|
FontMetrics |
getFontMetrics(Font f) |
Gets the font metrics for the specified font.
|
FontRenderContext |
getFontRenderContext() |
Returns the FontRenderContext.
|
Paint |
getPaint() |
Returns the current Paint of the EpsGraphics2D object.
|
Object |
getRenderingHint(RenderingHints.Key hintKey) |
Returns the value of a single preference for the rendering algorithms.
|
RenderingHints |
getRenderingHints() |
Returns the preferences for the rendering algorithms.
|
Stroke |
getStroke() |
Returns the Stroke currently used.
|
AffineTransform |
getTransform() |
Gets the AffineTransform used by this EpsGraphics2D.
|
boolean |
hit(Rectangle rect,
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.
|
private void |
methodNotSupported() |
This method is called to indicate that a particular method is not
supported yet.
|
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(Color color) |
Sets the background colour 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(Shape clip) |
Sets the current clipping area to an arbitrary clip shape.
|
void |
setColor(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(Composite comp) |
Sets the Composite to be used by this EpsGraphics2D.
|
void |
setFont(Font font) |
Sets the Font to be used in future text.
|
void |
setPaint(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 colour.
|
void |
setRenderingHint(RenderingHints.Key hintKey,
Object hintValue) |
Sets a rendering hint.
|
void |
setRenderingHints(Map hints) |
Sets the rendering hints.
|
void |
setStroke(Stroke s) |
Sets the stroke.
|
void |
setTransform(AffineTransform Tx) |
Sets the AffineTransform to be used by this EpsGraphics2D.
|
void |
setXORMode(Color c1) |
Not implemented - performs no action.
|
void |
shear(double shx,
double shy) |
Concatenates the current EpsGraphics2D Transform with a shearing transform.
|
private String |
toHexString(int n) |
Returns a hex string that always contains two characters.
|
String |
toString() |
Returns the entire contents of the EPS document, complete with headers
and bounding box.
|
private Point2D |
transform(float x,
float y) |
Returns the point after it has been transformed by the transformation.
|
void |
transform(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.
|
getClipRectpublic static final String VERSION
public static final int BLACK_AND_WHITE
public static final int GRAYSCALE
public static final int RGB
private Color _color
private Color _backgroundColor
private Paint _paint
private Composite _composite
private BasicStroke _stroke
private Font _font
private Shape _clip
private AffineTransform _clipTransform
private AffineTransform _transform
private boolean _accurateTextMode
private int _colorDepth
private EpsDocument _document
private static FontRenderContext _fontRenderContext
public EpsGraphics2D()
public EpsGraphics2D(String title)
public EpsGraphics2D(String title, File file, int minX, int minY, int maxX, int maxY) throws IOException
IOExceptionpublic EpsGraphics2D(String title, OutputStream outputStream, int minX, int minY, int maxX, int maxY) throws IOException
IOExceptionprotected EpsGraphics2D(EpsGraphics2D g)
private void methodNotSupported()
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 IOException
IOExceptionpublic void close()
throws IOException
IOExceptionprivate void append(String line)
private Point2D transform(float x, float y)
private void draw(Shape s, String action)
private String toHexString(int n)
public void draw3DRect(int x,
int y,
int width,
int height,
boolean raised)
draw3DRect in class Graphics2Dpublic void fill3DRect(int x,
int y,
int width,
int height,
boolean raised)
fill3DRect in class Graphics2Dpublic void draw(Shape s)
draw in class Graphics2Dpublic boolean drawImage(Image img, AffineTransform xform, ImageObserver obs)
drawImage in class Graphics2Dpublic void drawImage(BufferedImage img, BufferedImageOp op, int x, int y)
drawImage in class Graphics2Dpublic void drawRenderedImage(RenderedImage img, AffineTransform xform)
drawRenderedImage in class Graphics2Dpublic void drawRenderableImage(RenderableImage img, AffineTransform xform)
drawRenderableImage in class Graphics2Dpublic void drawString(String str, int x, int y)
drawString in class Graphics2Dpublic void drawString(String s, float x, float y)
drawString in class Graphics2Dpublic void drawString(AttributedCharacterIterator iterator, int x, int y)
drawString in class Graphics2Dpublic void drawString(AttributedCharacterIterator iterator, float x, float y)
drawString in class Graphics2Dpublic void drawGlyphVector(GlyphVector g, float x, float y)
drawGlyphVector in class Graphics2Dpublic void fill(Shape s)
fill in class Graphics2Dpublic boolean hit(Rectangle rect, Shape s, boolean onStroke)
hit in class Graphics2Dpublic GraphicsConfiguration getDeviceConfiguration()
getDeviceConfiguration in class Graphics2Dpublic void setComposite(Composite comp)
setComposite in class Graphics2Dpublic void setPaint(Paint paint)
setPaint in class Graphics2Dpublic void setStroke(Stroke s)
setStroke in class Graphics2Dpublic void setRenderingHint(RenderingHints.Key hintKey, Object hintValue)
setRenderingHint in class Graphics2Dpublic Object getRenderingHint(RenderingHints.Key hintKey)
getRenderingHint in class Graphics2Dpublic void setRenderingHints(Map hints)
setRenderingHints in class Graphics2Dpublic void addRenderingHints(Map hints)
addRenderingHints in class Graphics2Dpublic RenderingHints getRenderingHints()
getRenderingHints in class Graphics2Dpublic void translate(int x,
int y)
translate in class Graphics2Dpublic void translate(double tx,
double ty)
translate in class Graphics2Dpublic void rotate(double theta)
rotate in class Graphics2Dpublic void rotate(double theta,
double x,
double y)
rotate in class Graphics2Dpublic void scale(double sx,
double sy)
scale in class Graphics2Dpublic void shear(double shx,
double shy)
shear in class Graphics2Dpublic void transform(AffineTransform Tx)
transform in class Graphics2Dpublic void setTransform(AffineTransform Tx)
setTransform in class Graphics2Dpublic AffineTransform getTransform()
getTransform in class Graphics2Dpublic Paint getPaint()
getPaint in class Graphics2Dpublic Composite getComposite()
getComposite in class Graphics2Dpublic void setBackground(Color color)
setBackground in class Graphics2Dpublic Color getBackground()
getBackground in class Graphics2Dpublic Stroke getStroke()
getStroke in class Graphics2Dpublic void clip(Shape s)
clip in class Graphics2Dpublic FontRenderContext getFontRenderContext()
getFontRenderContext in class Graphics2Dpublic Graphics create()
public Graphics create(int x, int y, int width, int height)
public Color getColor()
public void setColor(Color c)
public void setPaintMode()
setPaintMode in class Graphicspublic void setXORMode(Color c1)
setXORMode in class Graphicspublic Font getFont()
public void setFont(Font font)
public FontMetrics getFontMetrics()
getFontMetrics in class Graphicspublic FontMetrics getFontMetrics(Font f)
getFontMetrics in class Graphicspublic Rectangle getClipBounds()
getClipBounds in class Graphicspublic void clipRect(int x,
int y,
int width,
int height)
public void setClip(int x,
int y,
int width,
int height)
public Shape getClip()
public void setClip(Shape clip)
public void copyArea(int x,
int y,
int width,
int height,
int dx,
int dy)
public void drawLine(int x1,
int y1,
int x2,
int y2)
public void fillRect(int x,
int y,
int width,
int height)
public void drawRect(int x,
int y,
int width,
int height)
public void clearRect(int x,
int y,
int width,
int height)
public void drawRoundRect(int x,
int y,
int width,
int height,
int arcWidth,
int arcHeight)
drawRoundRect in class Graphicspublic void fillRoundRect(int x,
int y,
int width,
int height,
int arcWidth,
int arcHeight)
fillRoundRect in class Graphicspublic void drawOval(int x,
int y,
int width,
int height)
public void fillOval(int x,
int y,
int width,
int height)
public void drawArc(int x,
int y,
int width,
int height,
int startAngle,
int arcAngle)
public void fillArc(int x,
int y,
int width,
int height,
int startAngle,
int arcAngle)
public void drawPolyline(int[] xPoints,
int[] yPoints,
int nPoints)
drawPolyline in class Graphicspublic void drawPolygon(int[] xPoints,
int[] yPoints,
int nPoints)
drawPolygon in class Graphicspublic void drawPolygon(Polygon p)
drawPolygon in class Graphicspublic void fillPolygon(int[] xPoints,
int[] yPoints,
int nPoints)
fillPolygon in class Graphicspublic void fillPolygon(Polygon p)
fillPolygon in class Graphicspublic void drawChars(char[] data,
int offset,
int length,
int x,
int y)
public void drawBytes(byte[] data,
int offset,
int length,
int x,
int y)
public boolean drawImage(Image img, int x, int y, ImageObserver observer)
public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer)
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
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)
public void dispose()
public void finalize()
public String toString()
public boolean hitClip(int x,
int y,
int width,
int height)
public Rectangle getClipBounds(Rectangle r)
getClipBounds in class Graphics