Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/batik/ext/awt/LinearGradientPaint.java


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;
21  import java.awt.PaintContext;
22  import java.awt.Rectangle;
23  import java.awt.RenderingHints;
24  import java.awt.geom.AffineTransform;
25  import java.awt.geom.NoninvertibleTransformException;
26  import java.awt.geom.Point2D;
27  import java.awt.geom.Rectangle2D;
28  import java.awt.image.ColorModel;
29  
30  /**
31   * The <code>LinearGradientPaint</code> class provides a way to fill
32   * a {@link java.awt.Shape} with a linear color gradient pattern.  The user may
33   * specify 2 or more gradient colors, and this paint will provide an
34   * interpolation between each color.  The user also specifies start and end
35   * points which define where in user space the color gradient should begin 
36   * and end.
37   * <p>
38   * The user must provide an array of floats specifying how to distribute the
39   * colors along the gradient.  These values should range from 0.0 to 1.0 and 
40   * act like keyframes along the gradient (they mark where the gradient should 
41   * be exactly a particular color).
42   * <p>
43   * For example:
44   * <br>
45   * <code>
46   * <p>
47   * Point2D start = new Point2D.Float(0, 0);<br>
48   * Point2D end = new Point2D.Float(100,100);<br>
49   * float[] dist = {0.0, 0.2, 1.0};<br>
50   * Color[] colors = {Color.red, Color.white, Color.blue};<br>
51   * LinearGradientPaint p = new LinearGradientPaint(start, end, dist, colors);
52   * </code>
53   *<p>
54   * This code will create a LinearGradientPaint which interpolates between 
55   * red and white for the first 20% of the gradient and between white and blue 
56   * for the remaining 80%.
57   *
58   * <p> In the event that the user does not set the first keyframe value equal
59   * to 0 and the last keyframe value equal to 1, keyframes will be created at
60   * these positions and the first and last colors will be replicated there.
61   * So, if a user specifies the following arrays to construct a gradient:<br>
62   * {Color.blue, Color.red}, {.3, .7}<br>
63   * this will be converted to a gradient with the following keyframes:
64   * {Color.blue, Color.blue, Color.red, Color.red}, {0, .3, .7, 1}
65   *
66   * <p>
67   * The user may also select what action the LinearGradientPaint should take
68   * when filling color outside the start and end points. If no cycle method is
69   * specified, NO_CYCLE will be chosen by default, so the endpoint colors 
70   * will be used to fill the remaining area.  
71   *
72   * <p> The following image demonstrates the options NO_CYCLE and REFLECT.
73   *
74   * <p>
75   * <img src = "cyclic.jpg">
76   *
77   * <p> The colorSpace parameter allows the user to specify in which colorspace
78   *  the interpolation should be performed, default sRGB or linearized RGB.
79   *  
80   *
81   * @author Nicholas Talian, Vincent Hardy, Jim Graham, Jerry Evans
82   * @author <a href="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
83   * @version $Id: LinearGradientPaint.java,v 1.6 2005/03/27 08:58:32 cam Exp $
84   * @see java.awt.Paint
85   * @see java.awt.Graphics2D#setPaint
86   *
87   */
88  public final class LinearGradientPaint extends MultipleGradientPaint {
89  
90      /** Gradient start and end points. */
91      private Point2D start, end;   
92         
93      /**<p>
94       * Constructs an <code>LinearGradientPaint</code> with the default 
95       * NO_CYCLE repeating method and SRGB colorspace.
96       *
97       * @param startX the x coordinate of the gradient axis start point 
98       * in user space
99       *
100      * @param startY the y coordinate of the gradient axis start point 
101      * in user space
102      *
103      * @param endX the x coordinate of the gradient axis end point 
104      * in user space
105      *
106      * @param endY the y coordinate of the gradient axis end point 
107      * in user space
108      *
109      * @param fractions numbers ranging from 0.0 to 1.0 specifying the 
110      * distribution of colors along the gradient
111      *
112      * @param colors array of colors corresponding to each fractional value
113      *     
114      *
115      * @throws IllegalArgumentException if start and end points are the 
116      * same points, or if fractions.length != colors.length, or if colors 
117      * is less than 2 in size.
118      *
119      */
120     public LinearGradientPaint(float startX, float startY, 
121                                float endX, float endY, 
122                                float[] fractions, Color[] colors) {
123 
124         this(new Point2D.Float(startX, startY),
125              new Point2D.Float(endX, endY), 
126              fractions, 
127              colors,
128              NO_CYCLE,
129              SRGB);
130     }
131 
132     /**<p>
133      * Constructs an <code>LinearGradientPaint</code> with default SRGB 
134      * colorspace.
135      *
136      * @param startX the x coordinate of the gradient axis start point 
137      * in user space
138      *
139      * @param startY the y coordinate of the gradient axis start point 
140      * in user space
141      *
142      * @param endX the x coordinate of the gradient axis end point 
143      * in user space
144      * 
145      * @param endY the y coordinate of the gradient axis end point 
146      * in user space
147      *
148      * @param fractions numbers ranging from 0.0 to 1.0 specifying the 
149      * distribution of colors along the gradient
150      *
151      * @param colors array of colors corresponding to each fractional value
152      *
153      * @param cycleMethod either NO_CYCLE, REFLECT, or REPEAT
154      *
155      * @throws IllegalArgumentException if start and end points are the 
156      * same points, or if fractions.length != colors.length, or if colors 
157      * is less than 2 in size.
158      *
159      */
160     public LinearGradientPaint(float startX, float startY, 
161                                float endX, float endY, 
162                                float[] fractions, Color[] colors, 
163                                CycleMethodEnum cycleMethod) {
164         this(new Point2D.Float(startX, startY), 
165              new Point2D.Float(endX, endY), 
166              fractions, 
167              colors,
168              cycleMethod,
169              SRGB);
170     }
171 
172     /**<p>
173      * Constructs a <code>LinearGradientPaint</code> with the default 
174      * NO_CYCLE repeating method and SRGB colorspace.
175      *
176      * @param start the gradient axis start <code>Point</code> in user space
177      *
178      * @param end the gradient axis end <code>Point</code> in user space
179      *
180      * @param fractions numbers ranging from 0.0 to 1.0 specifying the 
181      * distribution of colors along the gradient
182      *
183      * @param colors array of colors corresponding to each fractional value
184      *
185      * @throws NullPointerException if one of the points is null
186      *
187      * @throws IllegalArgumentException if start and end points are the 
188      * same points, or if fractions.length != colors.length, or if colors 
189      * is less than 2 in size.
190      *
191      */
192     public LinearGradientPaint(Point2D start, Point2D end, float[] fractions,
193                                Color[] colors) {
194 
195         this(start, end, fractions, colors, NO_CYCLE, SRGB);
196     }
197     
198     /**<p>
199      * Constructs a <code>LinearGradientPaint</code>.
200      *
201      * @param start the gradient axis start <code>Point</code> in user space
202      *
203      * @param end the gradient axis end <code>Point</code> in user space
204      *
205      * @param fractions numbers ranging from 0.0 to 1.0 specifying the 
206      * distribution of colors along the gradient
207      *
208      * @param colors array of colors corresponding to each fractional value
209      *
210      * @param cycleMethod either NO_CYCLE, REFLECT, or REPEAT
211      *
212      * @param colorSpace which colorspace to use for interpolation, 
213      * either SRGB or LINEAR_RGB
214      *   
215      * @throws NullPointerException if one of the points is null
216      *
217      * @throws IllegalArgumentException if start and end points are the 
218      * same points, or if fractions.length != colors.length, or if colors 
219      * is less than 2 in size.
220      *
221      */
222     public LinearGradientPaint(Point2D start, Point2D end, float[] fractions,
223                                Color[] colors, 
224                                CycleMethodEnum cycleMethod, 
225                                ColorSpaceEnum colorSpace) {
226   
227         this(start, end, fractions, colors, cycleMethod, colorSpace, 
228              new AffineTransform());
229   
230     }
231     
232     /**<p>
233      * Constructs a <code>LinearGradientPaint</code>.
234      *
235      * @param start the gradient axis start <code>Point</code> in user space
236      *
237      * @param end the gradient axis end <code>Point</code> in user space
238      *
239      * @param fractions numbers ranging from 0.0 to 1.0 specifying the 
240      * distribution of colors along the gradient
241      *
242      * @param colors array of colors corresponding to each fractional value
243      *
244      * @param cycleMethod either NO_CYCLE, REFLECT, or REPEAT
245      *
246      * @param colorSpace which colorspace to use for interpolation, 
247      * either SRGB or LINEAR_RGB
248      *
249      * @param gradientTransform transform to apply to the gradient
250      *     
251      * @throws NullPointerException if one of the points is null, 
252      * or gradientTransform is null
253      *
254      * @throws IllegalArgumentException if start and end points are the 
255      * same points, or if fractions.length != colors.length, or if colors 
256      * is less than 2 in size.
257      *
258      */
259     public LinearGradientPaint(Point2D start, Point2D end, float[] fractions,
260                                Color[] colors,
261                                CycleMethodEnum cycleMethod, 
262                                ColorSpaceEnum colorSpace, 
263                                AffineTransform gradientTransform) {
264         super(fractions, colors, cycleMethod, colorSpace, gradientTransform);
265 
266         //
267         // Check input parameters
268         //  
269         if (start == null || end == null) {
270             throw new NullPointerException("Start and end points must be" +
271                                            "non-null");
272         }
273 
274         if (start.equals(end)) {
275             throw new IllegalArgumentException("Start point cannot equal" +
276                                                "endpoint");
277         }
278 
279         //copy the points...
280         this.start = (Point2D)start.clone();
281 
282         this.end = (Point2D)end.clone();
283   
284     }
285     
286     /**
287      * Creates and returns a PaintContext used to generate the color pattern,
288      * for use by the internal rendering engine.
289      *
290      * @param cm {@link ColorModel} that receives
291      * the <code>Paint</code> data. This is used only as a hint.
292      *
293      * @param deviceBounds the device space bounding box of the 
294      * graphics primitive being rendered
295      *
296      * @param userBounds the user space bounding box of the 
297      * graphics primitive being rendered
298      *
299      * @param transform the {@link AffineTransform} from user
300      * space into device space
301      *
302      * @param hints the hints that the context object uses to choose
303      * between rendering alternatives
304      *
305      * @return the {@link PaintContext} that generates color patterns.
306      *
307      * @see PaintContext
308      */
309     public PaintContext createContext(ColorModel cm,
310                                       Rectangle deviceBounds,
311                                       Rectangle2D userBounds,
312                                       AffineTransform transform,
313                                       RenderingHints hints) {
314 
315         // Can't modify the transform passed in...
316         transform = new AffineTransform(transform);
317         //incorporate the gradient transform
318         transform.concatenate(gradientTransform); 
319 
320         try {
321             return new LinearGradientPaintContext(cm, 
322                                                   deviceBounds,
323                                                   userBounds, 
324                                                   transform,
325                                                   hints,
326                                                   start, 
327                                                   end,
328                                                   fractions,
329                                                   this.getColors(),
330                                                   cycleMethod,
331                                                   colorSpace);
332         }
333   
334         catch(NoninvertibleTransformException e) {
335             e.printStackTrace();
336             throw new IllegalArgumentException("transform should be" + 
337                                                "invertible");
338         }
339     }
340     
341     /**
342      * Returns a copy of the start point of the gradient axis
343      * @return a {@link Point2D} object that is a copy of the point
344      * that anchors the first color of this 
345      * <code>LinearGradientPaint</code>.  
346      */
347     public Point2D getStartPoint() {
348         return new Point2D.Double(start.getX(), start.getY());
349     }
350     
351     /** Returns a copy of the end point of the gradient axis
352      * @return a {@link Point2D} object that is a copy of the point
353      * that anchors the last color of this 
354      * <code>LinearGradientPaint</code>.  
355      */
356     public Point2D getEndPoint() {
357         return new Point2D.Double(end.getX(), end.getY());
358     }
359         
360 }
361 
362