Class AbstractRenderer<T extends IChemObject>

Direct Known Subclasses:
AtomContainerRenderer, ChemModelRenderer, MoleculeSetRenderer, ReactionRenderer, ReactionSetRenderer

public abstract class AbstractRenderer<T extends IChemObject> extends Object

The base class for all renderers, handling the core aspects of rendering such as the location of the model in 'model space' and the location on the screen to draw the model. It also holds a reference to the list of IGenerator instances that are used to create the diagram. These generators are accessed through the generateDiagram method.

The terminology 'model space' and 'screen space' refer to the coordinate systems for the model and the drawing, respectively. So the 2D points for atoms in the model might be 1 unit apart (roughly representing Ångstrom, perhaps) but the circles in the diagram that represent those atoms might be 10 pixels apart on screen. Therefore screen space will be 10 times model space for this example.

The abstract method calculateScaleForBondLength(double) is needed to determine the scale. For the model example just given, this would return '10.0' for an input of '10.0', as that is the scale for that desired bond length.

Source code:
Belongs to CDK module:
  • Field Details

    • rendererModel

      protected final RendererModel rendererModel
      The renderer model is final as it is not intended to be replaced.
    • fontManager

      protected IFontManager fontManager
      Font managers change the font size depending on the zoom.
    • modelCenter

      protected javax.vecmath.Point2d modelCenter
      The center point of the model (IAtomContainer, IAtomContainerSet, etc).
    • drawCenter

      protected javax.vecmath.Point2d drawCenter
      The center of the desired position on screen to draw.
    • generators

      protected List<IGenerator<T extends IChemObject>> generators
      Generators for diagram elements.
    • cachedDiagram

      protected IRenderingElement cachedDiagram
      Used when repainting an unchanged model.
    • transform

      protected AffineTransform transform
      Converts between model coordinates and screen coordinates.
  • Constructor Details

    • AbstractRenderer

      public AbstractRenderer(RendererModel rendererModel)
  • Method Details

    • generateDiagram

      public IRenderingElement generateDiagram(T object)
      The main method of the renderer, that uses each of the generators to create a different set of IRenderingElements grouped together into a tree.
      object - the object of type T to draw
      the diagram as a tree of IRenderingElements
    • calculateScaleForBondLength

      public abstract double calculateScaleForBondLength(double bondLength)
      Calculate the scale to convert the model bonds into bonds of the length supplied. A standard way to do this is to calculate the average bond length (mean, or median) in model space, and divide the supplied screen distance by this average to give a scale.
      bondLength - the desired length on screen
      a multiplication factor, or 'scale'
    • calculateScreenBounds

      public Rectangle calculateScreenBounds(Rectangle2D modelBounds)
      Converts a bounding rectangle in 'model space' into the equivalent bounds in 'screen space'. Used to determine how much space the model will take up on screen given a particular scale, zoom, and margin.
      modelBounds - the bounds of the model
      the bounds of the diagram as drawn on screen
    • toModelCoordinates

      public javax.vecmath.Point2d toModelCoordinates(double screenX, double screenY)
      Convert a point in screen space into a point in model space.
      screenX - the screen x-coordinate
      screenY - the screen y-coordinate
      the equivalent point in model space, or (0,0) if there is an error
    • toScreenCoordinates

      public javax.vecmath.Point2d toScreenCoordinates(double modelX, double modelY)
      Convert a point in model space into a point in screen space.
      modelX - the model x-coordinate
      modelY - the model y-coordinate
      the equivalent point in screen space
    • setModelCenter

      public void setModelCenter(double modelX, double modelY)
      Set the position of the center of the model.
      modelX - the x-coordinate of the model center
      modelY - the y-coordinate of the model center
    • setDrawCenter

      public void setDrawCenter(double modelX, double modelY)
      Set the point on the screen to draw the diagram.
      modelX - the x-coordinate of the point to draw at
      modelY - the y-coordinate of the point to draw at
    • setZoom

      public void setZoom(double zoom)
      Set the zoom, where 1.0 is 100% zoom.
      zoom - the zoom as a double value
    • setup

      protected void setup()
      Creates the transform using the scale, zoom, drawCenter, and modelCenter. In order to scale (and zoom) all elements in a uniform way, a point is first moved so that the center of the model is at the origin, scaled, then moved to the correct place on screen.
    • getRenderer2DModel

      public RendererModel getRenderer2DModel()
      Get the RendererModel used by this renderer, which provides access to the various parameters used to generate and draw the diagram.
      a reference to the RendererModel
    • shiftDrawCenter

      public void shiftDrawCenter(double shiftX, double shiftY)
      Move the draw center by dx and dy.
      shiftX - the x shift
      shiftY - the y shift
    • getDrawCenter

      public javax.vecmath.Point2d getDrawCenter()
      Get the position on screen that the diagram will be drawn.
      the draw center
    • getModelCenter

      public javax.vecmath.Point2d getModelCenter()
      Get the center of the model.
      the model center
    • setZoomToFit

      public void setZoomToFit(double drawWidth, double drawHeight, double diagramWidth, double diagramHeight)
      Calculate and set the zoom factor needed to completely fit the diagram onto the screen bounds.
      drawWidth - the width of the area to draw onto
      drawHeight - the height of the area to draw onto
      diagramWidth - the width of the diagram
      diagramHeight - the height of the diagram
    • repaint

      public void repaint(IDrawVisitor drawVisitor)
      Repaint using the cached diagram.
      drawVisitor - the wrapper for the graphics object that draws
    • paint

      protected void paint(IDrawVisitor drawVisitor, IRenderingElement diagram)
      The target method for paintChemModel, paintReaction, and paintMolecule.
      drawVisitor - the visitor to draw with
      diagram - the IRenderingElement tree to render
    • setupTransformNatural

      protected void setupTransformNatural(Rectangle2D modelBounds)
      Set the transform for a non-fit to screen paint.
      modelBounds - the bounding box of the model
    • shift

      public Rectangle shift(Rectangle screenBounds, Rectangle diagramBounds)
      Determine the overlap of the diagram with the screen, and shift (if necessary) the diagram draw center. It returns a rectangle only because that is a convenient class to hold the four parameters calculated, but it is not a rectangle representing an area...
      screenBounds - the bounds of the screen
      diagramBounds - the bounds of the diagram
      the shape that the screen should be
    • convertToDiagramBounds

      protected Rectangle convertToDiagramBounds(Rectangle2D modelBounds)
      Calculate the bounds of the diagram on screen, given the current scale, zoom, and margin.
      modelBounds - the bounds in model space of the chem object
      the bounds in screen space of the drawn diagram
    • setupTransformToFit

      protected void setupTransformToFit(Rectangle2D screenBounds, Rectangle2D modelBounds, double bondLength, boolean reset)
      Sets the transformation needed to draw the model on the canvas when the diagram needs to fit the screen.
      screenBounds - the bounding box of the draw area
      modelBounds - the bounding box of the model
      bondLength - the average bond length of the model
      reset - if true, model center will be set to the modelBounds center and the scale will be re-calculated
    • getBounds

      public Rectangle2D getBounds(IRenderingElement element)
      Given a rendering element, traverse the elements compute required bounds to full display all elements. The method searches for Bounds elements which act to specify the required bounds when adjunct labels are considered.
      element - a rendering element
      the bounds required (null if unspecified)