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 * StandardBarPainter.java
029 * -----------------------
030 * (C) Copyright 2008, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 */
035
036package org.jfree.chart.renderer.category;
037
038import java.awt.Color;
039import java.awt.GradientPaint;
040import java.awt.Graphics2D;
041import java.awt.Paint;
042import java.awt.Stroke;
043import java.awt.geom.Rectangle2D;
044import java.awt.geom.RectangularShape;
045import java.io.Serializable;
046import org.jfree.chart.ui.GradientPaintTransformer;
047import org.jfree.chart.ui.RectangleEdge;
048
049/**
050 * An implementation of the {@link BarPainter} interface that preserves the
051 * behaviour of bar painting that existed prior to the introduction of the
052 * {@link BarPainter} interface.
053 *
054 * @see GradientBarPainter
055 *
056 * @since 1.0.11
057 */
058public class StandardBarPainter implements BarPainter, Serializable {
059
060    /**
061     * Creates a new instance.
062     */
063    public StandardBarPainter() {
064    }
065
066    /**
067     * Paints a single bar instance.
068     *
069     * @param g2  the graphics target.
070     * @param renderer  the renderer.
071     * @param row  the row index.
072     * @param column  the column index.
073     * @param bar  the bar
074     * @param base  indicates which side of the rectangle is the base of the
075     *              bar.
076     */
077    @Override
078    public void paintBar(Graphics2D g2, BarRenderer renderer, int row,
079            int column, RectangularShape bar, RectangleEdge base) {
080
081        Paint itemPaint = renderer.getItemPaint(row, column);
082        GradientPaintTransformer t = renderer.getGradientPaintTransformer();
083        if (t != null && itemPaint instanceof GradientPaint) {
084            itemPaint = t.transform((GradientPaint) itemPaint, bar);
085        }
086        g2.setPaint(itemPaint);
087        g2.fill(bar);
088
089        // draw the outline...
090        if (renderer.isDrawBarOutline()) {
091               // && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
092            Stroke stroke = renderer.getItemOutlineStroke(row, column);
093            Paint paint = renderer.getItemOutlinePaint(row, column);
094            if (stroke != null && paint != null) {
095                g2.setStroke(stroke);
096                g2.setPaint(paint);
097                g2.draw(bar);
098            }
099        }
100
101    }
102
103    /**
104     * Paints a single bar instance.
105     *
106     * @param g2  the graphics target.
107     * @param renderer  the renderer.
108     * @param row  the row index.
109     * @param column  the column index.
110     * @param bar  the bar
111     * @param base  indicates which side of the rectangle is the base of the
112     *              bar.
113     * @param pegShadow  peg the shadow to the base of the bar?
114     */
115    @Override
116    public void paintBarShadow(Graphics2D g2, BarRenderer renderer, int row,
117            int column, RectangularShape bar, RectangleEdge base,
118            boolean pegShadow) {
119
120        // handle a special case - if the bar colour has alpha == 0, it is
121        // invisible so we shouldn't draw any shadow
122        Paint itemPaint = renderer.getItemPaint(row, column);
123        if (itemPaint instanceof Color) {
124            Color c = (Color) itemPaint;
125            if (c.getAlpha() == 0) {
126                return;
127            }
128        }
129
130        RectangularShape shadow = createShadow(bar, renderer.getShadowXOffset(),
131                renderer.getShadowYOffset(), base, pegShadow);
132        g2.setPaint(renderer.getShadowPaint());
133        g2.fill(shadow);
134
135    }
136
137    /**
138     * Creates a shadow for the bar.
139     *
140     * @param bar  the bar shape.
141     * @param xOffset  the x-offset for the shadow.
142     * @param yOffset  the y-offset for the shadow.
143     * @param base  the edge that is the base of the bar.
144     * @param pegShadow  peg the shadow to the base?
145     *
146     * @return A rectangle for the shadow.
147     */
148    private Rectangle2D createShadow(RectangularShape bar, double xOffset,
149            double yOffset, RectangleEdge base, boolean pegShadow) {
150        double x0 = bar.getMinX();
151        double x1 = bar.getMaxX();
152        double y0 = bar.getMinY();
153        double y1 = bar.getMaxY();
154        if (base == RectangleEdge.TOP) {
155            x0 += xOffset;
156            x1 += xOffset;
157            if (!pegShadow) {
158                y0 += yOffset;
159            }
160            y1 += yOffset;
161        }
162        else if (base == RectangleEdge.BOTTOM) {
163            x0 += xOffset;
164            x1 += xOffset;
165            y0 += yOffset;
166            if (!pegShadow) {
167                y1 += yOffset;
168            }
169        }
170        else if (base == RectangleEdge.LEFT) {
171            if (!pegShadow) {
172                x0 += xOffset;
173            }
174            x1 += xOffset;
175            y0 += yOffset;
176            y1 += yOffset;
177        }
178        else if (base == RectangleEdge.RIGHT) {
179            x0 += xOffset;
180            if (!pegShadow) {
181                x1 += xOffset;
182            }
183            y0 += yOffset;
184            y1 += yOffset;
185        }
186        return new Rectangle2D.Double(x0, y0, (x1 - x0), (y1 - y0));
187    }
188
189    /**
190     * Tests this instance for equality with an arbitrary object.
191     *
192     * @param obj  the obj ({@code null} permitted).
193     *
194     * @return A boolean.
195     */
196    @Override
197    public boolean equals(Object obj) {
198        if (obj == this) {
199            return true;
200        }
201        if (!(obj instanceof StandardBarPainter)) {
202            return false;
203        }
204        return true;
205    }
206
207    /**
208     * Returns a hash code for this instance.
209     *
210     * @return A hash code.
211     */
212    @Override
213    public int hashCode() {
214        int hash = 37;
215        // no fields to compute...
216        return hash;
217    }
218
219}