From: <gil...@us...> - 2008-12-05 12:31:09
|
Revision: 13489 http://cdk.svn.sourceforge.net/cdk/?rev=13489&view=rev Author: gilleain Date: 2008-12-05 12:31:06 +0000 (Fri, 05 Dec 2008) Log Message: ----------- Implemented caching of the intermediate representation so that a call to repaint(g) will not regenerate from the chem model. This should speed up operations like scrolling. Modified Paths: -------------- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/renderer/IntermediateRenderer.java jchempaint/trunk/src/main/org/openscience/jchempaint/RenderPanel.java Modified: cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/renderer/IntermediateRenderer.java =================================================================== --- cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/renderer/IntermediateRenderer.java 2008-12-05 10:24:44 UTC (rev 13488) +++ cdk/branches/jchempaint-primary/src/main/org/openscience/cdk/renderer/IntermediateRenderer.java 2008-12-05 12:31:06 UTC (rev 13489) @@ -70,6 +70,8 @@ private double scale = NATURAL_SCALE; + private IRenderingElement cachedDiagram; + /** * If true, the renderer will calculate a scale that will fit * the molecule into the drawBounds, as well as centring it there. @@ -198,6 +200,9 @@ // generate the elements IRenderingElement diagram = this.generateDiagram(moleculeSet); + // cache the diagram for quick-redraw + this.cachedDiagram = diagram; + // setup the scale and translation for the transform this.setupDrawArea(bounds, modelBounds); if (resetCenter) { @@ -207,6 +212,15 @@ // finally, paint it this.paint(g, diagram); } + + /** + * Repaint using the cached diagram + * + * @param g a Graphics2D object to paint with + */ + public void repaint(Graphics2D g) { + this.paint(g, cachedDiagram); + } /** * Paint an atom container at the natural scale, with Modified: jchempaint/trunk/src/main/org/openscience/jchempaint/RenderPanel.java =================================================================== --- jchempaint/trunk/src/main/org/openscience/jchempaint/RenderPanel.java 2008-12-05 10:24:44 UTC (rev 13488) +++ jchempaint/trunk/src/main/org/openscience/jchempaint/RenderPanel.java 2008-12-05 12:31:06 UTC (rev 13489) @@ -55,12 +55,19 @@ public class RenderPanel extends JPanel implements IViewEventRelay { private IntermediateRenderer renderer; + private boolean isNewChemModel; + private ControllerHub hub; + private ControllerModel controllerModel; + private SwingMouseEventRelay mouseEventRelay; + private boolean fitToScreen; + private boolean shouldPaintFromCache = false; + public RenderPanel(IChemModel chemModel, int width, int height, boolean fitToScreen) { this.setupMachinery(chemModel, fitToScreen); @@ -115,23 +122,20 @@ .getScreenDevices()[0] .getDefaultConfiguration() .createCompatibleImage(bounds.width, bounds.height); - Graphics g = image.getGraphics(); + Graphics2D g = (Graphics2D)image.getGraphics(); this.paintChemModel(g, bounds); return image; } - public void paintChemModel(Graphics g, Rectangle screenBounds) { - super.paint(g); + public void paintChemModel(Graphics2D g, Rectangle screenBounds) { + IChemModel chemModel = this.hub.getIChemModel(); if (chemModel != null && chemModel.getMoleculeSet() != null) { // determine the size the canvas needs to be in order to fit the model Rectangle diagramBounds = renderer.calculateScreenBounds(chemModel); -// ((Graphics2D)g).draw(diagramBounds); -// System.err.println("sb = " + diagramBounds + " b=" + screenBounds); if (this.overlaps(screenBounds, diagramBounds)) { -// System.err.println("overlap"); Rectangle union = screenBounds.union(diagramBounds); this.setPreferredSize(union.getSize()); this.revalidate(); @@ -154,15 +158,17 @@ || screenBounds.getMaxY() < diagramBounds.getMaxY(); } - private void paintChemModel(IChemModel chemModel, Graphics g, Rectangle bounds) { - // set the graphics to antialias - Graphics2D g2 = (Graphics2D)g; - g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); - + private void paintChemModel(IChemModel chemModel, Graphics2D g, Rectangle bounds) { + // paint the chem model, and record that it is no longer new - renderer.paintChemModel(chemModel, g2, bounds, isNewChemModel); + renderer.paintChemModel(chemModel, g, bounds, isNewChemModel); isNewChemModel = false; + + /* + * This is dangerous, but necessary to allow fast + * repainting when scrolling the canvas + */ + this.shouldPaintFromCache = true; } public void setIsNewChemModel(boolean isNewChemModel) { @@ -170,11 +176,31 @@ } public void paint(Graphics g) { - this.setBackground(renderer.getRenderer2DModel().getBackColor()); - this.paintChemModel(g, this.getBounds()); + this.setBackground(renderer.getRenderer2DModel().getBackColor()); + super.paint(g); + // set the graphics to antialias + Graphics2D g2 = (Graphics2D)g; + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + + if (this.shouldPaintFromCache) { + this.paintFromCache(g2); + } else { + this.paintChemModel(g2, this.getBounds()); + } } + + private void paintFromCache(Graphics2D g) { + renderer.repaint(g); + } public void updateView() { + /* + * updateView should only be called in a ControllerModule where + * we assume that things have changed so we can't use the cache + */ + this.shouldPaintFromCache = false; + this.repaint(); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |