1 45 46 package org.jfree.chart; 47 48 import java.awt.BasicStroke ; 49 import java.awt.Composite ; 50 import java.awt.Graphics2D ; 51 import java.awt.Paint ; 52 import java.awt.Stroke ; 53 import java.awt.geom.GeneralPath ; 54 import java.awt.geom.Rectangle2D ; 55 56 import org.jfree.chart.axis.ValueAxis; 57 import org.jfree.ui.RectangleEdge; 58 59 68 public class ClipPath implements Cloneable { 69 70 71 private double[] xValue = null; 72 73 74 private double[] yValue = null; 75 76 78 private boolean clip = true; 79 80 81 private boolean drawPath = false; 82 83 84 private boolean fillPath = false; 85 86 87 private Paint fillPaint = null; 88 89 90 private Paint drawPaint = null; 91 92 93 private Stroke drawStroke = null; 94 95 96 private Composite composite = null; 97 98 101 public ClipPath() { 102 super(); 103 } 104 105 115 public ClipPath(double[] xValue, double[] yValue) { 116 this(xValue, yValue, true, false, true); 117 } 118 119 120 131 public ClipPath(double[] xValue, double[] yValue, 132 boolean clip, boolean fillPath, boolean drawPath) { 133 this.xValue = xValue; 134 this.yValue = yValue; 135 136 this.clip = clip; 137 this.fillPath = fillPath; 138 this.drawPath = drawPath; 139 140 this.fillPaint = java.awt.Color.gray; 141 this.drawPaint = java.awt.Color.blue; 142 this.drawStroke = new BasicStroke (1); 143 this.composite = java.awt.AlphaComposite.Src; 144 } 145 146 158 public ClipPath(double[] xValue, double[] yValue, boolean fillPath, 159 boolean drawPath, Paint fillPaint, Paint drawPaint, 160 Stroke drawStroke, Composite composite) { 161 162 this.xValue = xValue; 163 this.yValue = yValue; 164 165 this.fillPath = fillPath; 166 this.drawPath = drawPath; 167 168 this.fillPaint = fillPaint; 169 this.drawPaint = drawPaint; 170 this.drawStroke = drawStroke; 171 this.composite = composite; 172 173 } 174 175 185 public GeneralPath draw(Graphics2D g2, 186 Rectangle2D dataArea, 187 ValueAxis horizontalAxis, ValueAxis verticalAxis) { 188 189 GeneralPath generalPath = generateClipPath( 190 dataArea, horizontalAxis, verticalAxis 191 ); 192 if (this.fillPath || this.drawPath) { 193 Composite saveComposite = g2.getComposite(); 194 Paint savePaint = g2.getPaint(); 195 Stroke saveStroke = g2.getStroke(); 196 197 if (this.fillPaint != null) { 198 g2.setPaint(this.fillPaint); 199 } 200 if (this.composite != null) { 201 g2.setComposite(this.composite); 202 } 203 if (this.fillPath) { 204 g2.fill(generalPath); 205 } 206 207 if (this.drawStroke != null) { 208 g2.setStroke(this.drawStroke); 209 } 210 if (this.drawPath) { 211 g2.draw(generalPath); 212 } 213 g2.setPaint(savePaint); 214 g2.setComposite(saveComposite); 215 g2.setStroke(saveStroke); 216 } 217 return generalPath; 218 219 } 220 221 230 public GeneralPath generateClipPath(Rectangle2D dataArea, 231 ValueAxis horizontalAxis, 232 ValueAxis verticalAxis) { 233 234 GeneralPath generalPath = new GeneralPath (); 235 double transX = horizontalAxis.valueToJava2D( 236 this.xValue[0], dataArea, RectangleEdge.BOTTOM 237 ); 238 double transY = verticalAxis.valueToJava2D( 239 this.yValue[0], dataArea, RectangleEdge.LEFT 240 ); 241 generalPath.moveTo((float) transX, (float) transY); 242 for (int k = 0; k < this.yValue.length; k++) { 243 transX = horizontalAxis.valueToJava2D( 244 this.xValue[k], dataArea, RectangleEdge.BOTTOM 245 ); 246 transY = verticalAxis.valueToJava2D( 247 this.yValue[k], dataArea, RectangleEdge.LEFT 248 ); 249 generalPath.lineTo((float) transX, (float) transY); 250 } 251 generalPath.closePath(); 252 253 return generalPath; 254 255 } 256 257 262 public Composite getComposite() { 263 return this.composite; 264 } 265 266 271 public Paint getDrawPaint() { 272 return this.drawPaint; 273 } 274 275 280 public boolean isDrawPath() { 281 return this.drawPath; 282 } 283 284 289 public Stroke getDrawStroke() { 290 return this.drawStroke; 291 } 292 293 298 public Paint getFillPaint() { 299 return this.fillPaint; 300 } 301 302 307 public boolean isFillPath() { 308 return this.fillPath; 309 } 310 311 316 public double[] getXValue() { 317 return this.xValue; 318 } 319 320 325 public double[] getYValue() { 326 return this.yValue; 327 } 328 329 334 public void setComposite(Composite composite) { 335 this.composite = composite; 336 } 337 338 343 public void setDrawPaint(Paint drawPaint) { 344 this.drawPaint = drawPaint; 345 } 346 347 352 public void setDrawPath(boolean drawPath) { 353 this.drawPath = drawPath; 354 } 355 356 361 public void setDrawStroke(Stroke drawStroke) { 362 this.drawStroke = drawStroke; 363 } 364 365 370 public void setFillPaint(Paint fillPaint) { 371 this.fillPaint = fillPaint; 372 } 373 374 379 public void setFillPath(boolean fillPath) { 380 this.fillPath = fillPath; 381 } 382 383 388 public void setXValue(double[] xValue) { 389 this.xValue = xValue; 390 } 391 392 397 public void setYValue(double[] yValue) { 398 this.yValue = yValue; 399 } 400 401 406 public boolean isClip() { 407 return this.clip; 408 } 409 410 415 public void setClip(boolean clip) { 416 this.clip = clip; 417 } 418 419 427 public Object clone() throws CloneNotSupportedException { 428 ClipPath clone = (ClipPath) super.clone(); 429 clone.xValue = (double[]) this.xValue.clone(); 430 clone.yValue = (double[]) this.yValue.clone(); 431 return clone; 432 } 433 434 } 435 | Popular Tags |