1. /*
  2. * @(#)GradientPaint.java 1.39 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.awt.geom.Point2D;
  9. import java.awt.geom.Rectangle2D;
  10. import java.awt.geom.AffineTransform;
  11. import java.awt.image.ColorModel;
  12. /**
  13. * The <code>GradientPaint</code> class provides a way to fill
  14. * a {@link Shape} with a linear color gradient pattern.
  15. * If {@link Point} P1 with {@link Color} C1 and <code>Point</code> P2 with
  16. * <code>Color</code> C2 are specified in user space, the
  17. * <code>Color</code> on the P1, P2 connecting line is proportionally
  18. * changed from C1 to C2. Any point P not on the extended P1, P2
  19. * connecting line has the color of the point P' that is the perpendicular
  20. * projection of P on the extended P1, P2 connecting line.
  21. * Points on the extended line outside of the P1, P2 segment can be colored
  22. * in one of two ways.
  23. * <ul>
  24. * <li>
  25. * If the gradient is cyclic then the points on the extended P1, P2
  26. * connecting line cycle back and forth between the colors C1 and C2.
  27. * <li>
  28. * If the gradient is acyclic then points on the P1 side of the segment
  29. * have the constant <code>Color</code> C1 while points on the P2 side
  30. * have the constant <code>Color</code> C2.
  31. * </ul>
  32. *
  33. * @see Paint
  34. * @see Graphics2D#setPaint
  35. * @version 10 Feb 1997
  36. */
  37. public class GradientPaint implements Paint {
  38. Point2D.Float p1;
  39. Point2D.Float p2;
  40. Color color1;
  41. Color color2;
  42. boolean cyclic;
  43. /**
  44. * Constructs a simple acyclic <code>GradientPaint</code> object.
  45. * @param x1 x coordinate of the first specified
  46. * <code>Point</code> in user space
  47. * @param y1 y coordinate of the first specified
  48. * <code>Point</code> in user space
  49. * @param color1 <code>Color</code> at the first specified
  50. * <code>Point</code>
  51. * @param x2 x coordinate of the second specified
  52. * <code>Point</code> in user space
  53. * @param y2 y coordinate of the second specified
  54. * <code>Point</code> in user space
  55. * @param color2 <code>Color</code> at the second specified
  56. * <code>Point</code>
  57. * @throws NullPointerException if either one of colors is null
  58. */
  59. public GradientPaint(float x1,
  60. float y1,
  61. Color color1,
  62. float x2,
  63. float y2,
  64. Color color2) {
  65. if ((color1 == null) || (color2 == null)) {
  66. throw new NullPointerException("Colors cannot be null");
  67. }
  68. p1 = new Point2D.Float(x1, y1);
  69. p2 = new Point2D.Float(x2, y2);
  70. this.color1 = color1;
  71. this.color2 = color2;
  72. }
  73. /**
  74. * Constructs a simple acyclic <code>GradientPaint</code> object.
  75. * @param pt1 the first specified <code>Point</code> in user space
  76. * @param color1 <code>Color</code> at the first specified
  77. * <code>Point</code>
  78. * @param pt2 the second specified <code>Point</code> in user space
  79. * @param color2 <code>Color</code> at the second specified
  80. * <code>Point</code>
  81. * @throws NullPointerException if either one of colors or points
  82. * is null
  83. */
  84. public GradientPaint(Point2D pt1,
  85. Color color1,
  86. Point2D pt2,
  87. Color color2) {
  88. if ((color1 == null) || (color2 == null) ||
  89. (pt1 == null) || (pt2 == null)) {
  90. throw new NullPointerException("Colors and points should be non-null");
  91. }
  92. p1 = new Point2D.Float((float)pt1.getX(), (float)pt1.getY());
  93. p2 = new Point2D.Float((float)pt2.getX(), (float)pt2.getY());
  94. this.color1 = color1;
  95. this.color2 = color2;
  96. }
  97. /**
  98. * Constructs either a cyclic or acyclic <code>GradientPaint</code>
  99. * object depending on the <code>boolean</code> parameter.
  100. * @param x1 x coordinate of the first specified
  101. * <code>Point</code> in user space
  102. * @param y1 y coordinate of the first specified
  103. * <code>Point</code> in user space
  104. * @param color1 <code>Color</code> at the first specified
  105. * <code>Point</code>
  106. * @param x2 x coordinate of the second specified
  107. * <code>Point</code> in user space
  108. * @param y2 y coordinate of the second specified
  109. * <code>Point</code> in user space
  110. * @param color2 <code>Color</code> at the second specified
  111. * <code>Point</code>
  112. * @param cyclic <code>true</code> if the gradient pattern should cycle
  113. * repeatedly between the two colors; <code>false</code> otherwise
  114. */
  115. public GradientPaint(float x1,
  116. float y1,
  117. Color color1,
  118. float x2,
  119. float y2,
  120. Color color2,
  121. boolean cyclic) {
  122. this (x1, y1, color1, x2, y2, color2);
  123. this.cyclic = cyclic;
  124. }
  125. /**
  126. * Constructs either a cyclic or acyclic <code>GradientPaint</code>
  127. * object depending on the <code>boolean</code> parameter.
  128. * @param pt1 the first specified <code>Point</code>
  129. * in user space
  130. * @param color1 <code>Color</code> at the first specified
  131. * <code>Point</code>
  132. * @param pt2 the second specified <code>Point</code>
  133. * in user space
  134. * @param color2 <code>Color</code> at the second specified
  135. * <code>Point</code>
  136. * @param cyclic <code>true</code> if the gradient pattern should cycle
  137. * repeatedly between the two colors; <code>false</code> otherwise
  138. * @throws NullPointerException if either one of colors or points
  139. * is null
  140. */
  141. public GradientPaint(Point2D pt1,
  142. Color color1,
  143. Point2D pt2,
  144. Color color2,
  145. boolean cyclic) {
  146. this (pt1, color1, pt2, color2);
  147. this.cyclic = cyclic;
  148. }
  149. /**
  150. * Returns a copy of the point P1 that anchors the first color.
  151. * @return a {@link Point2D} object that is a copy of the point
  152. * that anchors the first color of this
  153. * <code>GradientPaint</code>.
  154. */
  155. public Point2D getPoint1() {
  156. return new Point2D.Float(p1.x, p1.y);
  157. }
  158. /**
  159. * Returns the color C1 anchored by the point P1.
  160. * @return a <code>Color</code> object that is the color
  161. * anchored by P1.
  162. */
  163. public Color getColor1() {
  164. return color1;
  165. }
  166. /**
  167. * Returns a copy of the point P2 which anchors the second color.
  168. * @return a {@link Point2D} object that is a copy of the point
  169. * that anchors the second color of this
  170. * <code>GradientPaint</code>.
  171. */
  172. public Point2D getPoint2() {
  173. return new Point2D.Float(p2.x, p2.y);
  174. }
  175. /**
  176. * Returns the color C2 anchored by the point P2.
  177. * @return a <code>Color</code> object that is the color
  178. * anchored by P2.
  179. */
  180. public Color getColor2() {
  181. return color2;
  182. }
  183. /**
  184. * Returns <code>true</code> if the gradient cycles repeatedly
  185. * between the two colors C1 and C2.
  186. * @return <code>true</code> if the gradient cycles repeatedly
  187. * between the two colors; <code>false</code> otherwise.
  188. */
  189. public boolean isCyclic() {
  190. return cyclic;
  191. }
  192. /**
  193. * Creates and returns a context used to generate the color pattern.
  194. * @param cm {@link ColorModel} that receives
  195. * the <code>Paint</code> data. This is used only as a hint.
  196. * @param deviceBounds the device space bounding box of the
  197. * graphics primitive being rendered
  198. * @param userBounds the user space bounding box of the
  199. * graphics primitive being rendered
  200. * @param xform the {@link AffineTransform} from user
  201. * space into device space
  202. * @param hints the hints that the context object uses to choose
  203. * between rendering alternatives
  204. * @return the {@link PaintContext} that generates color patterns.
  205. * @see PaintContext
  206. */
  207. public PaintContext createContext(ColorModel cm,
  208. Rectangle deviceBounds,
  209. Rectangle2D userBounds,
  210. AffineTransform xform,
  211. RenderingHints hints) {
  212. return new GradientPaintContext(cm, p1, p2, xform,
  213. color1, color2, cyclic);
  214. }
  215. /**
  216. * Returns the transparency mode for this <code>GradientPaint</code>.
  217. * @return an integer value representing this <code>GradientPaint</code>
  218. * object's transparency mode.
  219. * @see Transparency
  220. */
  221. public int getTransparency() {
  222. int a1 = color1.getAlpha();
  223. int a2 = color2.getAlpha();
  224. return (((a1 & a2) == 0xff) ? OPAQUE : TRANSLUCENT);
  225. }
  226. }