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