1 /* GradientPaint.java --
2 Copyright (C) 2002, 2005, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.awt.geom.AffineTransform;
42 import java.awt.geom.Point2D;
43 import java.awt.geom.Rectangle2D;
44 import java.awt.image.ColorModel;
45 import gnu.java.awt.GradientPaintContext;
48 * A paint object that can be used to color a region by blending two colors.
49 * Instances of this class are immutable.
51 public class GradientPaint implements Paint
53 private final float x1;
54 private final float y1;
55 private final Color c1;
56 private final float x2;
57 private final float y2;
58 private final Color c2;
59 private final boolean cyclic;
62 * Creates a new acyclic <code>GradientPaint</code>.
64 * @param x1 the x-coordinate of the anchor point for color 1.
65 * @param y1 the y-coordinate of the anchor point for color 1.
66 * @param c1 color 1 (<code>null</code> not permitted).
67 * @param x2 the x-coordinate of the anchor point for color 2.
68 * @param y2 the y-coordinate of the anchor point for color 2.
69 * @param c2 the second color (<code>null</code> not permitted).
71 public GradientPaint(float x1, float y1, Color c1,
72 float x2, float y2, Color c2)
74 this(x1, y1, c1, x2, y2, c2, false);
78 * Creates a new acyclic <code>GradientPaint</code>.
80 * @param p1 anchor point 1 (<code>null</code> not permitted).
81 * @param c1 color 1 (<code>null</code> not permitted).
82 * @param p2 anchor point 2 (<code>null</code> not permitted).
83 * @param c2 color 2 (<code>null</code> not permitted).
85 public GradientPaint(Point2D p1, Color c1, Point2D p2, Color c2)
87 this((float) p1.getX(), (float) p1.getY(), c1,
88 (float) p2.getX(), (float) p2.getY(), c2, false);
92 * Creates a new cyclic or acyclic <code>GradientPaint</code>.
94 * @param x1 the x-coordinate of the anchor point for color 1.
95 * @param y1 the y-coordinate of the anchor point for color 1.
96 * @param c1 color 1 (<code>null</code> not permitted).
97 * @param x2 the x-coordinate of the anchor point for color 2.
98 * @param y2 the y-coordinate of the anchor point for color 2.
99 * @param c2 the second color (<code>null</code> not permitted).
100 * @param cyclic a flag that controls whether the gradient is cyclic or
103 public GradientPaint(float x1, float y1, Color c1,
104 float x2, float y2, Color c2, boolean cyclic)
106 if (c1 == null || c2 == null)
107 throw new NullPointerException();
114 this.cyclic = cyclic;
118 * Creates a new cyclic or acyclic <code>GradientPaint</code>.
120 * @param p1 anchor point 1 (<code>null</code> not permitted).
121 * @param c1 color 1 (<code>null</code> not permitted).
122 * @param p2 anchor point 2 (<code>null</code> not permitted).
123 * @param c2 color 2 (<code>null</code> not permitted).
124 * @param cyclic a flag that controls whether the gradient is cyclic or
127 public GradientPaint(Point2D p1, Color c1, Point2D p2, Color c2,
130 this((float) p1.getX(), (float) p1.getY(), c1,
131 (float) p2.getX(), (float) p2.getY(), c2, cyclic);
135 * Returns a point with the same coordinates as the anchor point for color 1.
136 * Note that if you modify this point, the <code>GradientPaint</code> remains
139 * @return A point with the same coordinates as the anchor point for color 1.
141 public Point2D getPoint1()
143 return new Point2D.Float(x1, y1);
147 * Returns the first color.
149 * @return The color (never <code>null</code>).
151 public Color getColor1()
157 * Returns a point with the same coordinates as the anchor point for color 2.
158 * Note that if you modify this point, the <code>GradientPaint</code> remains
161 * @return A point with the same coordinates as the anchor point for color 2.
163 public Point2D getPoint2()
165 return new Point2D.Float(x2, y2);
169 * Returns the second color.
171 * @return The color (never <code>null</code>).
173 public Color getColor2()
179 * Returns <code>true</code> if this <code>GradientPaint</code> instance is
180 * cyclic, and <code>false</code> otherwise.
184 public boolean isCyclic()
190 * Returns the {@link PaintContext} used to generate the color pattern.
192 * @param cm the color model, used as a hint (ignored in this
194 * @param deviceBounds the device space bounding box of the painted area.
195 * @param userBounds the user space bounding box of the painted area.
196 * @param xform the transformation from user space to device space.
197 * @param hints any hints for choosing between rendering alternatives.
199 * @return The context for performing the paint
201 public PaintContext createContext(ColorModel cm, Rectangle deviceBounds,
202 Rectangle2D userBounds,
203 AffineTransform xform,
204 RenderingHints hints)
206 Point2D xp1 = xform.transform(getPoint1(), null);
207 Point2D xp2 = xform.transform(getPoint2(), null);
208 return new GradientPaintContext((float) xp1.getX(), (float) xp1.getY(), c1,
209 (float) xp2.getX(), (float) xp2.getY(), c2, cyclic);
213 * Returns the transparency code for this <code>GradientPaint</code> instance.
214 * This is derived from the two {@link Color} objects used in creating this
215 * object: if both colors are opaque, this method returns
216 * {@link Transparency#OPAQUE}, otherwise it returns
217 * {@link Transparency#TRANSLUCENT}.
219 * @return {@link Transparency#OPAQUE} or {@link Transparency#TRANSLUCENT}.
221 public int getTransparency()
223 if (c1.getAlpha() == 255 && c2.getAlpha() == 255)
224 return Transparency.OPAQUE;
226 return Transparency.TRANSLUCENT;
229 } // class GradientPaint