1 7 8 package java.awt; 9 10 import java.awt.geom.AffineTransform ; 11 import java.awt.geom.NoninvertibleTransformException ; 12 import java.awt.geom.Point2D ; 13 import java.awt.geom.Rectangle2D ; 14 import java.awt.image.ColorModel ; 15 16 81 public final class LinearGradientPaint extends MultipleGradientPaint { 82 83 84 private final Point2D start, end; 85 86 112 public LinearGradientPaint(float startX, float startY, 113 float endX, float endY, 114 float[] fractions, Color [] colors) 115 { 116 this(new Point2D.Float (startX, startY), 117 new Point2D.Float (endX, endY), 118 fractions, 119 colors, 120 CycleMethod.NO_CYCLE); 121 } 122 123 152 public LinearGradientPaint(float startX, float startY, 153 float endX, float endY, 154 float[] fractions, Color [] colors, 155 CycleMethod cycleMethod) 156 { 157 this(new Point2D.Float (startX, startY), 158 new Point2D.Float (endX, endY), 159 fractions, 160 colors, 161 cycleMethod); 162 } 163 164 185 public LinearGradientPaint(Point2D start, Point2D end, 186 float[] fractions, Color [] colors) 187 { 188 this(start, end, 189 fractions, colors, 190 CycleMethod.NO_CYCLE); 191 } 192 193 217 public LinearGradientPaint(Point2D start, Point2D end, 218 float[] fractions, Color [] colors, 219 CycleMethod cycleMethod) 220 { 221 this(start, end, 222 fractions, colors, 223 cycleMethod, 224 ColorSpaceType.SRGB, 225 new AffineTransform ()); 226 } 227 228 256 public LinearGradientPaint(Point2D start, Point2D end, 257 float[] fractions, Color [] colors, 258 CycleMethod cycleMethod, 259 ColorSpaceType colorSpace, 260 AffineTransform gradientTransform) 261 { 262 super(fractions, colors, cycleMethod, colorSpace, gradientTransform); 263 264 if (start == null || end == null) { 266 throw new NullPointerException ("Start and end points must be" + 267 "non-null"); 268 } 269 270 if (start.equals(end)) { 271 throw new IllegalArgumentException ("Start point cannot equal" + 272 "endpoint"); 273 } 274 275 this.start = new Point2D.Double (start.getX(), start.getY()); 277 this.end = new Point2D.Double (end.getX(), end.getY()); 278 } 279 280 283 public PaintContext createContext(ColorModel cm, 284 Rectangle deviceBounds, 285 Rectangle2D userBounds, 286 AffineTransform transform, 287 RenderingHints hints) 288 { 289 transform = new AffineTransform (transform); 291 transform.concatenate(gradientTransform); 293 294 if ((fractions.length == 2) && 295 (cycleMethod != CycleMethod.REPEAT) && 296 (colorSpace == ColorSpaceType.SRGB)) 297 { 298 boolean cyclic = (cycleMethod != CycleMethod.NO_CYCLE); 301 return new GradientPaintContext (cm, start, end, 302 transform, 303 colors[0], colors[1], 304 cyclic); 305 } else { 306 return new LinearGradientPaintContext (this, cm, 307 deviceBounds, userBounds, 308 transform, hints, 309 start, end, 310 fractions, colors, 311 cycleMethod, colorSpace); 312 } 313 } 314 315 321 public Point2D getStartPoint() { 322 return new Point2D.Double (start.getX(), start.getY()); 323 } 324 325 331 public Point2D getEndPoint() { 332 return new Point2D.Double (end.getX(), end.getY()); 333 } 334 } 335 | Popular Tags |