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