Source for org.jfree.ui.DrawablePanel

   1: /**
   2:  * ========================================
   3:  * JCommon : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://www.jfree.org/jcommon/
   7:  * Project Lead:  Thomas Morgner;
   8:  *
   9:  * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
  10:  *
  11:  * This library is free software; you can redistribute it and/or modify it under the terms
  12:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  13:  * either version 2.1 of the License, or (at your option) any later version.
  14:  *
  15:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  16:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17:  * See the GNU Lesser General Public License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public License along with this
  20:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  21:  * Boston, MA 02111-1307, USA.
  22:  *
  23:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24:  * in the United States and other countries.]
  25:  *
  26:  * ------------
  27:  * DrawablePanel.java
  28:  * ------------
  29:  * (C) Copyright 2002-2006, by Object Refinery Limited.
  30:  *
  31:  * Original Author:  Thomas Morgner;
  32:  * Contributor(s):   -;
  33:  *
  34:  * $Id: DrawablePanel.java,v 1.1 2006/06/28 17:14:36 taqua Exp $
  35:  *
  36:  * Changes
  37:  * -------
  38:  *
  39:  *
  40:  */
  41: package org.jfree.ui;
  42: 
  43: import java.awt.Graphics;
  44: import java.awt.Graphics2D;
  45: import java.awt.Rectangle;
  46: import javax.swing.JComponent;
  47: 
  48: /**
  49:  * A component, that accepts a drawable and which draws that drawable.
  50:  *
  51:  * @author Thomas Morgner
  52:  */
  53: public class DrawablePanel extends JComponent
  54: {
  55:   private Drawable drawable;
  56: 
  57:   public DrawablePanel()
  58:   {
  59:     setOpaque(false);
  60:   }
  61: 
  62:   public Drawable getDrawable()
  63:   {
  64:     return drawable;
  65:   }
  66: 
  67:   public void setDrawable(final Drawable drawable)
  68:   {
  69:     this.drawable = drawable;
  70:   }
  71: 
  72:   /**
  73:    * Returns true if this component is completely opaque.
  74:    * <p/>
  75:    * An opaque component paints every pixel within its rectangular bounds. A
  76:    * non-opaque component paints only a subset of its pixels or none at all,
  77:    * allowing the pixels underneath it to "show through".  Therefore, a
  78:    * component that does not fully paint its pixels provides a degree of
  79:    * transparency.
  80:    * <p/>
  81:    * Subclasses that guarantee to always completely paint their contents should
  82:    * override this method and return true.
  83:    *
  84:    * @return true if this component is completely opaque
  85:    * @see #setOpaque
  86:    */
  87:   public boolean isOpaque()
  88:   {
  89:     if (drawable == null)
  90:     {
  91:       return false;
  92:     }
  93:     return super.isOpaque();
  94:   }
  95: 
  96:   /**
  97:    * Calls the UI delegate's paint method, if the UI delegate is
  98:    * non-<code>null</code>.  We pass the delegate a copy of the
  99:    * <code>Graphics</code> object to protect the rest of the paint code from
 100:    * irrevocable changes (for example, <code>Graphics.translate</code>).
 101:    * <p/>
 102:    * If you override this in a subclass you should not make permanent changes to
 103:    * the passed in <code>Graphics</code>. For example, you should not alter the
 104:    * clip <code>Rectangle</code> or modify the transform. If you need to do
 105:    * these operations you may find it easier to create a new
 106:    * <code>Graphics</code> from the passed in <code>Graphics</code> and
 107:    * manipulate it. Further, if you do not invoker super's implementation you
 108:    * must honor the opaque property, that is if this component is opaque, you
 109:    * must completely fill in the background in a non-opaque color. If you do not
 110:    * honor the opaque property you will likely see visual artifacts.
 111:    * <p/>
 112:    * The passed in <code>Graphics</code> object might have a transform other
 113:    * than the identify transform installed on it.  In this case, you might get
 114:    * unexpected results if you cumulatively apply another transform.
 115:    *
 116:    * @param g the <code>Graphics</code> object to protect
 117:    * @see #paint
 118:    * @see javax.swing.plaf.ComponentUI
 119:    */
 120:   protected void paintComponent(Graphics g)
 121:   {
 122:     if (drawable == null)
 123:     {
 124:       return;
 125:     }
 126: 
 127:     Rectangle bounds = getBounds();
 128: 
 129:     final Graphics2D g2 = (Graphics2D) g.create
 130:             (bounds.x, bounds.y, bounds.width, bounds.height);
 131: 
 132:     drawable.draw(g2, bounds);
 133:     g2.dispose();
 134:   }
 135: 
 136: }