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 * DirectionalGradientPaintTransformer.java 029 * ---------------------------------------- 030 * (C) Copyright 2013 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 * <p> 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> 068 * In all cases, the cyclic flag of the original paint will be taken into 069 * account. 070 * 071 * @author Peter Kolb 072 * @since 1.0.17 073 */ 074public class DirectionalGradientPaintTransformer 075 implements GradientPaintTransformer { 076 077 /** 078 * Default constructor. 079 */ 080 public DirectionalGradientPaintTransformer() { 081 super(); 082 } 083 084 /** 085 * Transforms a <code>GradientPaint</code> instance to fit some target 086 * shape. 087 * 088 * @param paint the original paint (not <code>null</code>). 089 * @param target the reference area (not <code>null</code>). 090 * 091 * @return A transformed paint. 092 */ 093 @Override 094 public GradientPaint transform(GradientPaint paint, Shape target) { 095 //get the coordinates of the original GradientPaint 096 final double px1 = paint.getPoint1().getX(); 097 final double py1 = paint.getPoint1().getY(); 098 final double px2 = paint.getPoint2().getX(); 099 final double py2 = paint.getPoint2().getY(); 100 //get the coordinates of the shape that is to be filled 101 final Rectangle2D bounds = target.getBounds(); 102 final float bx = (float)bounds.getX(); 103 final float by = (float)bounds.getY(); 104 final float bw = (float)bounds.getWidth(); 105 final float bh = (float)bounds.getHeight(); 106 //reserve variables to store the coordinates of the resulting GradientPaint 107 float rx1, ry1, rx2, ry2; 108 if (px1 == 0 && py1 == 0) { 109 //start point is upper left corner 110 rx1 = bx; 111 ry1 = by; 112 if (px2 != 0.0f && py2 != 0.0f) { 113 //end point is lower right corner --> diagonal gradient 114 float offset = (paint.isCyclic()) ? (bw + bh) / 4.0f 115 : (bw + bh) / 2.0f ; 116 rx2 = bx + offset; 117 ry2 = by + offset; 118 } 119 else { 120 //end point is either lower left corner --> vertical gradient 121 //or end point is upper right corner --> horizontal gradient 122 rx2 = (px2 == 0) ? rx1 : (paint.isCyclic() ? (rx1 + bw / 2.0f) 123 : (rx1 + bw)); 124 ry2 = (py2 == 0) ? ry1 : (paint.isCyclic() ? (ry1 + bh / 2.0f) 125 : (ry1 + bh)); 126 } 127 } 128 else { 129 //start point is lower left right corner --> diagonal gradient 130 rx1 = bx; 131 ry1 = by + bh; 132 float offset = (paint.isCyclic()) ? (bw + bh) / 4.0f 133 : (bw + bh) / 2.0f; 134 rx2 = bx + offset; 135 ry2 = by + bh - offset; 136 } 137 return new GradientPaint(rx1, ry1, paint.getColor1(), rx2, ry2, 138 paint.getColor2(), paint.isCyclic()); 139 } 140}