org.faceless.graph2
Class ImageOutput

java.lang.Object
  extended by org.faceless.graph2.Output
      extended by org.faceless.graph2.ImageOutput

public class ImageOutput
extends Output

The ImageOutput class is a subclass of Output, which allows Graphs to be drawn to a 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"


Constructor Summary
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.
 
Method Summary
 Map getAreas()
          Return a Map containing information about the areas used in the graph.
 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 storeAreas(boolean store)
          Whether to store the areas used by each object on the Graph.
 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.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 pixels
height - 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 pixels
height - the height of the image in pixels
background - the color to use as the background color, or null 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 specified 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.

Parameters:
graphics - the Graphics2D object to render to
width - the width of the Graphics2D object
height - 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 pixels
left - the left margin in pixels
right - the right margin in pixels
bottom - 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 to TextStyle.setFont(java.lang.String, double). A name of "Default" will override the default font. The name is case-insensitive
font - the Font to use

getImage

public BufferedImage getImage()
Return the Image created by the 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.


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 the ImageOutput(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 256
mask - 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.

writePNG

public void writePNG(OutputStream out,
                     int numcolors)
              throws IOException
Write the image as a PNG to the specified OutputStream. Calls writePNG(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 specified 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

Parameters:
out - the OutputStream to write to
numcolors - 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()

getAreas

public Map getAreas()
Return a Map containing information about the areas used in the graph. This can be used to create rollover or clickable areas on the resulting image. The returned map has a String as a key and an Area or GeneralPath as a value. The key determines which object is being described, and is in one of the following formats:
graph.series.name Represents the Area used by the actual series, where name is one (or more) fields describing the actual data, separated by dots. For instance, if you created a series like this:
 BarSeries s = new BarSeries("myseries");
 s.set("Apples", 20);
 s.set("Oranges", 20);
 
Then this Map would include at least the two keys graph.series.myseries.Apples and graph.series.myseries.Oranges. For stacked bars, you might get graph.series.myseries.2001.Apples, and for LineGraphs, where there is no real division of the data, you would simply get graph.series.myseries.
trace.graph.series.name This is a GeneralPath object representing the course actually taken by the data, which may be different the the value returned by graph.series.name if, for instance, the graph is drawn with a linethickness > 1 or is rotated in 3D.
graph.marker.name Represents the Area used by a Marker on the graph. Here series is the series the marker was added to, and name is the name of the marker. If no name was specified for the marker (the default) then no entry will be added to the Map.
key.frame Represents the Area used by the Key box.

Returns:
the Map described above, or null if storeAreas(true) wasn't called before drawing.

storeAreas

public void storeAreas(boolean store)
Whether to store the areas used by each object on the Graph. This is useful for creating clickable areas in the graph, but it adds a small amount of time to the rendering process so it's not turned on by default. See the getAreas() method for more information.

Parameters:
store - whether to store information required by the getAreas() method.
See Also:
getAreas()


Copyright © 2001-2011 Big Faceless Organization