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