Source for org.jfree.chart.block.ColorBlock

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ---------------
  28:  * ColorBlock.java
  29:  * ---------------
  30:  * (C) Copyright 2004, 2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: ColorBlock.java,v 1.4.2.2 2007/03/16 15:26:15 mungady Exp $
  36:  *
  37:  * Changes:
  38:  * --------
  39:  * 22-Oct-2004 : Version 1 (DG);
  40:  * 20-Apr-2005 : Added new draw() method (DG);
  41:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  42:  * 16-Mar-2007 : Implemented equals() and fixed serialization (DG);
  43:  * 
  44:  */
  45: 
  46: package org.jfree.chart.block;
  47: 
  48: import java.awt.Graphics2D;
  49: import java.awt.Paint;
  50: import java.awt.geom.Rectangle2D;
  51: import java.io.IOException;
  52: import java.io.ObjectInputStream;
  53: import java.io.ObjectOutputStream;
  54: 
  55: import org.jfree.io.SerialUtilities;
  56: import org.jfree.util.PaintUtilities;
  57: 
  58: /**
  59:  * A block that is filled with a single color.
  60:  */
  61: public class ColorBlock extends AbstractBlock implements Block {
  62: 
  63:     /** The paint. */
  64:     private transient Paint paint;
  65:     
  66:     /**
  67:      * Creates a new block.
  68:      * 
  69:      * @param paint  the paint (<code>null</code> not permitted).
  70:      * @param width  the width.
  71:      * @param height  the height.
  72:      */
  73:     public ColorBlock(Paint paint, double width, double height) {
  74:         if (paint == null) {
  75:             throw new IllegalArgumentException("Null 'paint' argument.");
  76:         }
  77:         this.paint = paint;
  78:         setWidth(width);
  79:         setHeight(height);
  80:     }
  81: 
  82:     /**
  83:      * Returns the paint.
  84:      * 
  85:      * @return The paint (never <code>null</code>).
  86:      * 
  87:      * @since 1.0.5
  88:      */
  89:     public Paint getPaint() {
  90:         return this.paint;
  91:     }
  92:     
  93:     /**
  94:      * Draws the block.
  95:      * 
  96:      * @param g2  the graphics device.
  97:      * @param area  the area.
  98:      */
  99:     public void draw(Graphics2D g2, Rectangle2D area) {
 100:         Rectangle2D bounds = getBounds();
 101:         g2.setPaint(this.paint);
 102:         g2.fill(bounds);
 103:     }
 104:     
 105:     /**
 106:      * Draws the block within the specified area.
 107:      * 
 108:      * @param g2  the graphics device.
 109:      * @param area  the area.
 110:      * @param params  ignored (<code>null</code> permitted).
 111:      * 
 112:      * @return Always <code>null</code>.
 113:      */
 114:     public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
 115:         draw(g2, area);
 116:         return null;
 117:     }
 118:     
 119:     /**
 120:      * Tests this block for equality with an arbitrary object.
 121:      * 
 122:      * @param obj  the object (<code>null</code> permitted).
 123:      * 
 124:      * @return A boolean.
 125:      */
 126:     public boolean equals(Object obj) {
 127:         if (obj == this) {
 128:             return true;
 129:         }
 130:         if (!(obj instanceof ColorBlock)) {
 131:             return false;
 132:         }
 133:         ColorBlock that = (ColorBlock) obj;
 134:         if (!PaintUtilities.equal(this.paint, that.paint)) {
 135:             return false;
 136:         }
 137:         return super.equals(obj);
 138:     }
 139:     
 140:     /**
 141:      * Provides serialization support.
 142:      *
 143:      * @param stream  the output stream.
 144:      *
 145:      * @throws IOException if there is an I/O error.
 146:      */
 147:     private void writeObject(ObjectOutputStream stream) throws IOException {
 148:         stream.defaultWriteObject();
 149:         SerialUtilities.writePaint(this.paint, stream);
 150:     }
 151: 
 152:     /**
 153:      * Provides serialization support.
 154:      *
 155:      * @param stream  the input stream.
 156:      *
 157:      * @throws IOException  if there is an I/O error.
 158:      * @throws ClassNotFoundException  if there is a classpath problem.
 159:      */
 160:     private void readObject(ObjectInputStream stream) 
 161:         throws IOException, ClassNotFoundException {
 162:         stream.defaultReadObject();
 163:         this.paint = SerialUtilities.readPaint(stream);
 164:     }
 165: 
 166: }