1. /*
  2. * @(#)GradientPaint.java 1.31 01/11/29
  3. *
  4. * Copyright 2002 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, y1 coordinates of the first specified
  46. * <code>Point</code> in user space
  47. * @param color1 <code>Color</code> at the first specified
  48. * <code>Point</code>
  49. * @param x2, y2 coordinates of the second specified
  50. * <code>Point</code> in user space
  51. * @param color2 <code>Color</code> at the second specified
  52. * <code>Point</code>
  53. */
  54. public GradientPaint(float x1,
  55. float y1,
  56. Color color1,
  57. float x2,
  58. float y2,
  59. Color color2) {
  60. p1 = new Point2D.Float(x1, y1);
  61. p2 = new Point2D.Float(x2, y2);
  62. this.color1 = color1;
  63. this.color2 = color2;
  64. }
  65. /**
  66. * Constructs a simple acyclic <code>GradientPaint</code> object.
  67. * @param pt1 the first specified <code>Point</code> in user space
  68. * @param color1 <code>Color</code> at the first specified
  69. * <code>Point</code>
  70. * @param pt2 the second specified <code>Point</code> in user space
  71. * @param color2 <code>Color</code> at the second specified
  72. * <code>Point</code>
  73. */
  74. public GradientPaint(Point2D pt1,
  75. Color color1,
  76. Point2D pt2,
  77. Color color2) {
  78. p1 = new Point2D.Float((float)pt1.getX(), (float)pt1.getY());
  79. p2 = new Point2D.Float((float)pt2.getX(), (float)pt2.getY());
  80. this.color1 = color1;
  81. this.color2 = color2;
  82. }
  83. /**
  84. * Constructs either a cyclic or acyclic <code>GradientPaint</code>
  85. * object depending on the <code>boolean</code> parameter.
  86. * @param x1, y1 coordinates of the first specified
  87. * <code>Point</code> in user space
  88. * @param color1 <code>Color</code> at the first specified
  89. * <code>Point</code>
  90. * @param x2, y2 coordinates of the second specified
  91. * <code>Point</code> in user space
  92. * @param color2 <code>Color</code> at the second specified
  93. * <code>Point</code>
  94. * @param cyclic <code>true</code> if the gradient pattern should cycle
  95. * repeatedly between the two colors; <code>false</code> otherwise
  96. */
  97. public GradientPaint(float x1,
  98. float y1,
  99. Color color1,
  100. float x2,
  101. float y2,
  102. Color color2,
  103. boolean cyclic) {
  104. this (x1, y1, color1, x2, y2, color2);
  105. this.cyclic = cyclic;
  106. }
  107. /**
  108. * Constructs either a cyclic or acyclic <code>GradientPaint</code>
  109. * object depending on the <code>boolean</code> parameter.
  110. * @param pt1 the first specified <code>Point</code>
  111. * in user space
  112. * @param color1 <code>Color</code> at the first specified
  113. * <code>Point</code>
  114. * @param pt2 the second specified <code>Point</code>
  115. * in user space
  116. * @param color2 <code>Color</code> at the second specified
  117. * <code>Point</code>
  118. * @param cyclic <code>true</code> if the gradient pattern should cycle
  119. * repeatedly between the two colors; <code>false</code> otherwise
  120. */
  121. public GradientPaint(Point2D pt1,
  122. Color color1,
  123. Point2D pt2,
  124. Color color2,
  125. boolean cyclic) {
  126. this (pt1, color1, pt2, color2);
  127. this.cyclic = cyclic;
  128. }
  129. /**
  130. * Returns a copy of the point P1 that anchors the first color.
  131. * @return a {@link Point2D} object that is a copy of the point
  132. * that anchors the first color of this
  133. * <code>GradientPaint</code>.
  134. */
  135. public Point2D getPoint1() {
  136. return new Point2D.Float(p1.x, p1.y);
  137. }
  138. /**
  139. * Returns the color C1 anchored by the point P1.
  140. * @return a <code>Color</code> object that is the color
  141. * anchored by P1.
  142. */
  143. public Color getColor1() {
  144. return color1;
  145. }
  146. /**
  147. * Returns a copy of the point P2 which anchors the second color.
  148. * @return a {@link Point2D} object that is a copy of the point
  149. * that anchors the second color of this
  150. * <code>GradientPaint</code>.
  151. */
  152. public Point2D getPoint2() {
  153. return new Point2D.Float(p2.x, p2.y);
  154. }
  155. /**
  156. * Returns the color C2 anchored by the point P2.
  157. * @return a <code>Color</code> object that is the color
  158. * anchored by P2.
  159. */
  160. public Color getColor2() {
  161. return color2;
  162. }
  163. /**
  164. * Returns <code>true</code> if the gradient cycles repeatedly
  165. * between the two colors C1 and C2.
  166. * @return <code>true</code> if the gradient cycles repeatedly
  167. * between the two colors; <code>false</code> otherwise.
  168. */
  169. public boolean isCyclic() {
  170. return cyclic;
  171. }
  172. /**
  173. * Creates and returns a context used to generate the color pattern.
  174. * @param cm {@link ColorModel} that receives
  175. * the <code>Paint</code> data. This is used only as a hint.
  176. * @param deviceBounds the device space bounding box of the
  177. * graphics primitive being rendered
  178. * @param userBounds the user space bounding box of the
  179. * graphics primitive being rendered
  180. * @param xform the {@link AffineTransform} from user
  181. * space into device space
  182. * @param hints the hints that the context object uses to choose
  183. * between rendering alternatives
  184. * @return the {@link PaintContext} that generates color patterns.
  185. * @see PaintContext
  186. */
  187. public PaintContext createContext(ColorModel cm,
  188. Rectangle deviceBounds,
  189. Rectangle2D userBounds,
  190. AffineTransform xform,
  191. RenderingHints hints) {
  192. return new GradientPaintContext(p1, p2, xform, color1, color2, cyclic);
  193. }
  194. /**
  195. * Returns the transparency mode for this <code>GradientPaint</code>.
  196. * @return an integer value representing this <code>GradientPaint</code>
  197. * object's transparency mode.
  198. * @see Transparency
  199. */
  200. public int getTransparency() {
  201. int a1 = color1.getAlpha();
  202. int a2 = color2.getAlpha();
  203. return (((a1 & a2) == 0xff) ? OPAQUE : TRANSLUCENT);
  204. }
  205. }