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:  * Changes:
  36:  * --------
  37:  * 22-Oct-2004 : Version 1 (DG);
  38:  * 20-Apr-2005 : Added new draw() method (DG);
  39:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  40:  * 16-Mar-2007 : Implemented equals() and fixed serialization (DG);
  41:  * 
  42:  */
  43: 
  44: package org.jfree.chart.block;
  45: 
  46: import java.awt.Graphics2D;
  47: import java.awt.Paint;
  48: import java.awt.geom.Rectangle2D;
  49: import java.io.IOException;
  50: import java.io.ObjectInputStream;
  51: import java.io.ObjectOutputStream;
  52: 
  53: import org.jfree.io.SerialUtilities;
  54: import org.jfree.util.PaintUtilities;
  55: 
  56: /**
  57:  * A block that is filled with a single color.
  58:  */
  59: public class ColorBlock extends AbstractBlock implements Block {
  60: 
  61:     /** For serialization. */
  62:     static final long serialVersionUID = 3383866145634010865L;
  63: 
  64:     /** The paint. */
  65:     private transient Paint paint;
  66:     
  67:     /**
  68:      * Creates a new block.
  69:      * 
  70:      * @param paint  the paint (<code>null</code> not permitted).
  71:      * @param width  the width.
  72:      * @param height  the height.
  73:      */
  74:     public ColorBlock(Paint paint, double width, double height) {
  75:         if (paint == null) {
  76:             throw new IllegalArgumentException("Null 'paint' argument.");
  77:         }
  78:         this.paint = paint;
  79:         setWidth(width);
  80:         setHeight(height);
  81:     }
  82: 
  83:     /**
  84:      * Returns the paint.
  85:      * 
  86:      * @return The paint (never <code>null</code>).
  87:      * 
  88:      * @since 1.0.5
  89:      */
  90:     public Paint getPaint() {
  91:         return this.paint;
  92:     }
  93:     
  94:     /**
  95:      * Draws the block.
  96:      * 
  97:      * @param g2  the graphics device.
  98:      * @param area  the area.
  99:      */
 100:     public void draw(Graphics2D g2, Rectangle2D area) {
 101:         Rectangle2D bounds = getBounds();
 102:         g2.setPaint(this.paint);
 103:         g2.fill(bounds);
 104:     }
 105:     
 106:     /**
 107:      * Draws the block within the specified area.
 108:      * 
 109:      * @param g2  the graphics device.
 110:      * @param area  the area.
 111:      * @param params  ignored (<code>null</code> permitted).
 112:      * 
 113:      * @return Always <code>null</code>.
 114:      */
 115:     public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
 116:         draw(g2, area);
 117:         return null;
 118:     }
 119:     
 120:     /**
 121:      * Tests this block for equality with an arbitrary object.
 122:      * 
 123:      * @param obj  the object (<code>null</code> permitted).
 124:      * 
 125:      * @return A boolean.
 126:      */
 127:     public boolean equals(Object obj) {
 128:         if (obj == this) {
 129:             return true;
 130:         }
 131:         if (!(obj instanceof ColorBlock)) {
 132:             return false;
 133:         }
 134:         ColorBlock that = (ColorBlock) obj;
 135:         if (!PaintUtilities.equal(this.paint, that.paint)) {
 136:             return false;
 137:         }
 138:         return super.equals(obj);
 139:     }
 140:     
 141:     /**
 142:      * Provides serialization support.
 143:      *
 144:      * @param stream  the output stream.
 145:      *
 146:      * @throws IOException if there is an I/O error.
 147:      */
 148:     private void writeObject(ObjectOutputStream stream) throws IOException {
 149:         stream.defaultWriteObject();
 150:         SerialUtilities.writePaint(this.paint, stream);
 151:     }
 152: 
 153:     /**
 154:      * Provides serialization support.
 155:      *
 156:      * @param stream  the input stream.
 157:      *
 158:      * @throws IOException  if there is an I/O error.
 159:      * @throws ClassNotFoundException  if there is a classpath problem.
 160:      */
 161:     private void readObject(ObjectInputStream stream) 
 162:         throws IOException, ClassNotFoundException {
 163:         stream.defaultReadObject();
 164:         this.paint = SerialUtilities.readPaint(stream);
 165:     }
 166: 
 167: }