Class 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 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
      • 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,
                             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()