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     * Changes:
036     * --------
037     * 22-Oct-2004 : Version 1 (DG);
038     * 20-Apr-2005 : Added new draw() method (DG);
039     * ------------- JFREECHART 1.0.x ---------------------------------------------
040     * 16-Mar-2007 : Implemented equals() and fixed serialization (DG);
041     * 
042     */
043    
044    package org.jfree.chart.block;
045    
046    import java.awt.Graphics2D;
047    import java.awt.Paint;
048    import java.awt.geom.Rectangle2D;
049    import java.io.IOException;
050    import java.io.ObjectInputStream;
051    import java.io.ObjectOutputStream;
052    
053    import org.jfree.io.SerialUtilities;
054    import org.jfree.util.PaintUtilities;
055    
056    /**
057     * A block that is filled with a single color.
058     */
059    public class ColorBlock extends AbstractBlock implements Block {
060    
061        /** For serialization. */
062        static final long serialVersionUID = 3383866145634010865L;
063    
064        /** The paint. */
065        private transient Paint paint;
066        
067        /**
068         * Creates a new block.
069         * 
070         * @param paint  the paint (<code>null</code> not permitted).
071         * @param width  the width.
072         * @param height  the height.
073         */
074        public ColorBlock(Paint paint, double width, double height) {
075            if (paint == null) {
076                throw new IllegalArgumentException("Null 'paint' argument.");
077            }
078            this.paint = paint;
079            setWidth(width);
080            setHeight(height);
081        }
082    
083        /**
084         * Returns the paint.
085         * 
086         * @return The paint (never <code>null</code>).
087         * 
088         * @since 1.0.5
089         */
090        public Paint getPaint() {
091            return this.paint;
092        }
093        
094        /**
095         * Draws the block.
096         * 
097         * @param g2  the graphics device.
098         * @param area  the area.
099         */
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    }