001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2014, 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 * DirectionalGradientPaintTransformer.java
029 * ----------------------------------------
030 * (C) Copyright 2013-2014 by Peter Kolb and Contributors.
031 *
032 * Original Author:  Peter Kolb;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *
035 * Changes:
036 * --------
037 * 21-Nov-2013 : Version 1, with modifications by DG (PK);
038 *
039 */
040
041package org.jfree.chart.util;
042
043import java.awt.GradientPaint;
044import java.awt.geom.Rectangle2D;
045import java.awt.Shape;
046import org.jfree.ui.GradientPaintTransformer;
047
048/**
049 * Transforms a <code>GradientPaint</code> to range over the width of a target 
050 * shape.  The orientation of the resulting <code>GradientPaint</code>
051 * depend on the coordinates of the original paint:
052 *
053 * <ul>
054 * <li> If the original paint starts at 0,0 and ends at a point 0, y != 0,
055 * the resulting paint will have a vertical orientation.
056 * <li> If the original paint starts at 0,0 and ends at a point x !=0, 0,
057 * the resulting paint will have a horizontal orientation.
058 * <li> If the original paint starts at 0,0 and ends at a point x != 0, y != 0,
059 * the resulting paint will have a diagonal orientation from the upper left to
060 * the lower right edge. Lines of equal color will have a 45 ∞ angle,
061 * pointing upwards from left to right.
062 * <li> If the original paint starts at a point x != 0, y != 0,
063 * the resulting paint will have a diagonal orientation from the lower left to
064 * the upper right edge. Lines of equal color will have a 45 ∞ angle,
065 * pointing downwards from left to right.
066 * </ul>
067 * <p>In all cases, the cyclic flag of the original paint will be taken into 
068 * account.</p>
069 *
070 * @author Peter Kolb
071 * @since 1.0.17
072 */
073public class DirectionalGradientPaintTransformer 
074        implements GradientPaintTransformer {
075    
076    /**
077     * Default constructor.
078     */
079    public DirectionalGradientPaintTransformer() {
080        super();    
081    }
082    
083    /**
084     * Transforms a <code>GradientPaint</code> instance to fit some target 
085     * shape.
086     * 
087     * @param paint  the original paint (not <code>null</code>).
088     * @param target  the reference area (not <code>null</code>).
089     * 
090     * @return A transformed paint.
091     */
092    @Override
093    public GradientPaint transform(GradientPaint paint, Shape target) {
094        //get the coordinates of the original GradientPaint
095        final double px1 = paint.getPoint1().getX();
096        final double py1 = paint.getPoint1().getY();
097        final double px2 = paint.getPoint2().getX();
098        final double py2 = paint.getPoint2().getY();
099        //get the coordinates of the shape that is to be filled
100        final Rectangle2D bounds = target.getBounds();
101        final float bx = (float)bounds.getX();
102        final float by = (float)bounds.getY();
103        final float bw = (float)bounds.getWidth();
104        final float bh = (float)bounds.getHeight();
105        //reserve variables to store the coordinates of the resulting GradientPaint
106        float rx1, ry1, rx2, ry2;
107        if (px1 == 0 && py1 == 0) {
108            //start point is upper left corner
109            rx1 = bx;
110            ry1 = by;
111            if (px2 != 0.0f && py2 != 0.0f) {
112                //end point is lower right corner --> diagonal gradient
113                float offset = (paint.isCyclic()) ? (bw + bh) / 4.0f 
114                        : (bw + bh) / 2.0f ;
115                rx2 = bx + offset;
116                ry2 = by + offset;
117            }
118            else {
119                //end point is either lower left corner --> vertical gradient
120                //or end point is upper right corner --> horizontal gradient
121                rx2 = (px2 == 0) ? rx1 : (paint.isCyclic() ? (rx1 + bw / 2.0f) 
122                        : (rx1 + bw));
123                ry2 = (py2 == 0) ? ry1 : (paint.isCyclic() ? (ry1 + bh / 2.0f) 
124                        : (ry1 + bh));
125            }
126        }
127        else {
128            //start point is lower left right corner --> diagonal gradient
129            rx1 = bx;
130            ry1 = by + bh;
131            float offset = (paint.isCyclic()) ? (bw + bh) / 4.0f 
132                    : (bw + bh) / 2.0f;
133            rx2 = bx + offset;
134            ry2 = by + bh - offset;
135        }
136        return new GradientPaint(rx1, ry1, paint.getColor1(), rx2, ry2, 
137                paint.getColor2(), paint.isCyclic());
138    }
139}