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