1 7 8 package java.awt; 9 10 import java.awt.geom.AffineTransform ; 11 import java.awt.image.ColorModel ; 12 import java.lang.ref.SoftReference ; 13 import java.util.Arrays ; 14 15 24 public abstract class MultipleGradientPaint implements Paint { 25 26 29 public static enum CycleMethod { 30 33 NO_CYCLE, 34 35 39 REFLECT, 40 41 45 REPEAT 46 } 47 48 51 public static enum ColorSpaceType { 52 55 SRGB, 56 57 61 LINEAR_RGB 62 } 63 64 65 final int transparency; 66 67 68 final float[] fractions; 69 70 71 final Color [] colors; 72 73 74 final AffineTransform gradientTransform; 75 76 77 final CycleMethod cycleMethod; 78 79 80 final ColorSpaceType colorSpace; 81 82 87 ColorModel model; 88 float[] normalizedIntervals; 89 boolean isSimpleLookup; 90 SoftReference <int[][]> gradients; 91 SoftReference <int[]> gradient; 92 int fastGradientArraySize; 93 94 118 MultipleGradientPaint(float[] fractions, 119 Color [] colors, 120 CycleMethod cycleMethod, 121 ColorSpaceType colorSpace, 122 AffineTransform gradientTransform) 123 { 124 if (fractions == null) { 125 throw new NullPointerException ("Fractions array cannot be null"); 126 } 127 128 if (colors == null) { 129 throw new NullPointerException ("Colors array cannot be null"); 130 } 131 132 if (cycleMethod == null) { 133 throw new NullPointerException ("Cycle method cannot be null"); 134 } 135 136 if (colorSpace == null) { 137 throw new NullPointerException ("Color space cannot be null"); 138 } 139 140 if (gradientTransform == null) { 141 throw new NullPointerException ("Gradient transform cannot be "+ 142 "null"); 143 } 144 145 if (fractions.length != colors.length) { 146 throw new IllegalArgumentException ("Colors and fractions must " + 147 "have equal size"); 148 } 149 150 if (colors.length < 2) { 151 throw new IllegalArgumentException ("User must specify at least " + 152 "2 colors"); 153 } 154 155 float previousFraction = -1.0f; 158 for (float currentFraction : fractions) { 159 if (currentFraction < 0f || currentFraction > 1f) { 160 throw new IllegalArgumentException ("Fraction values must " + 161 "be in the range 0 to 1: " + 162 currentFraction); 163 } 164 165 if (currentFraction <= previousFraction) { 166 throw new IllegalArgumentException ("Keyframe fractions " + 167 "must be increasing: " + 168 currentFraction); 169 } 170 171 previousFraction = currentFraction; 172 } 173 174 boolean fixFirst = false; 179 boolean fixLast = false; 180 int len = fractions.length; 181 int off = 0; 182 183 if (fractions[0] != 0f) { 184 fixFirst = true; 186 len++; 187 off++; 188 } 189 if (fractions[fractions.length-1] != 1f) { 190 fixLast = true; 192 len++; 193 } 194 195 this.fractions = new float[len]; 196 System.arraycopy(fractions, 0, this.fractions, off, fractions.length); 197 this.colors = new Color [len]; 198 System.arraycopy(colors, 0, this.colors, off, colors.length); 199 200 if (fixFirst) { 201 this.fractions[0] = 0f; 202 this.colors[0] = colors[0]; 203 } 204 if (fixLast) { 205 this.fractions[len-1] = 1f; 206 this.colors[len-1] = colors[colors.length - 1]; 207 } 208 209 this.colorSpace = colorSpace; 211 this.cycleMethod = cycleMethod; 212 213 this.gradientTransform = new AffineTransform (gradientTransform); 215 216 boolean opaque = true; 218 for (int i = 0; i < colors.length; i++){ 219 opaque = opaque && (colors[i].getAlpha() == 0xff); 220 } 221 this.transparency = opaque ? OPAQUE : TRANSLUCENT; 222 } 223 224 233 public final float[] getFractions() { 234 return Arrays.copyOf(fractions, fractions.length); 235 } 236 237 244 public final Color [] getColors() { 245 return Arrays.copyOf(colors, colors.length); 246 } 247 248 253 public final CycleMethod getCycleMethod() { 254 return cycleMethod; 255 } 256 257 264 public final ColorSpaceType getColorSpace() { 265 return colorSpace; 266 } 267 268 273 public final AffineTransform getTransform() { 274 return new AffineTransform (gradientTransform); 275 } 276 277 284 public final int getTransparency() { 285 return transparency; 286 } 287 } 288
| Popular Tags
|