Class AtomContainerRenderer

All Implemented Interfaces:

public class AtomContainerRenderer extends AbstractRenderer<IAtomContainer> implements IRenderer<IAtomContainer>
A general renderer for IAtomContainers. The chem object is converted into a 'diagram' made up of IRenderingElements. It takes an IDrawVisitor to do the drawing of the generated diagram. Various display properties can be set using the RendererModel.

This class has several usage patterns. For just painting fit-to-screen do:

   renderer.paintMolecule(molecule, visitor, drawArea)
for painting at a scale determined by the bond length in the RendererModel:
   if (moleculeIsNew) {
     renderer.setup(molecule, drawArea);
   Rectangle diagramSize = renderer.paintMolecule(molecule, visitor);
   // ...update scroll bars here
to paint at full screen size, but not resize with each change:
   if (moleculeIsNew) {
     Rectangle diagramBounds = renderer.calculateDiagramBounds(molecule);
     renderer.setZoomToFit(diagramBounds, drawArea);
     renderer.paintMolecule(molecule, visitor);
   } else {
     Rectangle diagramSize = renderer.paintMolecule(molecule, visitor);
   // ...update scroll bars here
finally, if you are scrolling, and have not changed the diagram:
will just repaint the previously generated diagram, at the same scale.

There are two sets of methods for painting IChemObjects - those that take a Rectangle that represents the desired draw area, and those that return a Rectangle that represents the actual draw area. The first are intended for drawing molecules fitted to the screen (where 'screen' means any drawing area) while the second type of method are for drawing bonds at the length defined by the RendererModel parameter bondLength.

There are two numbers used to transform the model so that it fits on screen. The first is scale, which is used to map model coordinates to screen coordinates. The second is zoom which is used to, well, zoom the on screen coordinates. If the diagram is fit-to-screen, then the ratio of the bounds when drawn using bondLength and the bounds of the screen is used as the zoom.

So, if the bond length on screen is set to 40, and the average bond length of the model is 2 (unitless, but roughly Ångstrom scale) then the scale will be 20. If the model is 10 units wide, then the diagram drawn at 100% zoom will be 10 * 20 = 200 in width on screen. If the screen is 400 pixels wide, then fitting it to the screen will make the zoom 200%. Since the zoom is just a floating point number, 100% = 1 and 200% = 2.

Source code:
Belongs to CDK module:
  • Field Details


      public static final double DEFAULT_SCALE
      The default scale is used when the model is empty.
      See Also:
  • Constructor Details

    • AtomContainerRenderer

      public AtomContainerRenderer(List<IGenerator<IAtomContainer>> generators, IFontManager fontManager)
      A renderer that generates diagrams using the specified generators and manages fonts with the supplied font manager.
      generators - a list of classes that implement the IGenerator interface
      fontManager - a class that manages mappings between zoom and font sizes
    • AtomContainerRenderer

      public AtomContainerRenderer(RendererModel rendererModel, List<IGenerator<IAtomContainer>> generators, IFontManager fontManager)
  • Method Details

    • setup

      public void setup(IAtomContainer atomContainer, Rectangle screen)
      Setup the transformations necessary to draw this Atom Container.
      Specified by:
      setup in interface IRenderer<IAtomContainer>
      atomContainer - the atom container to use in the setup
      screen - the area to draw on
    • reset

      public void reset()
      Reset the draw center and model center, and set the zoom to 100%.
    • setScale

      public void setScale(IAtomContainer atomContainer)
      Set the scale for an IAtomContainer. It calculates the average bond length of the model and calculates the multiplication factor to transform this to the bond length that is set in the RendererModel.
      Specified by:
      setScale in interface IRenderer<IAtomContainer>
      atomContainer - the atom container that will be drawn
    • paint

      public Rectangle paint(IAtomContainer atomContainer, IDrawVisitor drawVisitor)
      Paint an IChemObject.
      Specified by:
      paint in interface IRenderer<IAtomContainer>
      atomContainer - the chem object to paint
      drawVisitor - the class that visits the generated elements
      the rectangular area where was drawn
    • paint

      public void paint(IAtomContainer atomContainer, IDrawVisitor drawVisitor, Rectangle2D bounds, boolean resetCenter)
      Paint a molecule (an IAtomContainer).
      Specified by:
      paint in interface IRenderer<IAtomContainer>
      atomContainer - the molecule to paint
      drawVisitor - the visitor that does the drawing
      bounds - the bounds on the screen
      resetCenter - if true, set the draw center to be the center of bounds
    • calculateDiagramBounds

      public Rectangle calculateDiagramBounds(IAtomContainer atomContainer)
      Given a IChemObject, calculates the bounding rectangle in screen space.
      Specified by:
      calculateDiagramBounds in interface IRenderer<IAtomContainer>
      atomContainer - the IChemObject to draw.
      a rectangle in screen space.
    • calculateScaleForBondLength

      public double calculateScaleForBondLength(double modelBondLength)
      Given a bond length for a model, calculate the scale that will transform this length to the on screen bond length in RendererModel.
      Specified by:
      calculateScaleForBondLength in class AbstractRenderer<IAtomContainer>
      modelBondLength - the average bond length of the model
      the scale necessary to transform this to a screen bond
    • getGenerators

      public List<IGenerator<IAtomContainer>> getGenerators()
      Returns a List of IGenerators for this renderer.
      Specified by:
      getGenerators in interface IRenderer<IAtomContainer>
      the list of generators for this renderer.