Package org.faceless.graph2
Class ImageOutput
- java.lang.Object
-
- org.faceless.graph2.Output
-
- org.faceless.graph2.ImageOutput
-
public class ImageOutput extends Output
The ImageOutput class is a subclass of Output, which allows Graphs to be drawn to aBufferedImage
orGraphics2D
object, and then optionally rendered to a PNG image by calling thewritePNG()
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 callinggetImage()
for use in an applet or similar, or to write to a different file format. This class has it's "Default" font set to theFont
"Helvetica"
-
-
Constructor Summary
Constructors Constructor Description ImageOutput(int width, int height)
Create a new ImageOutput of the specified width and height with a white backgroundImageOutput(int width, int height, Paint background)
Create a new ImageOutput of the specified width, height and background color.ImageOutput(Graphics2D graphics, int width, int height)
Create a new ImageOutput which will draw to the specifiedGraphics2D
object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BufferedImage
getImage()
Return the Image created by theGraph.draw(org.faceless.graph2.Output)
method.BufferedImage
getReducedColorImage(int numcolors, Color mask)
Return the Image created by theGraph.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 specifiedOutputStream
.void
writePNG(OutputStream out, int numcolors, int dpi)
Write the image as a PNG to the specifiedOutputStream
.-
Methods inherited from class org.faceless.graph2.Output
getAreas, storeAreas
-
-
-
-
Constructor Detail
-
ImageOutput
public ImageOutput(int width, int height)
Create a new ImageOutput of the specified width and height with a white background- Parameters:
width
- the width of the image in pixelsheight
- the height of the image in pixels
-
ImageOutput
public ImageOutput(int width, int height, Paint background)
Create a new ImageOutput of the specified width, height and background color.- Parameters:
width
- the width of the image in pixelsheight
- the height of the image in pixelsbackground
- the color to use as the background color, ornull
for no background (ie. the background is transparent)
-
ImageOutput
public ImageOutput(Graphics2D graphics, int width, int height)
Create a new ImageOutput which will draw to the specifiedGraphics2D
object. This constructor can be used when the graph is to be drawn to an existing object, such as a Swing component. Usually thesetMargin
method is called as well, to control the area of the graphics object to write to.- Parameters:
graphics
- the Graphics2D object to render towidth
- the width of the Graphics2D objectheight
- the height of the Graphics2D object
-
-
Method Detail
-
setRenderingHint
public void setRenderingHint(RenderingHints.Key key, Object val)
Set a rendering hint for drawing the image. The following rendering hints are set by default: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
-
setMargin
public void setMargin(int left, int top, int right, int bottom)
Set the margin between the graph and the edge of the image. By default, these are all zero, so the graph expands to fill the entire image.- Parameters:
top
- the top margin in pixelsleft
- the left margin in pixelsright
- the right margin in pixelsbottom
- the bottom margin in pixels
-
setFont
public void setFont(String name, Font font)
Define a custom mapping from the specified font description to the specified font. Use this to use custom fonts in the graph, e.g.output.setFont("myfont", Font.createFont("myfont.ttf"));
- Parameters:
name
- the name of the font, as passed toTextStyle.setFont(java.lang.String, double)
. A name of "Default" will override the default font. The name is case-insensitivefont
- the Font to use
-
getImage
public BufferedImage getImage()
Return the Image created by theGraph.draw(org.faceless.graph2.Output)
method. Note that if thisImageOutput
was created by calling theImageOutput(Graphics2D,int,int)
constructor, this method will returnnull
.
-
getReducedColorImage
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 theImageOutput(Graphics2D,int,int)
constructor, this method will return null.- Parameters:
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 benull
for no masking.
-
writePNG
public void writePNG(OutputStream out, int numcolors) throws IOException
Write the image as a PNG to the specifiedOutputStream
. CallswritePNG(out, numcolors, 0)
.- Throws:
IOException
-
writePNG
public void writePNG(OutputStream out, int numcolors, int dpi) throws IOException
Write the image as a PNG to the specifiedOutputStream
. If a partially transparent background color was used andnumcolors
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 thisImageOutput
was created by calling theImageOutput(Graphics2D,int,int)
constructor, this method will throw anIllegalStateException
- Parameters:
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.- Throws:
IOException
- Since:
- 2.4
- See Also:
Graph.setMetaData()
-
-