KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > ext > awt > MultipleGradientPaint


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.ext.awt;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Paint JavaDoc;
22 import java.awt.geom.AffineTransform JavaDoc;
23
24 /** This is the superclass for Paints which use a multiple color
25  * gradient to fill in their raster. It provides storage for variables and
26  * enumerated values common to LinearGradientPaint and RadialGradientPaint.
27  *
28  *
29  * @author Nicholas Talian, Vincent Hardy, Jim Graham, Jerry Evans
30  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
31  * @version $Id: MultipleGradientPaint.java,v 1.7 2005/03/27 08:58:32 cam Exp $
32  *
33  */

34
35 public abstract class MultipleGradientPaint implements Paint JavaDoc {
36
37     /** Transparency. */
38     protected int transparency;
39
40     /** Gradient keyframe values in the range 0 to 1. */
41     protected float[] fractions;
42
43     /** Gradient colors. */
44     protected Color JavaDoc[] colors;
45
46     /** Transform to apply to gradient. */
47     protected AffineTransform JavaDoc gradientTransform;
48
49     /** The method to use when painting out of the gradient bounds. */
50     protected CycleMethodEnum cycleMethod;
51
52     /** The colorSpace in which to perform the interpolation. */
53     protected ColorSpaceEnum colorSpace;
54
55     /** Inner class to allow for typesafe enumerated ColorSpace values. */
56     public static class ColorSpaceEnum {
57     }
58
59     /** Inner class to allow for typesafe enumerated CycleMethod values. */
60     public static class CycleMethodEnum {
61     }
62
63     /** Indicates (if the gradient starts or ends inside the target region)
64      * to use the terminal colors to fill the remaining area. (default)
65      */

66     public static final CycleMethodEnum NO_CYCLE = new CycleMethodEnum();
67
68     /** Indicates (if the gradient starts or ends inside the target region),
69      * to cycle the gradient colors start-to-end, end-to-start to fill the
70      * remaining area.
71      */

72     public static final CycleMethodEnum REFLECT = new CycleMethodEnum();
73
74     /** Indicates (if the gradient starts or ends inside the target region),
75      * to cycle the gradient colors start-to-end, start-to-end to fill the
76      * remaining area.
77      */

78     public static final CycleMethodEnum REPEAT = new CycleMethodEnum();
79
80     /** Indicates that the color interpolation should occur in sRGB space.
81      * (default)
82      */

83     public static final ColorSpaceEnum SRGB = new ColorSpaceEnum();
84
85     /** Indicates that the color interpolation should occur in linearized
86      * RGB space.
87      */

88     public static final ColorSpaceEnum LINEAR_RGB = new ColorSpaceEnum();
89
90
91      /**
92      * Superclass constructor, typical user should never have to call this.
93      *
94      * @param fractions numbers ranging from 0.0 to 1.0 specifying the
95      * distribution of colors along the gradient
96      *
97      * @param colors array of colors corresponding to each fractional value
98      *
99      * @param cycleMethod either NO_CYCLE, REFLECT, or REPEAT
100      *
101      * @param colorSpace which colorspace to use for interpolation,
102      * either SRGB or LINEAR_RGB
103      *
104      * @param gradientTransform transform to apply to the gradient
105      *
106      * @throws NullPointerException if arrays are null, or
107      * gradientTransform is null
108      *
109      * @throws IllegalArgumentException if fractions.length != colors.length,
110      * or if colors is less than 2 in size, or if an enumerated value is bad.
111      */

112     public MultipleGradientPaint(float[] fractions,
113                                  Color JavaDoc[] colors,
114                                  CycleMethodEnum cycleMethod,
115                                  ColorSpaceEnum colorSpace,
116                                  AffineTransform JavaDoc gradientTransform) {
117
118         if (fractions == null) {
119             throw new IllegalArgumentException JavaDoc("Fractions array cannot be " +
120                                                "null");
121         }
122
123         if (colors == null) {
124             throw new IllegalArgumentException JavaDoc("Colors array cannot be null");
125         }
126
127         if (fractions.length != colors.length) {
128             throw new IllegalArgumentException JavaDoc("Colors and fractions must " +
129                                                "have equal size");
130         }
131
132         if (colors.length < 2) {
133             throw new IllegalArgumentException JavaDoc("User must specify at least " +
134                                                "2 colors");
135         }
136
137         if ((colorSpace != LINEAR_RGB) &&
138             (colorSpace != SRGB)) {
139             throw new IllegalArgumentException JavaDoc("Invalid colorspace for " +
140                                                "interpolation.");
141         }
142
143         if ((cycleMethod != NO_CYCLE) &&
144             (cycleMethod != REFLECT) &&
145             (cycleMethod != REPEAT)) {
146             throw new IllegalArgumentException JavaDoc("Invalid cycle method.");
147         }
148
149         if (gradientTransform == null) {
150             throw new IllegalArgumentException JavaDoc("Gradient transform cannot be "+
151                                                "null.");
152         }
153
154         //copy the fractions array
155
this.fractions = new float[fractions.length];
156         System.arraycopy(fractions, 0, this.fractions, 0, fractions.length);
157
158         //copy the colors array
159
this.colors = new Color JavaDoc[colors.length];
160         System.arraycopy(colors, 0, this.colors, 0, colors.length);
161
162         //copy some flags
163
this.colorSpace = colorSpace;
164         this.cycleMethod = cycleMethod;
165
166         //copy the gradient transform
167
this.gradientTransform = (AffineTransform JavaDoc)gradientTransform.clone();
168
169         // Process transparency
170
boolean opaque = true;
171         for(int i=0; i<colors.length; i++){
172             opaque = opaque && (colors[i].getAlpha()==0xff);
173         }
174
175         if(opaque) {
176             transparency = OPAQUE;
177         }
178
179         else {
180             transparency = TRANSLUCENT;
181         }
182     }
183
184     /**
185      * Returns a copy of the array of colors used by this gradient.
186      * @return a copy of the array of colors used by this gradient
187      *
188      */

189     public Color JavaDoc[] getColors() {
190         Color JavaDoc colors[] = new Color JavaDoc[this.colors.length];
191         System.arraycopy(this.colors, 0, colors, 0, this.colors.length);
192         return colors;
193     }
194
195     /**
196      * Returns a copy of the array of floats used by this gradient
197      * to calculate color distribution.
198      * @return a copy of the array of floats used by this gradient to
199      * calculate color distribution
200      *
201      */

202     public float[] getFractions() {
203         float fractions[] = new float[this.fractions.length];
204         System.arraycopy(this.fractions, 0, fractions, 0, this.fractions.length);
205         return fractions;
206     }
207
208     /**
209      * Returns the transparency mode for this LinearGradientPaint.
210      * @return an integer value representing this LinearGradientPaint object's
211      * transparency mode.
212      */

213     public int getTransparency() {
214         return transparency;
215     }
216
217     /**
218      * Returns the enumerated type which specifies cycling behavior.
219      * @return the enumerated type which specifies cycling behavior
220      */

221     public CycleMethodEnum getCycleMethod() {
222         return cycleMethod;
223     }
224
225     /**
226      * Returns the enumerated type which specifies color space for
227      * interpolation.
228      * @return the enumerated type which specifies color space for
229      * interpolation
230      */

231     public ColorSpaceEnum getColorSpace() {
232         return colorSpace;
233     }
234
235     /**
236      * Returns a copy of the transform applied to the gradient.
237      * @return a copy of the transform applied to the gradient.
238      */

239     public AffineTransform JavaDoc getTransform() {
240         return (AffineTransform JavaDoc)gradientTransform.clone();
241     }
242 }
243
Popular Tags