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