1 42 43 package org.jfree.chart.annotations; 44 45 import java.awt.BasicStroke ; 46 import java.awt.Color ; 47 import java.awt.Graphics2D ; 48 import java.awt.Paint ; 49 import java.awt.Stroke ; 50 import java.awt.geom.Rectangle2D ; 51 import java.io.IOException ; 52 import java.io.ObjectInputStream ; 53 import java.io.ObjectOutputStream ; 54 import java.io.Serializable ; 55 56 import org.jfree.chart.axis.CategoryAnchor; 57 import org.jfree.chart.axis.CategoryAxis; 58 import org.jfree.chart.axis.ValueAxis; 59 import org.jfree.chart.plot.CategoryPlot; 60 import org.jfree.chart.plot.Plot; 61 import org.jfree.chart.plot.PlotOrientation; 62 import org.jfree.data.category.CategoryDataset; 63 import org.jfree.io.SerialUtilities; 64 import org.jfree.ui.RectangleEdge; 65 import org.jfree.util.ObjectUtilities; 66 import org.jfree.util.PaintUtilities; 67 68 72 public class CategoryLineAnnotation implements CategoryAnnotation, 73 Cloneable , Serializable { 74 75 76 private Comparable category1; 77 78 79 private double value1; 80 81 82 private Comparable category2; 83 84 85 private double value2; 86 87 88 private transient Paint paint = Color.black; 89 90 91 private transient Stroke stroke = new BasicStroke (1.0f); 92 93 104 public CategoryLineAnnotation(Comparable category1, double value1, 105 Comparable category2, double value2, 106 Paint paint, Stroke stroke) { 107 if (category1 == null) { 108 throw new IllegalArgumentException ("Null 'category1' argument."); 109 } 110 if (category2 == null) { 111 throw new IllegalArgumentException ("Null 'category2' argument."); 112 } 113 if (paint == null) { 114 throw new IllegalArgumentException ("Null 'paint' argument."); 115 } 116 if (stroke == null) { 117 throw new IllegalArgumentException ("Null 'stroke' argument."); 118 } 119 this.category1 = category1; 120 this.value1 = value1; 121 this.category2 = category2; 122 this.value2 = value2; 123 this.paint = paint; 124 this.stroke = stroke; 125 } 126 127 132 public Comparable getCategory1() { 133 return this.category1; 134 } 135 136 141 public void setCategory1(Comparable category) { 142 if (category == null) { 143 throw new IllegalArgumentException ("Null 'category' argument."); 144 } 145 this.category1 = category; 146 } 147 148 153 public double getValue1() { 154 return this.value1; 155 } 156 157 162 public void setValue1(double value) { 163 this.value1 = value; 164 } 165 166 171 public Comparable getCategory2() { 172 return this.category2; 173 } 174 175 180 public void setCategory2(Comparable category) { 181 if (category == null) { 182 throw new IllegalArgumentException ("Null 'category' argument."); 183 } 184 this.category2 = category; 185 } 186 187 192 public double getValue2() { 193 return this.value2; 194 } 195 196 201 public void setValue2(double value) { 202 this.value2 = value; 203 } 204 205 210 public Paint getPaint() { 211 return this.paint; 212 } 213 214 219 public void setPaint(Paint paint) { 220 if (paint == null) { 221 throw new IllegalArgumentException ("Null 'paint' argument."); 222 } 223 this.paint = paint; 224 } 225 226 231 public Stroke getStroke() { 232 return this.stroke; 233 } 234 235 240 public void setStroke(Stroke stroke) { 241 if (stroke == null) { 242 throw new IllegalArgumentException ("Null 'stroke' argument."); 243 } 244 this.stroke = stroke; 245 } 246 247 256 public void draw(Graphics2D g2, CategoryPlot plot, Rectangle2D dataArea, 257 CategoryAxis domainAxis, ValueAxis rangeAxis) { 258 259 CategoryDataset dataset = plot.getDataset(); 260 int catIndex1 = dataset.getColumnIndex(this.category1); 261 int catIndex2 = dataset.getColumnIndex(this.category2); 262 int catCount = dataset.getColumnCount(); 263 264 double lineX1 = 0.0f; 265 double lineY1 = 0.0f; 266 double lineX2 = 0.0f; 267 double lineY2 = 0.0f; 268 PlotOrientation orientation = plot.getOrientation(); 269 RectangleEdge domainEdge = Plot.resolveDomainAxisLocation( 270 plot.getDomainAxisLocation(), orientation); 271 RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation( 272 plot.getRangeAxisLocation(), orientation); 273 274 if (orientation == PlotOrientation.HORIZONTAL) { 275 lineY1 = domainAxis.getCategoryJava2DCoordinate( 276 CategoryAnchor.MIDDLE, catIndex1, catCount, dataArea, 277 domainEdge); 278 lineX1 = rangeAxis.valueToJava2D(this.value1, dataArea, rangeEdge); 279 lineY2 = domainAxis.getCategoryJava2DCoordinate( 280 CategoryAnchor.MIDDLE, catIndex2, catCount, dataArea, 281 domainEdge); 282 lineX2 = rangeAxis.valueToJava2D(this.value2, dataArea, rangeEdge); 283 } 284 else if (orientation == PlotOrientation.VERTICAL) { 285 lineX1 = domainAxis.getCategoryJava2DCoordinate( 286 CategoryAnchor.MIDDLE, catIndex1, catCount, dataArea, 287 domainEdge); 288 lineY1 = rangeAxis.valueToJava2D(this.value1, dataArea, rangeEdge); 289 lineX2 = domainAxis.getCategoryJava2DCoordinate( 290 CategoryAnchor.MIDDLE, catIndex2, catCount, dataArea, 291 domainEdge); 292 lineY2 = rangeAxis.valueToJava2D(this.value2, dataArea, rangeEdge); 293 } 294 g2.setPaint(this.paint); 295 g2.setStroke(this.stroke); 296 g2.drawLine((int) lineX1, (int) lineY1, (int) lineX2, (int) lineY2); 297 } 298 299 306 public boolean equals(Object obj) { 307 if (obj == this) { 308 return true; 309 } 310 if (!(obj instanceof CategoryLineAnnotation)) { 311 return false; 312 } 313 CategoryLineAnnotation that = (CategoryLineAnnotation) obj; 314 if (!this.category1.equals(that.getCategory1())) { 315 return false; 316 } 317 if (this.value1 != that.getValue1()) { 318 return false; 319 } 320 if (!this.category2.equals(that.getCategory2())) { 321 return false; 322 } 323 if (this.value2 != that.getValue2()) { 324 return false; 325 } 326 if (!PaintUtilities.equal(this.paint, that.paint)) { 327 return false; 328 } 329 if (!ObjectUtilities.equal(this.stroke, that.stroke)) { 330 return false; 331 } 332 return true; 333 } 334 335 340 public int hashCode() { 341 return this.category1.hashCode() + this.category2.hashCode(); 343 } 344 345 353 public Object clone() throws CloneNotSupportedException { 354 return super.clone(); 355 } 356 357 364 private void writeObject(ObjectOutputStream stream) throws IOException { 365 stream.defaultWriteObject(); 366 SerialUtilities.writePaint(this.paint, stream); 367 SerialUtilities.writeStroke(this.stroke, stream); 368 } 369 370 378 private void readObject(ObjectInputStream stream) 379 throws IOException , ClassNotFoundException { 380 stream.defaultReadObject(); 381 this.paint = SerialUtilities.readPaint(stream); 382 this.stroke = SerialUtilities.readStroke(stream); 383 } 384 385 } 386 | Popular Tags |