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