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 * GradientXYBarPainter.java 029 * ------------------------- 030 * (C) Copyright 2008-2022, by David Gilbert. 031 * 032 * Original Author: David Gilbert; 033 * Contributor(s): -; 034 * 035 */ 036 037package org.jfree.chart.renderer.xy; 038 039import java.awt.Color; 040import java.awt.GradientPaint; 041import java.awt.Graphics2D; 042import java.awt.Paint; 043import java.awt.Stroke; 044import java.awt.geom.Rectangle2D; 045import java.awt.geom.RectangularShape; 046import java.io.Serializable; 047 048import org.jfree.chart.internal.HashUtils; 049import org.jfree.chart.api.RectangleEdge; 050 051/** 052 * An implementation of the {@link XYBarPainter} interface that uses several 053 * gradient fills to enrich the appearance of the bars. 054 */ 055public class GradientXYBarPainter implements XYBarPainter, Serializable { 056 057 /** The division point between the first and second gradient regions. */ 058 private double g1; 059 060 /** The division point between the second and third gradient regions. */ 061 private double g2; 062 063 /** The division point between the third and fourth gradient regions. */ 064 private double g3; 065 066 /** 067 * Creates a new instance. 068 */ 069 public GradientXYBarPainter() { 070 this(0.10, 0.20, 0.80); 071 } 072 073 /** 074 * Creates a new instance. 075 * 076 * @param g1 the division between regions 1 and 2. 077 * @param g2 the division between regions 2 and 3. 078 * @param g3 the division between regions 3 and 4. 079 */ 080 public GradientXYBarPainter(double g1, double g2, double g3) { 081 this.g1 = g1; 082 this.g2 = g2; 083 this.g3 = g3; 084 } 085 086 /** 087 * Paints a single bar instance. 088 * 089 * @param g2 the graphics target. 090 * @param renderer the renderer. 091 * @param row the row index. 092 * @param column the column index. 093 * @param bar the bar 094 * @param base indicates which side of the rectangle is the base of the 095 * bar. 096 */ 097 @Override 098 public void paintBar(Graphics2D g2, XYBarRenderer renderer, int row, 099 int column, RectangularShape bar, RectangleEdge base) { 100 101 Paint itemPaint = renderer.getItemPaint(row, column); 102 103 Color c0, c1; 104 if (itemPaint instanceof Color) { 105 c0 = (Color) itemPaint; 106 c1 = c0.brighter(); 107 } 108 else if (itemPaint instanceof GradientPaint) { 109 GradientPaint gp = (GradientPaint) itemPaint; 110 c0 = gp.getColor1(); 111 c1 = gp.getColor2(); 112 } 113 else { 114 c0 = Color.BLUE; 115 c1 = Color.BLUE.brighter(); 116 } 117 118 // as a special case, if the bar colour has alpha == 0, we draw 119 // nothing. 120 if (c0.getAlpha() == 0) { 121 return; 122 } 123 124 if (base == RectangleEdge.TOP || base == RectangleEdge.BOTTOM) { 125 Rectangle2D[] regions = splitVerticalBar(bar, this.g1, this.g2, 126 this.g3); 127 GradientPaint gp = new GradientPaint((float) regions[0].getMinX(), 128 0.0f, c0, (float) regions[0].getMaxX(), 0.0f, Color.WHITE); 129 g2.setPaint(gp); 130 g2.fill(regions[0]); 131 132 gp = new GradientPaint((float) regions[1].getMinX(), 0.0f, 133 Color.WHITE, (float) regions[1].getMaxX(), 0.0f, c0); 134 g2.setPaint(gp); 135 g2.fill(regions[1]); 136 137 gp = new GradientPaint((float) regions[2].getMinX(), 0.0f, c0, 138 (float) regions[2].getMaxX(), 0.0f, c1); 139 g2.setPaint(gp); 140 g2.fill(regions[2]); 141 142 gp = new GradientPaint((float) regions[3].getMinX(), 0.0f, c1, 143 (float) regions[3].getMaxX(), 0.0f, c0); 144 g2.setPaint(gp); 145 g2.fill(regions[3]); 146 } 147 else if (base == RectangleEdge.LEFT || base == RectangleEdge.RIGHT) { 148 Rectangle2D[] regions = splitHorizontalBar(bar, this.g1, this.g2, 149 this.g3); 150 GradientPaint gp = new GradientPaint(0.0f, 151 (float) regions[0].getMinY(), c0, 0.0f, 152 (float) regions[0].getMaxX(), Color.WHITE); 153 g2.setPaint(gp); 154 g2.fill(regions[0]); 155 156 gp = new GradientPaint(0.0f, (float) regions[1].getMinY(), 157 Color.WHITE, 0.0f, (float) regions[1].getMaxY(), c0); 158 g2.setPaint(gp); 159 g2.fill(regions[1]); 160 161 gp = new GradientPaint(0.0f, (float) regions[2].getMinY(), c0, 162 0.0f, (float) regions[2].getMaxY(), c1); 163 g2.setPaint(gp); 164 g2.fill(regions[2]); 165 166 gp = new GradientPaint(0.0f, (float) regions[3].getMinY(), c1, 167 0.0f, (float) regions[3].getMaxY(), c0); 168 g2.setPaint(gp); 169 g2.fill(regions[3]); 170 171 } 172 173 // draw the outline... 174 if (renderer.isDrawBarOutline()) { 175 Stroke stroke = renderer.getItemOutlineStroke(row, column); 176 Paint paint = renderer.getItemOutlinePaint(row, column); 177 if (stroke != null && paint != null) { 178 g2.setStroke(stroke); 179 g2.setPaint(paint); 180 g2.draw(bar); 181 } 182 } 183 184 } 185 186 /** 187 * Paints a single bar instance. 188 * 189 * @param g2 the graphics target. 190 * @param renderer the renderer. 191 * @param row the row index. 192 * @param column the column index. 193 * @param bar the bar 194 * @param base indicates which side of the rectangle is the base of the 195 * bar. 196 * @param pegShadow peg the shadow to the base of the bar? 197 */ 198 @Override 199 public void paintBarShadow(Graphics2D g2, XYBarRenderer renderer, int row, 200 int column, RectangularShape bar, RectangleEdge base, 201 boolean pegShadow) { 202 203 // handle a special case - if the bar colour has alpha == 0, it is 204 // invisible so we shouldn't draw any shadow 205 Paint itemPaint = renderer.getItemPaint(row, column); 206 if (itemPaint instanceof Color) { 207 Color c = (Color) itemPaint; 208 if (c.getAlpha() == 0) { 209 return; 210 } 211 } 212 213 RectangularShape shadow = createShadow(bar, renderer.getShadowXOffset(), 214 renderer.getShadowYOffset(), base, pegShadow); 215 g2.setPaint(Color.GRAY); 216 g2.fill(shadow); 217 218 } 219 220 /** 221 * Creates a shadow for the bar. 222 * 223 * @param bar the bar shape. 224 * @param xOffset the x-offset for the shadow. 225 * @param yOffset the y-offset for the shadow. 226 * @param base the edge that is the base of the bar. 227 * @param pegShadow peg the shadow to the base? 228 * 229 * @return A rectangle for the shadow. 230 */ 231 private Rectangle2D createShadow(RectangularShape bar, double xOffset, 232 double yOffset, RectangleEdge base, boolean pegShadow) { 233 double x0 = bar.getMinX(); 234 double x1 = bar.getMaxX(); 235 double y0 = bar.getMinY(); 236 double y1 = bar.getMaxY(); 237 if (base == RectangleEdge.TOP) { 238 x0 += xOffset; 239 x1 += xOffset; 240 if (!pegShadow) { 241 y0 += yOffset; 242 } 243 y1 += yOffset; 244 } 245 else if (base == RectangleEdge.BOTTOM) { 246 x0 += xOffset; 247 x1 += xOffset; 248 y0 += yOffset; 249 if (!pegShadow) { 250 y1 += yOffset; 251 } 252 } 253 else if (base == RectangleEdge.LEFT) { 254 if (!pegShadow) { 255 x0 += xOffset; 256 } 257 x1 += xOffset; 258 y0 += yOffset; 259 y1 += yOffset; 260 } 261 else if (base == RectangleEdge.RIGHT) { 262 x0 += xOffset; 263 if (!pegShadow) { 264 x1 += xOffset; 265 } 266 y0 += yOffset; 267 y1 += yOffset; 268 } 269 return new Rectangle2D.Double(x0, y0, (x1 - x0), (y1 - y0)); 270 } 271 272 /** 273 * Splits a bar into subregions (elsewhere, these subregions will have 274 * different gradients applied to them). 275 * 276 * @param bar the bar shape. 277 * @param a the first division. 278 * @param b the second division. 279 * @param c the third division. 280 * 281 * @return An array containing four subregions. 282 */ 283 private Rectangle2D[] splitVerticalBar(RectangularShape bar, double a, 284 double b, double c) { 285 Rectangle2D[] result = new Rectangle2D[4]; 286 double x0 = bar.getMinX(); 287 double x1 = Math.rint(x0 + (bar.getWidth() * a)); 288 double x2 = Math.rint(x0 + (bar.getWidth() * b)); 289 double x3 = Math.rint(x0 + (bar.getWidth() * c)); 290 result[0] = new Rectangle2D.Double(bar.getMinX(), bar.getMinY(), 291 x1 - x0, bar.getHeight()); 292 result[1] = new Rectangle2D.Double(x1, bar.getMinY(), x2 - x1, 293 bar.getHeight()); 294 result[2] = new Rectangle2D.Double(x2, bar.getMinY(), x3 - x2, 295 bar.getHeight()); 296 result[3] = new Rectangle2D.Double(x3, bar.getMinY(), 297 bar.getMaxX() - x3, bar.getHeight()); 298 return result; 299 } 300 301 /** 302 * Splits a bar into subregions (elsewhere, these subregions will have 303 * different gradients applied to them). 304 * 305 * @param bar the bar shape. 306 * @param a the first division. 307 * @param b the second division. 308 * @param c the third division. 309 * 310 * @return An array containing four subregions. 311 */ 312 private Rectangle2D[] splitHorizontalBar(RectangularShape bar, double a, 313 double b, double c) { 314 Rectangle2D[] result = new Rectangle2D[4]; 315 double y0 = bar.getMinY(); 316 double y1 = Math.rint(y0 + (bar.getHeight() * a)); 317 double y2 = Math.rint(y0 + (bar.getHeight() * b)); 318 double y3 = Math.rint(y0 + (bar.getHeight() * c)); 319 result[0] = new Rectangle2D.Double(bar.getMinX(), bar.getMinY(), 320 bar.getWidth(), y1 - y0); 321 result[1] = new Rectangle2D.Double(bar.getMinX(), y1, bar.getWidth(), 322 y2 - y1); 323 result[2] = new Rectangle2D.Double(bar.getMinX(), y2, bar.getWidth(), 324 y3 - y2); 325 result[3] = new Rectangle2D.Double(bar.getMinX(), y3, bar.getWidth(), 326 bar.getMaxY() - y3); 327 return result; 328 } 329 330 /** 331 * Tests this instance for equality with an arbitrary object. 332 * 333 * @param obj the obj ({@code null} permitted). 334 * 335 * @return A boolean. 336 */ 337 @Override 338 public boolean equals(Object obj) { 339 if (obj == this) { 340 return true; 341 } 342 if (!(obj instanceof GradientXYBarPainter)) { 343 return false; 344 } 345 GradientXYBarPainter that = (GradientXYBarPainter) obj; 346 if (this.g1 != that.g1) { 347 return false; 348 } 349 if (this.g2 != that.g2) { 350 return false; 351 } 352 if (this.g3 != that.g3) { 353 return false; 354 } 355 return true; 356 } 357 358 /** 359 * Returns a hash code for this instance. 360 * 361 * @return A hash code. 362 */ 363 @Override 364 public int hashCode() { 365 int hash = 37; 366 hash = HashUtils.hashCode(hash, this.g1); 367 hash = HashUtils.hashCode(hash, this.g2); 368 hash = HashUtils.hashCode(hash, this.g3); 369 return hash; 370 } 371 372}