Class HeadlessRenderContext

  • All Implemented Interfaces:
    SwingConstants, SequenceRenderContext

    public class HeadlessRenderContext
    extends Object
    implements SequenceRenderContext

    A stand-alone SequenceRenderContext to make it easy to render to an image.

    This class makes it very easy to render sequence information into an arbitrary graphics object without the need to fuss about with AWT or Swing components. You chose the width of the image and the region of the sequence to render. It will calculate the scale factor to ensure that the whole region of the sequence fits into that width. You can then use the context to render any number of SequenceRenderer instances to any Graphics2D instance you want, for example, to an image that's to be written out by a servlet.


     HeadlessRenderContext ctxt = new HeadlessRenderContext(
       seq,   // the sequence to render
       range, // a RangeLocation giving the block you want to render
       width  // an int specifying the image width in pixles
     BufferedImage img = new BufferedImage(
       width,                                   // image width
       (int) Math.ceil(seqRend.getDepth(ctxt),  // calculated height
       BufferedImage.TYPE_INT_RGB               // let's use RGB
     // set stuff up
     Graphics2D graph = img.createGraphics();
     graph.fillRect(0, 0, img.getWidth(), img.getHeight());
     // and now render the sequences
     sequenceRenderer.paint(graph, ctxt);
     // let's dump this out as a png
     ImageIO.write(image, "png", myFile);
    Matthew Pocock