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-2008, 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-2008, 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:  * 08-Apr-2008 : Added code for margin, border and padding in draw() 
  42:  *               method (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.ui.Size2D;
  57: import org.jfree.util.PaintUtilities;
  58: 
  59: /**
  60:  * A block that is filled with a single color.
  61:  */
  62: public class ColorBlock extends AbstractBlock implements Block {
  63: 
  64:     /** For serialization. */
  65:     static final long serialVersionUID = 3383866145634010865L;
  66: 
  67:     /** The paint. */
  68:     private transient Paint paint;
  69:     
  70:     /**
  71:      * Creates a new block.
  72:      * 
  73:      * @param paint  the paint (<code>null</code> not permitted).
  74:      * @param width  the width.
  75:      * @param height  the height.
  76:      */
  77:     public ColorBlock(Paint paint, double width, double height) {
  78:         if (paint == null) {
  79:             throw new IllegalArgumentException("Null 'paint' argument.");
  80:         }
  81:         this.paint = paint;
  82:         setWidth(width);
  83:         setHeight(height);
  84:     }
  85: 
  86:     /**
  87:      * Returns the paint.
  88:      * 
  89:      * @return The paint (never <code>null</code>).
  90:      * 
  91:      * @since 1.0.5
  92:      */
  93:     public Paint getPaint() {
  94:         return this.paint;
  95:     }
  96:     
  97:     /**
  98:      * Arranges the contents of the block, within the given constraints, and 
  99:      * returns the block size.
 100:      * 
 101:      * @param g2  the graphics device.
 102:      * @param constraint  the constraint (<code>null</code> not permitted).
 103:      * 
 104:      * @return The block size (in Java2D units, never <code>null</code>).
 105:      */
 106:     public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
 107:         return new Size2D(calculateTotalWidth(getWidth()), 
 108:                 calculateTotalHeight(getHeight()));
 109:     }
 110: 
 111:     /**
 112:      * Draws the block.
 113:      * 
 114:      * @param g2  the graphics device.
 115:      * @param area  the area.
 116:      */
 117:     public void draw(Graphics2D g2, Rectangle2D area) {
 118:         area = trimMargin(area);
 119:         drawBorder(g2, area);
 120:         area = trimBorder(area);
 121:         area = trimPadding(area);
 122:         g2.setPaint(this.paint);
 123:         g2.fill(area);
 124:     }
 125:     
 126:     /**
 127:      * Draws the block within the specified area.
 128:      * 
 129:      * @param g2  the graphics device.
 130:      * @param area  the area.
 131:      * @param params  ignored (<code>null</code> permitted).
 132:      * 
 133:      * @return Always <code>null</code>.
 134:      */
 135:     public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
 136:         draw(g2, area);
 137:         return null;
 138:     }
 139:     
 140:     /**
 141:      * Tests this block for equality with an arbitrary object.
 142:      * 
 143:      * @param obj  the object (<code>null</code> permitted).
 144:      * 
 145:      * @return A boolean.
 146:      */
 147:     public boolean equals(Object obj) {
 148:         if (obj == this) {
 149:             return true;
 150:         }
 151:         if (!(obj instanceof ColorBlock)) {
 152:             return false;
 153:         }
 154:         ColorBlock that = (ColorBlock) obj;
 155:         if (!PaintUtilities.equal(this.paint, that.paint)) {
 156:             return false;
 157:         }
 158:         return super.equals(obj);
 159:     }
 160:     
 161:     /**
 162:      * Provides serialization support.
 163:      *
 164:      * @param stream  the output stream.
 165:      *
 166:      * @throws IOException if there is an I/O error.
 167:      */
 168:     private void writeObject(ObjectOutputStream stream) throws IOException {
 169:         stream.defaultWriteObject();
 170:         SerialUtilities.writePaint(this.paint, stream);
 171:     }
 172: 
 173:     /**
 174:      * Provides serialization support.
 175:      *
 176:      * @param stream  the input stream.
 177:      *
 178:      * @throws IOException  if there is an I/O error.
 179:      * @throws ClassNotFoundException  if there is a classpath problem.
 180:      */
 181:     private void readObject(ObjectInputStream stream) 
 182:         throws IOException, ClassNotFoundException {
 183:         stream.defaultReadObject();
 184:         this.paint = SerialUtilities.readPaint(stream);
 185:     }
 186: 
 187: }