001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ---------------
028 * ColorBlock.java
029 * ---------------
030 * (C) Copyright 2004-2016, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.block;
038
039import java.awt.Graphics2D;
040import java.awt.Paint;
041import java.awt.geom.Rectangle2D;
042import java.io.IOException;
043import java.io.ObjectInputStream;
044import java.io.ObjectOutputStream;
045import org.jfree.chart.ui.Size2D;
046import org.jfree.chart.util.PaintUtils;
047import org.jfree.chart.util.Args;
048import org.jfree.chart.util.SerialUtils;
049
050/**
051 * A block that is filled with a single color.
052 */
053public class ColorBlock extends AbstractBlock implements Block {
054
055    /** For serialization. */
056    static final long serialVersionUID = 3383866145634010865L;
057
058    /** The paint. */
059    private transient Paint paint;
060
061    /**
062     * Creates a new block.
063     *
064     * @param paint  the paint ({@code null} not permitted).
065     * @param width  the width.
066     * @param height  the height.
067     */
068    public ColorBlock(Paint paint, double width, double height) {
069        Args.nullNotPermitted(paint, "paint");
070        this.paint = paint;
071        setWidth(width);
072        setHeight(height);
073    }
074
075    /**
076     * Returns the paint.
077     *
078     * @return The paint (never {@code null}).
079     *
080     * @since 1.0.5
081     */
082    public Paint getPaint() {
083        return this.paint;
084    }
085
086    /**
087     * Arranges the contents of the block, within the given constraints, and
088     * returns the block size.
089     *
090     * @param g2  the graphics device.
091     * @param constraint  the constraint ({@code null} not permitted).
092     *
093     * @return The block size (in Java2D units, never {@code null}).
094     */
095    @Override
096    public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
097        return new Size2D(calculateTotalWidth(getWidth()),
098                calculateTotalHeight(getHeight()));
099    }
100
101    /**
102     * Draws the block.
103     *
104     * @param g2  the graphics device.
105     * @param area  the area.
106     */
107    @Override
108    public void draw(Graphics2D g2, Rectangle2D area) {
109        area = trimMargin(area);
110        drawBorder(g2, area);
111        area = trimBorder(area);
112        area = trimPadding(area);
113        g2.setPaint(this.paint);
114        g2.fill(area);
115    }
116
117    /**
118     * Draws the block within the specified area.
119     *
120     * @param g2  the graphics device.
121     * @param area  the area.
122     * @param params  ignored ({@code null} permitted).
123     *
124     * @return Always {@code null}.
125     */
126    @Override
127    public Object draw(Graphics2D g2, Rectangle2D area, Object params) {
128        draw(g2, area);
129        return null;
130    }
131
132    /**
133     * Tests this block for equality with an arbitrary object.
134     *
135     * @param obj  the object ({@code null} permitted).
136     *
137     * @return A boolean.
138     */
139    @Override
140    public boolean equals(Object obj) {
141        if (obj == this) {
142            return true;
143        }
144        if (!(obj instanceof ColorBlock)) {
145            return false;
146        }
147        ColorBlock that = (ColorBlock) obj;
148        if (!PaintUtils.equal(this.paint, that.paint)) {
149            return false;
150        }
151        return super.equals(obj);
152    }
153
154    /**
155     * Provides serialization support.
156     *
157     * @param stream  the output stream.
158     *
159     * @throws IOException if there is an I/O error.
160     */
161    private void writeObject(ObjectOutputStream stream) throws IOException {
162        stream.defaultWriteObject();
163        SerialUtils.writePaint(this.paint, stream);
164    }
165
166    /**
167     * Provides serialization support.
168     *
169     * @param stream  the input stream.
170     *
171     * @throws IOException  if there is an I/O error.
172     * @throws ClassNotFoundException  if there is a classpath problem.
173     */
174    private void readObject(ObjectInputStream stream)
175        throws IOException, ClassNotFoundException {
176        stream.defaultReadObject();
177        this.paint = SerialUtils.readPaint(stream);
178    }
179
180}