Class AbstractRenderer<T extends IChemObject>
- java.lang.Object
-
- org.openscience.cdk.renderer.AbstractRenderer<T>
-
- 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 Summary
Fields Modifier and Type Field Description protected IRenderingElement
cachedDiagram
Used when repainting an unchanged model.protected javax.vecmath.Point2d
drawCenter
The center of the desired position on screen to draw.protected IFontManager
fontManager
Font managers change the font size depending on the zoom.protected List<IGenerator<T>>
generators
Generators for diagram elements.protected javax.vecmath.Point2d
modelCenter
The center point of the model (IAtomContainer, IAtomContainerSet, etc).protected RendererModel
rendererModel
The renderer model is final as it is not intended to be replaced.protected AffineTransform
transform
Converts between model coordinates and screen coordinates.
-
Constructor Summary
Constructors Constructor Description AbstractRenderer(RendererModel rendererModel)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract double
calculateScaleForBondLength(double bondLength)
Calculate the scale to convert the model bonds into bonds of the length supplied.Rectangle
calculateScreenBounds(Rectangle2D modelBounds)
Converts a bounding rectangle in 'model space' into the equivalent bounds in 'screen space'.protected Rectangle
convertToDiagramBounds(Rectangle2D modelBounds)
Calculate the bounds of the diagram on screen, given the current scale, zoom, and margin.IRenderingElement
generateDiagram(T object)
The main method of the renderer, that uses each of the generators to create a different set ofIRenderingElement
s grouped together into a tree.Rectangle2D
getBounds(IRenderingElement element)
Given a rendering element, traverse the elements compute required bounds to full display all elements.javax.vecmath.Point2d
getDrawCenter()
Get the position on screen that the diagram will be drawn.javax.vecmath.Point2d
getModelCenter()
Get the center of the model.RendererModel
getRenderer2DModel()
Get theRendererModel
used by this renderer, which provides access to the various parameters used to generate and draw the diagram.protected void
paint(IDrawVisitor drawVisitor, IRenderingElement diagram)
The target method for paintChemModel, paintReaction, and paintMolecule.void
repaint(IDrawVisitor drawVisitor)
Repaint using the cached diagram.void
setDrawCenter(double modelX, double modelY)
Set the point on the screen to draw the diagram.void
setModelCenter(double modelX, double modelY)
Set the position of the center of the model.protected void
setup()
Creates the transform using the scale, zoom, drawCenter, and modelCenter.protected void
setupTransformNatural(Rectangle2D modelBounds)
Set the transform for a non-fit to screen paint.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.void
setZoom(double zoom)
Set the zoom, where 1.0 is 100% zoom.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.Rectangle
shift(Rectangle screenBounds, Rectangle diagramBounds)
Determine the overlap of the diagram with the screen, and shift (if necessary) the diagram draw center.void
shiftDrawCenter(double shiftX, double shiftY)
Move the draw center by dx and dy.javax.vecmath.Point2d
toModelCoordinates(double screenX, double screenY)
Convert a point in screen space into a point in model space.javax.vecmath.Point2d
toScreenCoordinates(double modelX, double modelY)
Convert a point in model space into a point in screen space.
-
-
-
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.
-
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 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 ofIRenderingElement
s grouped together into a tree.- Parameters:
object
- the object of type T to draw- Returns:
- the diagram as a tree of
IRenderingElement
s
-
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-coordinatescreenY
- 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-coordinatemodelY
- 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 centermodelY
- 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 atmodelY
- 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 theRendererModel
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 shiftshiftY
- 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 ontodrawHeight
- the height of the area to draw ontodiagramWidth
- the width of the diagramdiagramHeight
- 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 withdiagram
- 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 screendiagramBounds
- 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 areamodelBounds
- the bounding box of the modelbondLength
- the average bond length of the modelreset
- 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 forBounds
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)
-
-