KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ShapeGraphicAttribute.java 1.15 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.Shape JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.Rectangle JavaDoc;
28 import java.awt.Graphics2D JavaDoc;
29 import java.awt.geom.Rectangle2D JavaDoc;
30
31 /**
32  * The <code>ShapeGraphicAttribute</code> class is an implementation of
33  * {@link GraphicAttribute} that draws shapes in a {@link TextLayout}.
34  * @see GraphicAttribute
35  */

36 public final class ShapeGraphicAttribute extends GraphicAttribute JavaDoc {
37
38     private Shape JavaDoc fShape;
39     private boolean fStroke;
40
41     /**
42      * A key indicating the shape should be stroked with a 1-pixel wide stroke.
43      */

44     public static final boolean STROKE = true;
45
46     /**
47      * A key indicating the shape should be filled.
48      */

49     public static final boolean FILL = false;
50
51     // cache shape bounds, since GeneralPath doesn't
52
private Rectangle2D JavaDoc fShapeBounds;
53
54     /**
55      * Constructs a <code>ShapeGraphicAttribute</code> for the specified
56      * {@link Shape}.
57      * @param shape the <code>Shape</code> to render. The
58      * <code>Shape</code> is rendered with its origin at the origin of
59      * this <code>ShapeGraphicAttribute</code> in the
60      * host <code>TextLayout</code>. This object maintains a reference to
61      * <code>shape</code>.
62      * @param alignment one of the alignments from this
63      * <code>ShapeGraphicAttribute</code>.
64      * @param stroke <code>true</code> if the <code>Shape</code> should be
65      * stroked; <code>false</code> if the <code>Shape</code> should be
66      * filled.
67      */

68     public ShapeGraphicAttribute(Shape JavaDoc shape,
69                                  int alignment,
70                                  boolean stroke) {
71
72         super(alignment);
73
74         fShape = shape;
75         fStroke = stroke;
76         fShapeBounds = fShape.getBounds2D();
77     }
78
79     /**
80      * Returns the ascent of this <code>ShapeGraphicAttribute</code>. The
81      * ascent of a <code>ShapeGraphicAttribute</code> is the positive
82      * distance from the origin of its <code>Shape</code> to the top of
83      * bounds of its <code>Shape</code>.
84      * @return the ascent of this <code>ShapeGraphicAttribute</code>.
85      */

86     public float getAscent() {
87
88         return (float) Math.max(0, -fShapeBounds.getMinY());
89     }
90
91     /**
92      * Returns the descent of this <code>ShapeGraphicAttribute</code>.
93      * The descent of a <code>ShapeGraphicAttribute</code> is the distance
94      * from the origin of its <code>Shape</code> to the bottom of the
95      * bounds of its <code>Shape</code>.
96      * @return the descent of this <code>ShapeGraphicAttribute</code>.
97      */

98     public float getDescent() {
99
100         return (float) Math.max(0, fShapeBounds.getMaxY());
101     }
102
103     /**
104      * Returns the advance of this <code>ShapeGraphicAttribute</code>.
105      * The advance of a <code>ShapeGraphicAttribute</code> is the distance
106      * from the origin of its <code>Shape</code> to the right side of the
107      * bounds of its <code>Shape</code>.
108      * @return the advance of this <code>ShapeGraphicAttribute</code>.
109      */

110     public float getAdvance() {
111
112         return (float) Math.max(0, fShapeBounds.getMaxX());
113     }
114
115     /**
116      * Draws the graphic at the given location. The <code>Shape</code>
117      * is drawn with its origin at (x,&nbsp;y).
118      * @param graphics the {@link Graphics2D} into which to draw the
119      * graphic
120      * @param x,&nbsp;y the user space coordinates where the graphic is
121      * drawn
122      */

123     public void draw(Graphics2D JavaDoc graphics, float x, float y) {
124
125         // translating graphics to draw Shape !!!
126
graphics.translate((int)x, (int)y);
127
128         try {
129             if (fStroke == STROKE) {
130                 // REMIND: set stroke to correct size
131
graphics.draw(fShape);
132             }
133             else {
134                 graphics.fill(fShape);
135             }
136         }
137         finally {
138             graphics.translate(-(int)x, -(int)y);
139         }
140     }
141
142     /**
143      * Returns a {@link Rectangle2D} that encloses all of the
144      * bits drawn by this <code>ShapeGraphicAttribute</code> relative to
145      * the rendering position. A graphic can be rendered beyond its
146      * origin, ascent, descent, or advance; but if it does, this method's
147      * implementation should indicate where the graphic is rendered.
148      * @return a <code>Rectangle2D</code> that encloses all of the bits
149      * rendered by this <code>ShapeGraphicAttribute</code>.
150      */

151     public Rectangle2D JavaDoc getBounds() {
152
153         Rectangle2D.Float JavaDoc bounds = new Rectangle2D.Float JavaDoc();
154         bounds.setRect(fShapeBounds);
155
156         if (fStroke == STROKE) {
157             ++bounds.width;
158             ++bounds.height;
159         }
160
161         return bounds;
162     }
163
164     /**
165      * Returns a hashcode for this <code>ShapeGraphicAttribute</code>.
166      * @return a hash code value for this
167      * <code>ShapeGraphicAttribute</code>.
168      */

169     public int hashCode() {
170
171         return fShape.hashCode();
172     }
173
174     /**
175      * Compares this <code>ShapeGraphicAttribute</code> to the specified
176      * <code>Object</code>.
177      * @param rhs the <code>Object</code> to compare for equality
178      * @return <code>true</code> if this
179      * <code>ShapeGraphicAttribute</code> equals <code>rhs</code>;
180      * <code>false</code> otherwise.
181      */

182     public boolean equals(Object JavaDoc rhs) {
183
184         try {
185             return equals((ShapeGraphicAttribute JavaDoc) rhs);
186         }
187         catch(ClassCastException JavaDoc e) {
188             return false;
189         }
190     }
191
192     /**
193      * Compares this <code>ShapeGraphicAttribute</code> to the specified
194      * <code>ShapeGraphicAttribute</code>.
195      * @param rhs the <code>ShapeGraphicAttribute</code> to compare for
196      * equality
197      * @return <code>true</code> if this
198      * <code>ShapeGraphicAttribute</code> equals <code>rhs</code>;
199      * <code>false</code> otherwise.
200      */

201     public boolean equals(ShapeGraphicAttribute JavaDoc rhs) {
202
203         if (rhs == null) {
204             return false;
205         }
206
207         if (this == rhs) {
208             return true;
209         }
210
211         if (fStroke != rhs.fStroke) {
212             return false;
213         }
214
215         if (getAlignment() != rhs.getAlignment()) {
216             return false;
217         }
218
219         if (!fShape.equals(rhs.fShape)) {
220             return false;
221         }
222
223         return true;
224     }
225 }
226
Popular Tags