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