1 48 49 package org.jfree.chart.axis; 50 51 import java.awt.AlphaComposite ; 52 import java.awt.Color ; 53 import java.awt.Composite ; 54 import java.awt.Font ; 55 import java.awt.FontMetrics ; 56 import java.awt.Graphics2D ; 57 import java.awt.font.LineMetrics ; 58 import java.awt.geom.Rectangle2D ; 59 import java.io.Serializable ; 60 import java.util.Iterator ; 61 import java.util.List ; 62 63 import org.jfree.chart.plot.IntervalMarker; 64 import org.jfree.text.TextUtilities; 65 import org.jfree.ui.RectangleEdge; 66 import org.jfree.util.ObjectUtilities; 67 68 71 public class MarkerAxisBand implements Serializable { 72 73 74 private static final long serialVersionUID = -1729482413886398919L; 75 76 77 private NumberAxis axis; 78 79 80 private double topOuterGap; 81 82 83 private double topInnerGap; 84 85 86 private double bottomOuterGap; 87 88 89 private double bottomInnerGap; 90 91 92 private Font font; 93 94 95 private List markers; 96 97 107 public MarkerAxisBand(NumberAxis axis, 108 double topOuterGap, double topInnerGap, 109 double bottomOuterGap, double bottomInnerGap, 110 Font font) { 111 this.axis = axis; 112 this.topOuterGap = topOuterGap; 113 this.topInnerGap = topInnerGap; 114 this.bottomOuterGap = bottomOuterGap; 115 this.bottomInnerGap = bottomInnerGap; 116 this.font = font; 117 this.markers = new java.util.ArrayList (); 118 } 119 120 125 public void addMarker(IntervalMarker marker) { 126 this.markers.add(marker); 127 } 128 129 136 public double getHeight(Graphics2D g2) { 137 138 double result = 0.0; 139 if (this.markers.size() > 0) { 140 LineMetrics metrics = this.font.getLineMetrics( 141 "123g", g2.getFontRenderContext() 142 ); 143 result = this.topOuterGap + this.topInnerGap + metrics.getHeight() 144 + this.bottomInnerGap + this.bottomOuterGap; 145 } 146 return result; 147 148 } 149 150 158 private void drawStringInRect(Graphics2D g2, Rectangle2D bounds, Font font, 159 String text) { 160 161 g2.setFont(font); 162 FontMetrics fm = g2.getFontMetrics(font); 163 Rectangle2D r = TextUtilities.getTextBounds(text, g2, fm); 164 double x = bounds.getX(); 165 if (r.getWidth() < bounds.getWidth()) { 166 x = x + (bounds.getWidth() - r.getWidth()) / 2; 167 } 168 LineMetrics metrics = font.getLineMetrics( 169 text, g2.getFontRenderContext() 170 ); 171 g2.drawString( 172 text, (float) x, (float) (bounds.getMaxY() 173 - this.bottomInnerGap - metrics.getDescent()) 174 ); 175 } 176 177 186 public void draw(Graphics2D g2, Rectangle2D plotArea, Rectangle2D dataArea, 187 double x, double y) { 188 189 double h = getHeight(g2); 190 Iterator iterator = this.markers.iterator(); 191 while (iterator.hasNext()) { 192 IntervalMarker marker = (IntervalMarker) iterator.next(); 193 double start = Math.max( 194 marker.getStartValue(), this.axis.getRange().getLowerBound() 195 ); 196 double end = Math.min( 197 marker.getEndValue(), this.axis.getRange().getUpperBound() 198 ); 199 double s = this.axis.valueToJava2D( 200 start, dataArea, RectangleEdge.BOTTOM 201 ); 202 double e = this.axis.valueToJava2D( 203 end, dataArea, RectangleEdge.BOTTOM 204 ); 205 Rectangle2D r = new Rectangle2D.Double ( 206 s, y + this.topOuterGap, e - s, 207 h - this.topOuterGap - this.bottomOuterGap 208 ); 209 210 Composite originalComposite = g2.getComposite(); 211 g2.setComposite(AlphaComposite.getInstance( 212 AlphaComposite.SRC_OVER, marker.getAlpha()) 213 ); 214 g2.setPaint(marker.getPaint()); 215 g2.fill(r); 216 g2.setPaint(marker.getOutlinePaint()); 217 g2.draw(r); 218 g2.setComposite(originalComposite); 219 220 g2.setPaint(Color.black); 221 drawStringInRect(g2, r, this.font, marker.getLabel()); 222 } 223 224 } 225 226 234 public boolean equals(Object obj) { 235 if (obj == this) { 236 return true; 237 } 238 if (!(obj instanceof MarkerAxisBand)) { 239 return false; 240 } 241 MarkerAxisBand that = (MarkerAxisBand) obj; 242 if (this.topOuterGap != that.topOuterGap) { 243 return false; 244 } 245 if (this.topInnerGap != that.topInnerGap) { 246 return false; 247 } 248 if (this.bottomInnerGap != that.bottomInnerGap) { 249 return false; 250 } 251 if (this.bottomOuterGap != that.bottomOuterGap) { 252 return false; 253 } 254 if (!ObjectUtilities.equal(this.font, that.font)) { 255 return false; 256 } 257 if (!ObjectUtilities.equal(this.markers, that.markers)) { 258 return false; 259 } 260 return true; 261 } 262 263 268 public int hashCode() { 269 int result = 37; 270 result = 19 * result + this.font.hashCode(); 271 result = 19 * result + this.markers.hashCode(); 272 return result; 273 } 274 275 } 276 | Popular Tags |