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 * StandardXYBarPainter.java 029 * ------------------------- 030 * (C) Copyright 2008, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): -; 034 * 035 * Changes: 036 * -------- 037 * 19-Jun-2008 : Version 1 (DG); 038 * 039 */ 040 041package org.jfree.chart.renderer.xy; 042 043import java.awt.Color; 044import java.awt.GradientPaint; 045import java.awt.Graphics2D; 046import java.awt.Paint; 047import java.awt.Stroke; 048import java.awt.geom.Rectangle2D; 049import java.awt.geom.RectangularShape; 050import java.io.Serializable; 051import org.jfree.chart.ui.GradientPaintTransformer; 052import org.jfree.chart.ui.RectangleEdge; 053 054/** 055 * An implementation of the {@link XYBarPainter} interface that preserves the 056 * behaviour of bar painting that existed prior to the introduction of the 057 * {@link XYBarPainter} interface. 058 * 059 * @see GradientXYBarPainter 060 * 061 * @since 1.0.11 062 */ 063public class StandardXYBarPainter implements XYBarPainter, Serializable { 064 065 /** 066 * Creates a new instance. 067 */ 068 public StandardXYBarPainter() { 069 } 070 071 /** 072 * Paints a single bar instance. 073 * 074 * @param g2 the graphics target. 075 * @param renderer the renderer. 076 * @param row the row index. 077 * @param column the column index. 078 * @param bar the bar 079 * @param base indicates which side of the rectangle is the base of the 080 * bar. 081 */ 082 @Override 083 public void paintBar(Graphics2D g2, XYBarRenderer renderer, int row, 084 int column, RectangularShape bar, RectangleEdge base) { 085 086 Paint itemPaint = renderer.getItemPaint(row, column); 087 GradientPaintTransformer t = renderer.getGradientPaintTransformer(); 088 if (t != null && itemPaint instanceof GradientPaint) { 089 itemPaint = t.transform((GradientPaint) itemPaint, bar); 090 } 091 g2.setPaint(itemPaint); 092 g2.fill(bar); 093 094 // draw the outline... 095 if (renderer.isDrawBarOutline()) { 096 // && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) { 097 Stroke stroke = renderer.getItemOutlineStroke(row, column); 098 Paint paint = renderer.getItemOutlinePaint(row, column); 099 if (stroke != null && paint != null) { 100 g2.setStroke(stroke); 101 g2.setPaint(paint); 102 g2.draw(bar); 103 } 104 } 105 106 } 107 108 /** 109 * Paints a single bar instance. 110 * 111 * @param g2 the graphics target. 112 * @param renderer the renderer. 113 * @param row the row index. 114 * @param column the column index. 115 * @param bar the bar 116 * @param base indicates which side of the rectangle is the base of the 117 * bar. 118 * @param pegShadow peg the shadow to the base of the bar? 119 */ 120 @Override 121 public void paintBarShadow(Graphics2D g2, XYBarRenderer renderer, int row, 122 int column, RectangularShape bar, RectangleEdge base, 123 boolean pegShadow) { 124 125 // handle a special case - if the bar colour has alpha == 0, it is 126 // invisible so we shouldn't draw any shadow 127 Paint itemPaint = renderer.getItemPaint(row, column); 128 if (itemPaint instanceof Color) { 129 Color c = (Color) itemPaint; 130 if (c.getAlpha() == 0) { 131 return; 132 } 133 } 134 135 RectangularShape shadow = createShadow(bar, renderer.getShadowXOffset(), 136 renderer.getShadowYOffset(), base, pegShadow); 137 g2.setPaint(Color.gray); 138 g2.fill(shadow); 139 140 } 141 142 /** 143 * Creates a shadow for the bar. 144 * 145 * @param bar the bar shape. 146 * @param xOffset the x-offset for the shadow. 147 * @param yOffset the y-offset for the shadow. 148 * @param base the edge that is the base of the bar. 149 * @param pegShadow peg the shadow to the base? 150 * 151 * @return A rectangle for the shadow. 152 */ 153 private Rectangle2D createShadow(RectangularShape bar, double xOffset, 154 double yOffset, RectangleEdge base, boolean pegShadow) { 155 double x0 = bar.getMinX(); 156 double x1 = bar.getMaxX(); 157 double y0 = bar.getMinY(); 158 double y1 = bar.getMaxY(); 159 if (base == RectangleEdge.TOP) { 160 x0 += xOffset; 161 x1 += xOffset; 162 if (!pegShadow) { 163 y0 += yOffset; 164 } 165 y1 += yOffset; 166 } 167 else if (base == RectangleEdge.BOTTOM) { 168 x0 += xOffset; 169 x1 += xOffset; 170 y0 += yOffset; 171 if (!pegShadow) { 172 y1 += yOffset; 173 } 174 } 175 else if (base == RectangleEdge.LEFT) { 176 if (!pegShadow) { 177 x0 += xOffset; 178 } 179 x1 += xOffset; 180 y0 += yOffset; 181 y1 += yOffset; 182 } 183 else if (base == RectangleEdge.RIGHT) { 184 x0 += xOffset; 185 if (!pegShadow) { 186 x1 += xOffset; 187 } 188 y0 += yOffset; 189 y1 += yOffset; 190 } 191 return new Rectangle2D.Double(x0, y0, (x1 - x0), (y1 - y0)); 192 } 193 194 /** 195 * Tests this instance for equality with an arbitrary object. 196 * 197 * @param obj the obj ({@code null} permitted). 198 * 199 * @return A boolean. 200 */ 201 @Override 202 public boolean equals(Object obj) { 203 if (obj == this) { 204 return true; 205 } 206 if (!(obj instanceof StandardXYBarPainter)) { 207 return false; 208 } 209 return true; 210 } 211 212 /** 213 * Returns a hash code for this instance. 214 * 215 * @return A hash code. 216 */ 217 @Override 218 public int hashCode() { 219 int hash = 37; 220 // no fields to compute... 221 return hash; 222 } 223 224}