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