GradientPaint.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* GradientPaint.java --
  2. Copyright (C) 2002, 2005, Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.awt;
  32. import java.awt.geom.AffineTransform;
  33. import java.awt.geom.Point2D;
  34. import java.awt.geom.Rectangle2D;
  35. import java.awt.image.ColorModel;
  36. import gnu.java.awt.GradientPaintContext;
  37. /**
  38. * A paint object that can be used to color a region by blending two colors.
  39. * Instances of this class are immutable.
  40. */
  41. public class GradientPaint implements Paint
  42. {
  43. private final float x1;
  44. private final float y1;
  45. private final Color c1;
  46. private final float x2;
  47. private final float y2;
  48. private final Color c2;
  49. private final boolean cyclic;
  50. /**
  51. * Creates a new acyclic <code>GradientPaint</code>.
  52. *
  53. * @param x1 the x-coordinate of the anchor point for color 1.
  54. * @param y1 the y-coordinate of the anchor point for color 1.
  55. * @param c1 color 1 (<code>null</code> not permitted).
  56. * @param x2 the x-coordinate of the anchor point for color 2.
  57. * @param y2 the y-coordinate of the anchor point for color 2.
  58. * @param c2 the second color (<code>null</code> not permitted).
  59. */
  60. public GradientPaint(float x1, float y1, Color c1,
  61. float x2, float y2, Color c2)
  62. {
  63. this(x1, y1, c1, x2, y2, c2, false);
  64. }
  65. /**
  66. * Creates a new acyclic <code>GradientPaint</code>.
  67. *
  68. * @param p1 anchor point 1 (<code>null</code> not permitted).
  69. * @param c1 color 1 (<code>null</code> not permitted).
  70. * @param p2 anchor point 2 (<code>null</code> not permitted).
  71. * @param c2 color 2 (<code>null</code> not permitted).
  72. */
  73. public GradientPaint(Point2D p1, Color c1, Point2D p2, Color c2)
  74. {
  75. this((float) p1.getX(), (float) p1.getY(), c1,
  76. (float) p2.getX(), (float) p2.getY(), c2, false);
  77. }
  78. /**
  79. * Creates a new cyclic or acyclic <code>GradientPaint</code>.
  80. *
  81. * @param x1 the x-coordinate of the anchor point for color 1.
  82. * @param y1 the y-coordinate of the anchor point for color 1.
  83. * @param c1 color 1 (<code>null</code> not permitted).
  84. * @param x2 the x-coordinate of the anchor point for color 2.
  85. * @param y2 the y-coordinate of the anchor point for color 2.
  86. * @param c2 the second color (<code>null</code> not permitted).
  87. * @param cyclic a flag that controls whether the gradient is cyclic or
  88. * acyclic.
  89. */
  90. public GradientPaint(float x1, float y1, Color c1,
  91. float x2, float y2, Color c2, boolean cyclic)
  92. {
  93. if (c1 == null || c2 == null)
  94. throw new NullPointerException();
  95. this.x1 = x1;
  96. this.y1 = y1;
  97. this.c1 = c1;
  98. this.x2 = x2;
  99. this.y2 = y2;
  100. this.c2 = c2;
  101. this.cyclic = cyclic;
  102. }
  103. /**
  104. * Creates a new cyclic or acyclic <code>GradientPaint</code>.
  105. *
  106. * @param p1 anchor point 1 (<code>null</code> not permitted).
  107. * @param c1 color 1 (<code>null</code> not permitted).
  108. * @param p2 anchor point 2 (<code>null</code> not permitted).
  109. * @param c2 color 2 (<code>null</code> not permitted).
  110. * @param cyclic a flag that controls whether the gradient is cyclic or
  111. * acyclic.
  112. */
  113. public GradientPaint(Point2D p1, Color c1, Point2D p2, Color c2,
  114. boolean cyclic)
  115. {
  116. this((float) p1.getX(), (float) p1.getY(), c1,
  117. (float) p2.getX(), (float) p2.getY(), c2, cyclic);
  118. }
  119. /**
  120. * Returns a point with the same coordinates as the anchor point for color 1.
  121. * Note that if you modify this point, the <code>GradientPaint</code> remains
  122. * unchanged.
  123. *
  124. * @return A point with the same coordinates as the anchor point for color 1.
  125. */
  126. public Point2D getPoint1()
  127. {
  128. return new Point2D.Float(x1, y1);
  129. }
  130. /**
  131. * Returns the first color.
  132. *
  133. * @return The color (never <code>null</code>).
  134. */
  135. public Color getColor1()
  136. {
  137. return c1;
  138. }
  139. /**
  140. * Returns a point with the same coordinates as the anchor point for color 2.
  141. * Note that if you modify this point, the <code>GradientPaint</code> remains
  142. * unchanged.
  143. *
  144. * @return A point with the same coordinates as the anchor point for color 2.
  145. */
  146. public Point2D getPoint2()
  147. {
  148. return new Point2D.Float(x2, y2);
  149. }
  150. /**
  151. * Returns the second color.
  152. *
  153. * @return The color (never <code>null</code>).
  154. */
  155. public Color getColor2()
  156. {
  157. return c2;
  158. }
  159. /**
  160. * Returns <code>true</code> if this <code>GradientPaint</code> instance is
  161. * cyclic, and <code>false</code> otherwise.
  162. *
  163. * @return A boolean.
  164. */
  165. public boolean isCyclic()
  166. {
  167. return cyclic;
  168. }
  169. /**
  170. * Returns the {@link PaintContext} used to generate the color pattern.
  171. *
  172. * @param cm the color model, used as a hint (ignored in this
  173. * implementation).
  174. * @param deviceBounds the device space bounding box of the painted area.
  175. * @param userBounds the user space bounding box of the painted area.
  176. * @param xform the transformation from user space to device space.
  177. * @param hints any hints for choosing between rendering alternatives.
  178. *
  179. * @return The context for performing the paint
  180. */
  181. public PaintContext createContext(ColorModel cm, Rectangle deviceBounds,
  182. Rectangle2D userBounds,
  183. AffineTransform xform,
  184. RenderingHints hints)
  185. {
  186. Point2D xp1 = xform.transform(getPoint1(), null);
  187. Point2D xp2 = xform.transform(getPoint2(), null);
  188. return new GradientPaintContext((float) xp1.getX(), (float) xp1.getY(), c1,
  189. (float) xp2.getX(), (float) xp2.getY(), c2, cyclic);
  190. }
  191. /**
  192. * Returns the transparency code for this <code>GradientPaint</code> instance.
  193. * This is derived from the two {@link Color} objects used in creating this
  194. * object: if both colors are opaque, this method returns
  195. * {@link Transparency#OPAQUE}, otherwise it returns
  196. * {@link Transparency#TRANSLUCENT}.
  197. *
  198. * @return {@link Transparency#OPAQUE} or {@link Transparency#TRANSLUCENT}.
  199. */
  200. public int getTransparency()
  201. {
  202. if (c1.getAlpha() == 255 && c2.getAlpha() == 255)
  203. return Transparency.OPAQUE;
  204. else
  205. return Transparency.TRANSLUCENT;
  206. }
  207. } // class GradientPaint