KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > font > ImageGraphicAttribute


1 /*
2  * @(#)ImageGraphicAttribute.java 1.16 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
8 /*
9  * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
10  * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
11  *
12  * The original version of this source code and documentation is
13  * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
14  * of IBM. These materials are provided under terms of a License
15  * Agreement between Taligent and Sun. This technology is protected
16  * by multiple US and International patents.
17  *
18  * This notice and attribution to Taligent may not be removed.
19  * Taligent is a registered trademark of Taligent, Inc.
20  *
21  */

22
23 package java.awt.font;
24
25 import java.awt.Image JavaDoc;
26 import java.awt.Graphics2D JavaDoc;
27 import java.awt.geom.Rectangle2D JavaDoc;
28
29 /**
30  * The <code>ImageGraphicAttribute</code> class is an implementation of
31  * {@link GraphicAttribute} which draws images in
32  * a {@link TextLayout}.
33  * @see GraphicAttribute
34  */

35
36 public final class ImageGraphicAttribute extends GraphicAttribute JavaDoc {
37
38     private Image JavaDoc fImage;
39     private float fImageWidth, fImageHeight;
40     private float fOriginX, fOriginY;
41
42     /**
43      * Constucts an <code>ImageGraphicAttribute</code> from the specified
44      * {@link Image}. The origin is at (0,&nbsp;0).
45      * @param image the <code>Image</code> rendered by this
46      * <code>ImageGraphicAttribute</code>.
47      * This object keeps a reference to <code>image</code>.
48      * @param alignment one of the alignments from this
49      * <code>ImageGraphicAttribute</code>
50      */

51     public ImageGraphicAttribute(Image JavaDoc image, int alignment) {
52
53         this(image, alignment, 0, 0);
54     }
55
56     /**
57      * Constructs an <code>ImageGraphicAttribute</code> from the specified
58      * <code>Image</code>. The point
59      * (<code>originX</code>,&nbsp;<code>originY</code>) in the
60      * <code>Image</code> appears at the origin of the
61      * <code>ImageGraphicAttribute</code> within the text.
62      * @param image the <code>Image</code> rendered by this
63      * <code>ImageGraphicAttribute</code>.
64      * This object keeps a reference to <code>image</code>.
65      * @param alignment one of the alignments from this
66      * <code>ImageGraphicAttribute</code>
67      * @param originX,&nbsp;originY the coordinates of the point within
68      * the <code>Image</code> that appears at the origin of the
69      * <code>ImageGraphicAttribute</code> in the text line.
70      */

71     public ImageGraphicAttribute(Image JavaDoc image,
72                                  int alignment,
73                                  float originX,
74                                  float originY) {
75
76         super(alignment);
77
78         // Can't clone image
79
// fImage = (Image) image.clone();
80
fImage = image;
81
82         fImageWidth = image.getWidth(null);
83         fImageHeight = image.getHeight(null);
84
85         // ensure origin is in Image?
86
fOriginX = originX;
87         fOriginY = originY;
88     }
89
90     /**
91      * Returns the ascent of this <code>ImageGraphicAttribute</code>. The
92      * ascent of an <code>ImageGraphicAttribute</code> is the distance
93      * from the top of the image to the origin.
94      * @return the ascent of this <code>ImageGraphicAttribute</code>.
95      */

96     public float getAscent() {
97
98         return Math.max(0, fOriginY);
99     }
100
101     /**
102      * Returns the descent of this <code>ImageGraphicAttribute</code>.
103      * The descent of an <code>ImageGraphicAttribute</code> is the
104      * distance from the origin to the bottom of the image.
105      * @return the descent of this <code>ImageGraphicAttribute</code>.
106      */

107     public float getDescent() {
108
109         return Math.max(0, fImageHeight-fOriginY);
110     }
111
112     /**
113      * Returns the advance of this <code>ImageGraphicAttribute</code>.
114      * The advance of an <code>ImageGraphicAttribute</code> is the
115      * distance from the origin to the right edge of the image.
116      * @return the advance of this <code>ImageGraphicAttribute</code>.
117      */

118     public float getAdvance() {
119
120         return Math.max(0, fImageWidth-fOriginX);
121     }
122
123     /**
124      * Returns a {@link Rectangle2D} that encloses all of the
125      * bits rendered by this <code>ImageGraphicAttribute</code>, relative
126      * to the rendering position. A graphic can be rendered beyond its
127      * origin, ascent, descent, or advance; but if it is, this
128      * method's implementation must indicate where the graphic is rendered.
129      * @return a <code>Rectangle2D</code> that encloses all of the bits
130      * rendered by this <code>ImageGraphicAttribute</code>.
131      */

132     public Rectangle2D JavaDoc getBounds() {
133
134         return new Rectangle2D.Float JavaDoc(
135                         -fOriginX, -fOriginY, fImageWidth, fImageHeight);
136     }
137
138     /**
139      * Renders the graphic at the specified location.
140      * @param graphics the {@link Graphics2D} into which to render the
141      * graphic
142      * @param x,&nbsp;y the user-space coordinates where the graphic is
143      * rendered
144      */

145     public void draw(Graphics2D JavaDoc graphics, float x, float y) {
146
147         graphics.drawImage(fImage, (int) (x-fOriginX), (int) (y-fOriginY), null);
148     }
149
150     /**
151      * Returns a hashcode for this <code>ImageGraphicAttribute</code>.
152      * @return a hash code value for this object.
153      */

154     public int hashCode() {
155
156         return fImage.hashCode();
157     }
158
159     /**
160      * Compares this <code>ImageGraphicAttribute</code> to the specified
161      * {@link Object}.
162      * @param rhs the <code>Object</code> to compare for equality
163      * @return <code>true</code> if this
164      * <code>ImageGraphicAttribute</code> equals <code>rhs</code>;
165      * <code>false</code> otherwise.
166      */

167     public boolean equals(Object JavaDoc rhs) {
168
169         try {
170             return equals((ImageGraphicAttribute JavaDoc) rhs);
171         }
172         catch(ClassCastException JavaDoc e) {
173             return false;
174         }
175     }
176
177     /**
178      * Compares this <code>ImageGraphicAttribute</code> to the specified
179      * <code>ImageGraphicAttribute</code>.
180      * @param rhs the <code>ImageGraphicAttribute</code> to compare for
181      * equality
182      * @return <code>true</code> if this
183      * <code>ImageGraphicAttribute</code> equals <code>rhs</code>;
184      * <code>false</code> otherwise.
185      */

186     public boolean equals(ImageGraphicAttribute JavaDoc rhs) {
187
188         if (rhs == null) {
189             return false;
190         }
191
192         if (this == rhs) {
193             return true;
194         }
195
196         if (fOriginX != rhs.fOriginX || fOriginY != rhs.fOriginY) {
197             return false;
198         }
199
200         if (getAlignment() != rhs.getAlignment()) {
201             return false;
202         }
203
204         if (!fImage.equals(rhs.fImage)) {
205             return false;
206         }
207
208         return true;
209     }
210 }
211
212
213
Popular Tags