1 7 8 package java.awt; 9 10 import java.awt.geom.AffineTransform ; 11 import java.awt.geom.Point2D ; 12 import java.awt.geom.Rectangle2D ; 13 import java.awt.image.ColorModel ; 14 15 115 public final class RadialGradientPaint extends MultipleGradientPaint { 116 117 118 private final Point2D focus; 119 120 121 private final Point2D center; 122 123 124 private final float radius; 125 126 155 public RadialGradientPaint(float cx, float cy, float radius, 156 float[] fractions, Color [] colors) 157 { 158 this(cx, cy, 159 radius, 160 cx, cy, 161 fractions, 162 colors, 163 CycleMethod.NO_CYCLE); 164 } 165 166 192 public RadialGradientPaint(Point2D center, float radius, 193 float[] fractions, Color [] colors) 194 { 195 this(center, 196 radius, 197 center, 198 fractions, 199 colors, 200 CycleMethod.NO_CYCLE); 201 } 202 203 234 public RadialGradientPaint(float cx, float cy, float radius, 235 float[] fractions, Color [] colors, 236 CycleMethod cycleMethod) 237 { 238 this(cx, cy, 239 radius, 240 cx, cy, 241 fractions, 242 colors, 243 cycleMethod); 244 } 245 246 274 public RadialGradientPaint(Point2D center, float radius, 275 float[] fractions, Color [] colors, 276 CycleMethod cycleMethod) 277 { 278 this(center, 279 radius, 280 center, 281 fractions, 282 colors, 283 cycleMethod); 284 } 285 286 321 public RadialGradientPaint(float cx, float cy, float radius, 322 float fx, float fy, 323 float[] fractions, Color [] colors, 324 CycleMethod cycleMethod) 325 { 326 this(new Point2D.Float (cx, cy), 327 radius, 328 new Point2D.Float (fx, fy), 329 fractions, 330 colors, 331 cycleMethod); 332 } 333 334 364 public RadialGradientPaint(Point2D center, float radius, 365 Point2D focus, 366 float[] fractions, Color [] colors, 367 CycleMethod cycleMethod) 368 { 369 this(center, 370 radius, 371 focus, 372 fractions, 373 colors, 374 cycleMethod, 375 ColorSpaceType.SRGB, 376 new AffineTransform ()); 377 } 378 379 413 public RadialGradientPaint(Point2D center, 414 float radius, 415 Point2D focus, 416 float[] fractions, Color [] colors, 417 CycleMethod cycleMethod, 418 ColorSpaceType colorSpace, 419 AffineTransform gradientTransform) 420 { 421 super(fractions, colors, cycleMethod, colorSpace, gradientTransform); 422 423 if (center == null) { 425 throw new NullPointerException ("Center point must be non-null"); 426 } 427 428 if (focus == null) { 429 throw new NullPointerException ("Focus point must be non-null"); 430 } 431 432 if (radius <= 0) { 433 throw new IllegalArgumentException ("Radius must be greater " + 434 "than zero"); 435 } 436 437 this.center = new Point2D.Double (center.getX(), center.getY()); 439 this.focus = new Point2D.Double (focus.getX(), focus.getY()); 440 this.radius = radius; 441 } 442 443 494 public RadialGradientPaint(Rectangle2D gradientBounds, 495 float[] fractions, Color [] colors, 496 CycleMethod cycleMethod) 497 { 498 this(new Point2D.Double (gradientBounds.getCenterX(), 502 gradientBounds.getCenterY()), 503 1.0f, 504 new Point2D.Double (gradientBounds.getCenterX(), 505 gradientBounds.getCenterY()), 506 fractions, 507 colors, 508 cycleMethod, 509 ColorSpaceType.SRGB, 510 createGradientTransform(gradientBounds)); 511 512 if (gradientBounds.isEmpty()) { 513 throw new IllegalArgumentException ("Gradient bounds must be " + 514 "non-empty"); 515 } 516 } 517 518 private static AffineTransform createGradientTransform(Rectangle2D r) { 519 double cx = r.getCenterX(); 520 double cy = r.getCenterY(); 521 AffineTransform xform = AffineTransform.getTranslateInstance(cx, cy); 522 xform.scale(r.getWidth()/2, r.getHeight()/2); 523 xform.translate(-cx, -cy); 524 return xform; 525 } 526 527 530 public PaintContext createContext(ColorModel cm, 531 Rectangle deviceBounds, 532 Rectangle2D userBounds, 533 AffineTransform transform, 534 RenderingHints hints) 535 { 536 transform = new AffineTransform (transform); 538 transform.concatenate(gradientTransform); 540 541 return new RadialGradientPaintContext (this, cm, 542 deviceBounds, userBounds, 543 transform, hints, 544 (float)center.getX(), 545 (float)center.getY(), 546 radius, 547 (float)focus.getX(), 548 (float)focus.getY(), 549 fractions, colors, 550 cycleMethod, colorSpace); 551 } 552 553 558 public Point2D getCenterPoint() { 559 return new Point2D.Double (center.getX(), center.getY()); 560 } 561 562 567 public Point2D getFocusPoint() { 568 return new Point2D.Double (focus.getX(), focus.getY()); 569 } 570 571 576 public float getRadius() { 577 return radius; 578 } 579 } 580 | Popular Tags |