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.

    Author:
    maclean
    Source code:
    main
    Belongs to CDK module:
    renderbasic
    • Field Detail

      • 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.
      • cachedDiagram

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

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

      • AbstractRenderer

        public AbstractRenderer​(RendererModel rendererModel)
    • Method Detail

      • 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.
        Parameters:
        object - the object of type T to draw
        Returns:
        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.
        Parameters:
        bondLength - the desired length on screen
        Returns:
        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.
        Parameters:
        modelBounds - the bounds of the model
        Returns:
        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.
        Parameters:
        screenX - the screen x-coordinate
        screenY - the screen y-coordinate
        Returns:
        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.
        Parameters:
        modelX - the model x-coordinate
        modelY - the model y-coordinate
        Returns:
        the equivalent point in screen space
      • setModelCenter

        public void setModelCenter​(double modelX,
                                   double modelY)
        Set the position of the center of the model.
        Parameters:
        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.
        Parameters:
        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.
        Parameters:
        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.
        Returns:
        a reference to the RendererModel
      • shiftDrawCenter

        public void shiftDrawCenter​(double shiftX,
                                    double shiftY)
        Move the draw center by dx and dy.
        Parameters:
        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.
        Returns:
        the draw center
      • getModelCenter

        public javax.vecmath.Point2d getModelCenter()
        Get the center of the model.
        Returns:
        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.
        Parameters:
        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.
        Parameters:
        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.
        Parameters:
        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.
        Parameters:
        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...
        Parameters:
        screenBounds - the bounds of the screen
        diagramBounds - the bounds of the diagram
        Returns:
        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.
        Parameters:
        modelBounds - the bounds in model space of the chem object
        Returns:
        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.
        Parameters:
        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.
        Parameters:
        element - a rendering element
        Returns:
        the bounds required (null if unspecified)