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.2 2006/11/17 20:09:50 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.Dimension;
  46: import java.awt.geom.Rectangle2D;
  47: import javax.swing.JPanel;
  48: 
  49: import org.jfree.util.Log;
  50: 
  51: /**
  52:  * A component, that accepts a drawable and which draws that drawable.
  53:  *
  54:  * @author Thomas Morgner
  55:  */
  56: public class DrawablePanel extends JPanel
  57: {
  58:   private Drawable drawable;
  59: 
  60:   public DrawablePanel()
  61:   {
  62:     setOpaque(false);
  63:   }
  64: 
  65:   public Drawable getDrawable()
  66:   {
  67:     return drawable;
  68:   }
  69: 
  70:   public void setDrawable(final Drawable drawable)
  71:   {
  72:     this.drawable = drawable;
  73:     revalidate();
  74:     repaint();
  75:   }
  76: 
  77:   /**
  78:    * If the <code>preferredSize</code> has been set to a non-<code>null</code>
  79:    * value just returns it. If the UI delegate's <code>getPreferredSize</code>
  80:    * method returns a non <code>null</code> value then return that; otherwise
  81:    * defer to the component's layout manager.
  82:    *
  83:    * @return the value of the <code>preferredSize</code> property
  84:    * @see #setPreferredSize
  85:    * @see javax.swing.plaf.ComponentUI
  86:    */
  87:   public Dimension getPreferredSize()
  88:   {
  89:     if (drawable instanceof ExtendedDrawable)
  90:     {
  91:       final ExtendedDrawable ed = (ExtendedDrawable) drawable;
  92:       return ed.getPreferredSize();
  93:     }
  94:     return super.getPreferredSize();
  95:   }
  96: 
  97:   /**
  98:    * If the minimum size has been set to a non-<code>null</code> value just
  99:    * returns it.  If the UI delegate's <code>getMinimumSize</code> method
 100:    * returns a non-<code>null</code> value then return that; otherwise defer to
 101:    * the component's layout manager.
 102:    *
 103:    * @return the value of the <code>minimumSize</code> property
 104:    * @see #setMinimumSize
 105:    * @see javax.swing.plaf.ComponentUI
 106:    */
 107:   public Dimension getMinimumSize()
 108:   {
 109:     if (drawable instanceof ExtendedDrawable)
 110:     {
 111:       final ExtendedDrawable ed = (ExtendedDrawable) drawable;
 112:       return ed.getPreferredSize();
 113:     }
 114:     return super.getMinimumSize();
 115:   }
 116: 
 117:   /**
 118:    * Returns true if this component is completely opaque.
 119:    * <p/>
 120:    * An opaque component paints every pixel within its rectangular bounds. A
 121:    * non-opaque component paints only a subset of its pixels or none at all,
 122:    * allowing the pixels underneath it to "show through".  Therefore, a
 123:    * component that does not fully paint its pixels provides a degree of
 124:    * transparency.
 125:    * <p/>
 126:    * Subclasses that guarantee to always completely paint their contents should
 127:    * override this method and return true.
 128:    *
 129:    * @return true if this component is completely opaque
 130:    * @see #setOpaque
 131:    */
 132:   public boolean isOpaque()
 133:   {
 134:     if (drawable == null)
 135:     {
 136:       return false;
 137:     }
 138:     return super.isOpaque();
 139:   }
 140: 
 141:   /**
 142:    * Calls the UI delegate's paint method, if the UI delegate is
 143:    * non-<code>null</code>.  We pass the delegate a copy of the
 144:    * <code>Graphics</code> object to protect the rest of the paint code from
 145:    * irrevocable changes (for example, <code>Graphics.translate</code>).
 146:    * <p/>
 147:    * If you override this in a subclass you should not make permanent changes to
 148:    * the passed in <code>Graphics</code>. For example, you should not alter the
 149:    * clip <code>Rectangle</code> or modify the transform. If you need to do
 150:    * these operations you may find it easier to create a new
 151:    * <code>Graphics</code> from the passed in <code>Graphics</code> and
 152:    * manipulate it. Further, if you do not invoker super's implementation you
 153:    * must honor the opaque property, that is if this component is opaque, you
 154:    * must completely fill in the background in a non-opaque color. If you do not
 155:    * honor the opaque property you will likely see visual artifacts.
 156:    * <p/>
 157:    * The passed in <code>Graphics</code> object might have a transform other
 158:    * than the identify transform installed on it.  In this case, you might get
 159:    * unexpected results if you cumulatively apply another transform.
 160:    *
 161:    * @param g the <code>Graphics</code> object to protect
 162:    * @see #paint
 163:    * @see javax.swing.plaf.ComponentUI
 164:    */
 165:   protected void paintComponent(Graphics g)
 166:   {
 167:     super.paintComponent(g);
 168:     if (drawable == null)
 169:     {
 170:       return;
 171:     }
 172: 
 173:     final Graphics2D g2 = (Graphics2D) g.create
 174:             (0, 0, getWidth(), getHeight());
 175: 
 176:     drawable.draw(g2, new Rectangle2D.Double(0, 0, getWidth(), getHeight()));
 177:     g2.dispose();
 178:   }
 179: 
 180: }