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