public class ImageOutput extends Output
BufferedImage
or Graphics2D
object, and then optionally rendered to
a PNG image by calling the writePNG()
method. Most commonly, the
image will be rendered to a PNG like so:
Graph graph = makeMyGraph(); ImageOutput out = new ImageOutput(400,400); graph.draw(out); out.writePNG(outputstream, 256);It's also possible to get at the generated image by calling
getImage()
for use in an applet or similar, or to write to a different file format.
This class has it's "Default" font set to the Font
"Helvetica"Modifier and Type | Field and Description |
---|---|
protected int |
height |
protected int |
width |
Constructor and Description |
---|
ImageOutput(Graphics2D graphics,
int width,
int height)
Create a new ImageOutput which will draw to the specified
Graphics2D object. |
ImageOutput(int width,
int height)
Create a new ImageOutput of the specified width and height with a white background
|
ImageOutput(int width,
int height,
Paint background)
Create a new ImageOutput of the specified width, height and background color.
|
Modifier and Type | Method and Description |
---|---|
BufferedImage |
getImage()
Return the Image created by the
Graph.draw(org.faceless.graph2.Output) method. |
BufferedImage |
getReducedColorImage(int numcolors,
Color mask)
Return the Image created by the
Graph.draw(org.faceless.graph2.Output) method, after
it's been reduced to the specified number of colors. |
void |
setFont(String name,
Font font)
Define a custom mapping from the specified font description
to the specified font.
|
void |
setMargin(int left,
int top,
int right,
int bottom)
Set the margin between the graph and the edge of the image.
|
void |
setRenderingHint(RenderingHints.Key key,
Object val)
Set a rendering hint for drawing the image.
|
void |
writePNG(OutputStream out,
int numcolors)
Write the image as a PNG to the specified
OutputStream . |
void |
writePNG(OutputStream out,
int numcolors,
int dpi)
Write the image as a PNG to the specified
OutputStream . |
getAreas, storeAreas
public ImageOutput(int width, int height)
width
- the width of the image in pixelsheight
- the height of the image in pixelspublic ImageOutput(int width, int height, Paint background)
width
- the width of the image in pixelsheight
- the height of the image in pixelsbackground
- the color to use as the background color, or null
for no background (ie. the background is transparent)public ImageOutput(Graphics2D graphics, int width, int height)
Graphics2D
object.
This constructor can be used when the graph is to be drawn to an existing object, such as a
Swing component. Usually the setMargin
method is called as well, to
control the area of the graphics object to write to.graphics
- the Graphics2D object to render towidth
- the width of the Graphics2D objectheight
- the height of the Graphics2D objectpublic void setRenderingHint(RenderingHints.Key key, Object val)
RenderingHints.KEY_ANTIALIASING = RenderingHints.VALUE_ANTIALIAS_ON RenderingHints.KEY_TEXT_ANTIALIASING = RenderingHints.VALUE_TEXT_ANTIALIAS_ON RenderingHints.KEY_RENDERING = RenderingHints.VALUE_RENDER_QUALITY RenderingHints.KEY_INTERPOLATION = RenderingHints.VALUE_INTERPOLATION_BICUBIC
public void setMargin(int left, int top, int right, int bottom)
top
- the top margin in pixelsleft
- the left margin in pixelsright
- the right margin in pixelsbottom
- the bottom margin in pixelspublic void setFont(String name, Font font)
output.setFont("myfont", Font.createFont("myfont.ttf"));
name
- the name of the font, as passed to TextStyle.setFont(java.lang.String, double)
. A
name of "Default" will override the default font. The name is case-insensitivefont
- the Font to usepublic BufferedImage getImage()
Graph.draw(org.faceless.graph2.Output)
method.
Note that if this ImageOutput
was created by calling the
ImageOutput(Graphics2D,int,int)
constructor, this
method will return null
.public BufferedImage getReducedColorImage(int numcolors, Color mask)
Return the Image created by the Graph.draw(org.faceless.graph2.Output)
method, after
it's been reduced to the specified number of colors. This is useful
for saving to bitmap formats like GIF and 8-bit PNG. The color-reduction
algorithm is fairly timeconsuming.
Note that if this ImageOutput
was created by calling the
ImageOutput(Graphics2D,int,int)
constructor, this
method will return null.
numcolors
- the number of colors to reduce the image to - must
be a power of two between 2 and 256mask
- the Color to mask transparent colors against during the
color reduction. Generally this is the background color you intend to
display the graph on, but it may be null
for no masking.public void writePNG(OutputStream out, int numcolors) throws IOException
OutputStream
.
Calls writePNG(out, numcolors, 0)
.IOException
public void writePNG(OutputStream out, int numcolors, int dpi) throws IOException
OutputStream
.
If a partially transparent background color was used and numcolors
is
not zero, the background color will be changed to to fully transparent in order to
display correctly in Internet Explorer (which has broken PNG alpha support).
Note that if this ImageOutput
was created by calling the
ImageOutput(Graphics2D,int,int)
constructor, this method will
throw an IllegalStateException
out
- the OutputStream to write tonumcolors
- the number of colors to reduce the image to. This may be 0, to write
a full 24-bit RGB image, or a power of 2 between 2 and 256 to write an 8-bit indexed
image (which gives a smaller file but is more time-consuming to produce).dpi
- The DPI of the image, or 0 for unspecified. This has
no effect on the size of the image created: it simply writes a chunk to the
PNG which states the intended resolution of the image, which may be helpful in some
workflows.IOException
Graph.setMetaData()
Copyright © 2001-2016 Big Faceless Organization