1. /*
  2. * @(#)TexturePaint.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.Rectangle2D;
  9. import java.awt.geom.AffineTransform;
  10. import java.awt.image.AffineTransformOp;
  11. import java.awt.image.BufferedImage;
  12. import java.awt.image.ColorModel;
  13. /**
  14. * The <code>TexturePaint</code> class provides a way to fill a
  15. * {@link Shape} with a texture that is specified as
  16. * a {@link BufferedImage}. The size of the <code>BufferedImage</code>
  17. * object should be small because the <code>BufferedImage</code> data
  18. * is copied by the <code>TexturePaint</code> object.
  19. * At construction time, the texture is anchored to the upper
  20. * left corner of a {@link Rectangle2D} that is
  21. * specified in user space. Texture is computed for
  22. * locations in the device space by conceptually replicating the
  23. * specified <code>Rectangle2D</code> infinitely in all directions
  24. * in user space and mapping the <code>BufferedImage</code> to each
  25. * replicated <code>Rectangle2D</code>.
  26. * @see Paint
  27. * @see Graphics2D#setPaint
  28. * @version 1.39, 12/19/03
  29. */
  30. public class TexturePaint implements Paint {
  31. BufferedImage bufImg;
  32. double tx;
  33. double ty;
  34. double sx;
  35. double sy;
  36. /**
  37. * Constructs a <code>TexturePaint</code> object.
  38. * @param txtr the <code>BufferedImage</code> object with the texture
  39. * used for painting
  40. * @param anchor the <code>Rectangle2D</code> in user space used to
  41. * anchor and replicate the texture
  42. */
  43. public TexturePaint(BufferedImage txtr,
  44. Rectangle2D anchor) {
  45. this.bufImg = txtr;
  46. this.tx = anchor.getX();
  47. this.ty = anchor.getY();
  48. this.sx = anchor.getWidth() / bufImg.getWidth();
  49. this.sy = anchor.getHeight() / bufImg.getHeight();
  50. }
  51. /**
  52. * Returns the <code>BufferedImage</code> texture used to
  53. * fill the shapes.
  54. * @return a <code>BufferedImage</code>.
  55. */
  56. public BufferedImage getImage() {
  57. return bufImg;
  58. }
  59. /**
  60. * Returns a copy of the anchor rectangle which positions and
  61. * sizes the textured image.
  62. * @return the <code>Rectangle2D</code> used to anchor and
  63. * size this <code>TexturePaint</code>.
  64. */
  65. public Rectangle2D getAnchorRect() {
  66. return new Rectangle2D.Double(tx, ty,
  67. sx * bufImg.getWidth(),
  68. sy * bufImg.getHeight());
  69. }
  70. /**
  71. * Creates and returns a context used to generate the color pattern.
  72. * @param cm the {@link ColorModel} that receives the
  73. * <code>Paint</code> data. This is used only as a hint.
  74. * @param deviceBounds the device space bounding box of the graphics
  75. * primitive being rendered
  76. * @param userBounds the user space bounding box of the graphics
  77. * primitive being rendered
  78. * @param xform the {@link AffineTransform} from user space
  79. * into device space
  80. * @param hints a {@link RenderingHints} object that can be used to
  81. * specify how the pattern is ultimately rendered
  82. * @return the {@link PaintContext} used for generating color
  83. * patterns.
  84. * @see PaintContext
  85. */
  86. public PaintContext createContext(ColorModel cm,
  87. Rectangle deviceBounds,
  88. Rectangle2D userBounds,
  89. AffineTransform xform,
  90. RenderingHints hints) {
  91. if (xform == null) {
  92. xform = new AffineTransform();
  93. } else {
  94. xform = (AffineTransform) xform.clone();
  95. }
  96. xform.translate(tx, ty);
  97. xform.scale(sx, sy);
  98. return TexturePaintContext.getContext(bufImg, xform, hints,
  99. deviceBounds);
  100. }
  101. /**
  102. * Returns the transparency mode for this <code>TexturePaint</code>.
  103. * @return the transparency mode for this <code>TexturePaint</code>
  104. * as an integer value.
  105. * @see Transparency
  106. */
  107. public int getTransparency() {
  108. return (bufImg.getColorModel()).getTransparency();
  109. }
  110. }