001 /*
002 * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 /*
027 * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
028 * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
029 *
030 * The original version of this source code and documentation is
031 * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
032 * of IBM. These materials are provided under terms of a License
033 * Agreement between Taligent and Sun. This technology is protected
034 * by multiple US and International patents.
035 *
036 * This notice and attribution to Taligent may not be removed.
037 * Taligent is a registered trademark of Taligent, Inc.
038 *
039 */
040
041 package java.awt.font;
042
043 import java.awt.Image;
044 import java.awt.Graphics2D;
045 import java.awt.geom.Rectangle2D;
046
047 /**
048 * The <code>ImageGraphicAttribute</code> class is an implementation of
049 * {@link GraphicAttribute} which draws images in
050 * a {@link TextLayout}.
051 * @see GraphicAttribute
052 */
053
054 public final class ImageGraphicAttribute extends GraphicAttribute {
055
056 private Image fImage;
057 private float fImageWidth, fImageHeight;
058 private float fOriginX, fOriginY;
059
060 /**
061 * Constucts an <code>ImageGraphicAttribute</code> from the specified
062 * {@link Image}. The origin is at (0, 0).
063 * @param image the <code>Image</code> rendered by this
064 * <code>ImageGraphicAttribute</code>.
065 * This object keeps a reference to <code>image</code>.
066 * @param alignment one of the alignments from this
067 * <code>ImageGraphicAttribute</code>
068 */
069 public ImageGraphicAttribute(Image image, int alignment) {
070
071 this (image, alignment, 0, 0);
072 }
073
074 /**
075 * Constructs an <code>ImageGraphicAttribute</code> from the specified
076 * <code>Image</code>. The point
077 * (<code>originX</code>, <code>originY</code>) in the
078 * <code>Image</code> appears at the origin of the
079 * <code>ImageGraphicAttribute</code> within the text.
080 * @param image the <code>Image</code> rendered by this
081 * <code>ImageGraphicAttribute</code>.
082 * This object keeps a reference to <code>image</code>.
083 * @param alignment one of the alignments from this
084 * <code>ImageGraphicAttribute</code>
085 * @param originX the X coordinate of the point within
086 * the <code>Image</code> that appears at the origin of the
087 * <code>ImageGraphicAttribute</code> in the text line.
088 * @param originY the Y coordinate of the point within
089 * the <code>Image</code> that appears at the origin of the
090 * <code>ImageGraphicAttribute</code> in the text line.
091 */
092 public ImageGraphicAttribute(Image image, int alignment,
093 float originX, float originY) {
094
095 super (alignment);
096
097 // Can't clone image
098 // fImage = (Image) image.clone();
099 fImage = image;
100
101 fImageWidth = image.getWidth(null);
102 fImageHeight = image.getHeight(null);
103
104 // ensure origin is in Image?
105 fOriginX = originX;
106 fOriginY = originY;
107 }
108
109 /**
110 * Returns the ascent of this <code>ImageGraphicAttribute</code>. The
111 * ascent of an <code>ImageGraphicAttribute</code> is the distance
112 * from the top of the image to the origin.
113 * @return the ascent of this <code>ImageGraphicAttribute</code>.
114 */
115 public float getAscent() {
116
117 return Math.max(0, fOriginY);
118 }
119
120 /**
121 * Returns the descent of this <code>ImageGraphicAttribute</code>.
122 * The descent of an <code>ImageGraphicAttribute</code> is the
123 * distance from the origin to the bottom of the image.
124 * @return the descent of this <code>ImageGraphicAttribute</code>.
125 */
126 public float getDescent() {
127
128 return Math.max(0, fImageHeight - fOriginY);
129 }
130
131 /**
132 * Returns the advance of this <code>ImageGraphicAttribute</code>.
133 * The advance of an <code>ImageGraphicAttribute</code> is the
134 * distance from the origin to the right edge of the image.
135 * @return the advance of this <code>ImageGraphicAttribute</code>.
136 */
137 public float getAdvance() {
138
139 return Math.max(0, fImageWidth - fOriginX);
140 }
141
142 /**
143 * Returns a {@link Rectangle2D} that encloses all of the
144 * bits rendered by this <code>ImageGraphicAttribute</code>, relative
145 * to the rendering position. A graphic can be rendered beyond its
146 * origin, ascent, descent, or advance; but if it is, this
147 * method's implementation must indicate where the graphic is rendered.
148 * @return a <code>Rectangle2D</code> that encloses all of the bits
149 * rendered by this <code>ImageGraphicAttribute</code>.
150 */
151 public Rectangle2D getBounds() {
152
153 return new Rectangle2D.Float(-fOriginX, -fOriginY, fImageWidth,
154 fImageHeight);
155 }
156
157 /**
158 * {@inheritDoc}
159 */
160 public void draw(Graphics2D graphics, float x, float y) {
161
162 graphics.drawImage(fImage, (int) (x - fOriginX),
163 (int) (y - fOriginY), null);
164 }
165
166 /**
167 * Returns a hashcode for this <code>ImageGraphicAttribute</code>.
168 * @return a hash code value for this object.
169 */
170 public int hashCode() {
171
172 return fImage.hashCode();
173 }
174
175 /**
176 * Compares this <code>ImageGraphicAttribute</code> to the specified
177 * {@link Object}.
178 * @param rhs the <code>Object</code> to compare for equality
179 * @return <code>true</code> if this
180 * <code>ImageGraphicAttribute</code> equals <code>rhs</code>;
181 * <code>false</code> otherwise.
182 */
183 public boolean equals(Object rhs) {
184
185 try {
186 return equals((ImageGraphicAttribute) rhs);
187 } catch (ClassCastException e) {
188 return false;
189 }
190 }
191
192 /**
193 * Compares this <code>ImageGraphicAttribute</code> to the specified
194 * <code>ImageGraphicAttribute</code>.
195 * @param rhs the <code>ImageGraphicAttribute</code> to compare for
196 * equality
197 * @return <code>true</code> if this
198 * <code>ImageGraphicAttribute</code> equals <code>rhs</code>;
199 * <code>false</code> otherwise.
200 */
201 public boolean equals(ImageGraphicAttribute rhs) {
202
203 if (rhs == null) {
204 return false;
205 }
206
207 if (this == rhs) {
208 return true;
209 }
210
211 if (fOriginX != rhs.fOriginX || fOriginY != rhs.fOriginY) {
212 return false;
213 }
214
215 if (getAlignment() != rhs.getAlignment()) {
216 return false;
217 }
218
219 if (!fImage.equals(rhs.fImage)) {
220 return false;
221 }
222
223 return true;
224 }
225 }
|