1. /*
  2. * @(#)ImageGraphicAttribute.java 1.15 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
  9. * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
  10. *
  11. * The original version of this source code and documentation is
  12. * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
  13. * of IBM. These materials are provided under terms of a License
  14. * Agreement between Taligent and Sun. This technology is protected
  15. * by multiple US and International patents.
  16. *
  17. * This notice and attribution to Taligent may not be removed.
  18. * Taligent is a registered trademark of Taligent, Inc.
  19. *
  20. */
  21. package java.awt.font;
  22. import java.awt.Image;
  23. import java.awt.Graphics2D;
  24. import java.awt.geom.Rectangle2D;
  25. /**
  26. * The <code>ImageGraphicAttribute</code> class is an implementation of
  27. * {@link GraphicAttribute} which draws images in
  28. * a {@link TextLayout}.
  29. * @see GraphicAttribute
  30. */
  31. public final class ImageGraphicAttribute extends GraphicAttribute {
  32. private Image fImage;
  33. private float fImageWidth, fImageHeight;
  34. private float fOriginX, fOriginY;
  35. /**
  36. * Constucts an <code>ImageGraphicAttribute</code> from the specified
  37. * {@link Image}. The origin is at (0, 0).
  38. * @param image the <code>Image</code> rendered by this
  39. * <code>ImageGraphicAttribute</code>.
  40. * This object keeps a reference to <code>image</code>.
  41. * @param alignment one of the alignments from this
  42. * <code>ImageGraphicAttribute</code>
  43. */
  44. public ImageGraphicAttribute(Image image, int alignment) {
  45. this(image, alignment, 0, 0);
  46. }
  47. /**
  48. * Constructs an <code>ImageGraphicAttribute</code> from the specified
  49. * <code>Image</code>. The point
  50. * (<code>originX</code>, <code>originY</code>) in the
  51. * <code>Image</code> appears at the origin of the
  52. * <code>ImageGraphicAttribute</code> within the text.
  53. * @param image the <code>Image</code> rendered by this
  54. * <code>ImageGraphicAttribute</code>.
  55. * This object keeps a reference to <code>image</code>.
  56. * @param alignment one of the alignments from this
  57. * <code>ImageGraphicAttribute</code>
  58. * @param originX, originY the coordinates of the point within
  59. * the <code>Image</code> that appears at the origin of the
  60. * <code>ImageGraphicAttribute</code> in the text line.
  61. */
  62. public ImageGraphicAttribute(Image image,
  63. int alignment,
  64. float originX,
  65. float originY) {
  66. super(alignment);
  67. // Can't clone image
  68. // fImage = (Image) image.clone();
  69. fImage = image;
  70. fImageWidth = image.getWidth(null);
  71. fImageHeight = image.getHeight(null);
  72. // ensure origin is in Image?
  73. fOriginX = originX;
  74. fOriginY = originY;
  75. }
  76. /**
  77. * Returns the ascent of this <code>ImageGraphicAttribute</code>. The
  78. * ascent of an <code>ImageGraphicAttribute</code> is the distance
  79. * from the top of the image to the origin.
  80. * @return the ascent of this <code>ImageGraphicAttribute</code>.
  81. */
  82. public float getAscent() {
  83. return Math.max(0, fOriginY);
  84. }
  85. /**
  86. * Returns the descent of this <code>ImageGraphicAttribute</code>.
  87. * The descent of an <code>ImageGraphicAttribute</code> is the
  88. * distance from the origin to the bottom of the image.
  89. * @return the descent of this <code>ImageGraphicAttribute</code>.
  90. */
  91. public float getDescent() {
  92. return Math.max(0, fImageHeight-fOriginY);
  93. }
  94. /**
  95. * Returns the advance of this <code>ImageGraphicAttribute</code>.
  96. * The advance of an <code>ImageGraphicAttribute</code> is the
  97. * distance from the origin to the right edge of the image.
  98. * @return the advance of this <code>ImageGraphicAttribute</code>.
  99. */
  100. public float getAdvance() {
  101. return Math.max(0, fImageWidth-fOriginX);
  102. }
  103. /**
  104. * Returns a {@link Rectangle2D} that encloses all of the
  105. * bits rendered by this <code>ImageGraphicAttribute</code>, relative
  106. * to the rendering position. A graphic can be rendered beyond its
  107. * origin, ascent, descent, or advance; but if it is, this
  108. * method's implementation must indicate where the graphic is rendered.
  109. * @return a <code>Rectangle2D</code> that encloses all of the bits
  110. * rendered by this <code>ImageGraphicAttribute</code>.
  111. */
  112. public Rectangle2D getBounds() {
  113. return new Rectangle2D.Float(
  114. -fOriginX, -fOriginY, fImageWidth, fImageHeight);
  115. }
  116. /**
  117. * Renders the graphic at the specified location.
  118. * @param graphics the {@link Graphics2D} into which to render the
  119. * graphic
  120. * @param x, y the user-space coordinates where the graphic is
  121. * rendered
  122. */
  123. public void draw(Graphics2D graphics, float x, float y) {
  124. graphics.drawImage(fImage, (int) (x-fOriginX), (int) (y-fOriginY), null);
  125. }
  126. /**
  127. * Returns a hashcode for this <code>ImageGraphicAttribute</code>.
  128. * @return a hash code value for this object.
  129. */
  130. public int hashCode() {
  131. return fImage.hashCode();
  132. }
  133. /**
  134. * Compares this <code>ImageGraphicAttribute</code> to the specified
  135. * {@link Object}.
  136. * @param rhs the <code>Object</code> to compare for equality
  137. * @return <code>true</code> if this
  138. * <code>ImageGraphicAttribute</code> equals <code>rhs</code>
  139. * <code>false</code> otherwise.
  140. */
  141. public boolean equals(Object rhs) {
  142. try {
  143. return equals((ImageGraphicAttribute) rhs);
  144. }
  145. catch(ClassCastException e) {
  146. return false;
  147. }
  148. }
  149. /**
  150. * Compares this <code>ImageGraphicAttribute</code> to the specified
  151. * <code>ImageGraphicAttribute</code>.
  152. * @param rhs the <code>ImageGraphicAttribute</code> to compare for
  153. * equality
  154. * @return <code>true</code> if this
  155. * <code>ImageGraphicAttribute</code> equals <code>rhs</code>
  156. * <code>false</code> otherwise.
  157. */
  158. public boolean equals(ImageGraphicAttribute rhs) {
  159. if (rhs == null) {
  160. return false;
  161. }
  162. if (this == rhs) {
  163. return true;
  164. }
  165. if (fOriginX != rhs.fOriginX || fOriginY != rhs.fOriginY) {
  166. return false;
  167. }
  168. if (getAlignment() != rhs.getAlignment()) {
  169. return false;
  170. }
  171. if (!fImage.equals(rhs.fImage)) {
  172. return false;
  173. }
  174. return true;
  175. }
  176. }