package com.macmillan.nmeyers; import javax.swing.*; import java.awt.*; import java.awt.event.*; /* BackingStore - A utility class to implement a backing store for graphics rendering performance enhancement. Copyright (c) 1999 Nathan Meyers $Id: BackingStore.java,v 1.4 1999/11/10 02:48:13 nathanm Exp $ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * Implement a container with graphical backing store. * This container, usable in a lightweight window hierarchy, * allows an application to reduce its workload in handling * repaint requests by satisfying those requests, when possible, * from a backing store buffer. *
* Customers of this class only need one method - the * {@link BackingStore#BackingStore constructor}. All other * methods are overrides of component methods required to implement * the backing store functionality. */ public class BackingStore extends Container { /** * Indicates whether a repaint request came through application- * requested repainting (true) or a window system exposure * event (false). */ static boolean repainting = false; /** * Indicates whether this class has already installed * its global repaint handler. */ static boolean installed = false; /** * The backing store buffer. * @serial */ Image bstore = null; /** * The lightweight component for which backing store services * are being provide. * @serial */ Component component; /** * This class subclasss and replace the standard repaint manager * one that assists the BackingStore class in its decision-making. */ private class BSRepaintManager extends RepaintManager { /** * Subclass override of * {@link RepaintManager#paintDirtyRegions()} * allows BackingStore to ascertain whether it is called * for application-requested repaint or for a window * system exposure event. */ public void paintDirtyRegions() { repainting = true; try { super.paintDirtyRegions(); } finally { repainting = false; } } } /** * Constructs a backing store handler for the requested lightweight * component. This is the only method you need to call. All other * methods are overrides of component methods required to implement * the backing store functionality. * @param comp The component for which backing store is being provided. */ public BackingStore(Component comp) { if (!installed) { installed = true; RepaintManager.setCurrentManager(new BSRepaintManager()); } component = comp; add(component); component.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { bstore = null; } }); Dimension size = component.getSize(); setSize(size); } /** * This wraps the BackingStore window tightly around the * window being managed by setting insets to 0. */ public Insets getInsets() { return new Insets(0, 0, 0, 0); } /** * This calls the managed component's version of this method. */ public Dimension getMinimumSize() { return component.getMinimumSize(); } /** * This calls the managed component's version of this method. */ public Dimension getMaximumSize() { return component.getMaximumSize(); } /** * This calls the managed component's version of this method. */ public Dimension getPreferredSize() { return component.getPreferredSize(); } /** * This lays out the managed component by sizing it to fill * the BackingStore window. */ public void doLayout() { Dimension size = getSize(); component.setBounds(0, 0, size.width, size.height); } /** * The core logic of BackingStore, this method repaints either from * the backing store buffer or by calling the managed object to * repaint. */ public void paint(Graphics g) { boolean doRepaint = repainting; if (bstore == null) { Dimension size = component.getSize(); bstore = component.createImage(size.width, size.height); doRepaint = true; } if (doRepaint) { Graphics g2 = bstore.getGraphics().create(); g2.setClip(component.getBounds()); g2.setFont(g.getFont()); g2.setColor(g.getColor()); component.paint(g2); g2.dispose(); } g.drawImage(bstore, 0, 0, this); } }