    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jfreechart/index.html
    008     *
    009     * This library is free software; you can redistribute it and/or modify it
    010     * under the terms of the GNU Lesser General Public License as published by
    011     * the Free Software Foundation; either version 2.1 of the License, or
    012     * (at your option) any later version.
    013     *
    014     * This library is distributed in the hope that it will be useful, but
    015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017     * License for more details.
    018     *
    019     * You should have received a copy of the GNU Lesser General Public
    020     * License along with this library; if not, write to the Free Software
    021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022     * USA.
    023     *
    024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * ---------------
    028     * ColorBlock.java
    029     * ---------------
    030     * (C) Copyright 2004, 2007, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: ColorBlock.java,v 1.4.2.2 2007/03/16 15:26:15 mungady Exp $
    036     *
    037     * Changes:
    038     * --------
    039     * 22-Oct-2004 : Version 1 (DG);
    040     * 20-Apr-2005 : Added new draw() method (DG);
    041     * ------------- JFREECHART 1.0.x ---------------------------------------------
    042     * 16-Mar-2007 : Implemented equals() and fixed serialization (DG);
    043     *
    044     */
    045
    046    package org.jfree.chart.block;
    047
    048    import java.awt.Graphics2D;
    049    import java.awt.Paint;
    050    import java.awt.geom.Rectangle2D;
    051    import java.io.IOException;
    052    import java.io.ObjectInputStream;
    053    import java.io.ObjectOutputStream;
    054
    055    import org.jfree.io.SerialUtilities;
    056    import org.jfree.util.PaintUtilities;
    057
    058    /**
    059     * A block that is filled with a single color.
    060     */
    061    public class ColorBlock extends AbstractBlock implements Block {
    062
    063        /** The paint. */
    064        private transient Paint paint;
    065
    066        /**
    067         * Creates a new block.
    068         *
    069         * @param paint  the paint (<code>null</code> not permitted).
    070         * @param width  the width.
    071         * @param height  the height.
    072         */
    073        public ColorBlock(Paint paint, double width, double height) {
    074            if (paint == null) {
    075                throw new IllegalArgumentException("Null 'paint' argument.");
    076            }
    077            this.paint = paint;
    078            setWidth(width);
    079            setHeight(height);
    080        }
    081
    082        /**
    083         * Returns the paint.
    084         *
    085         * @return The paint (never <code>null</code>).
    086         *
    087         * @since 1.0.5
    088         */
    089        public Paint getPaint() {
    090            return this.paint;
    091        }
    092
    093        /**
    094         * Draws the block.
    095         *
    096         * @param g2  the graphics device.
    097         * @param area  the area.
    098         */
    099        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    }