libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / java / awt / font / ShapeGraphicAttribute.java
blob06814972b068c1f9044259f8b54b6f7410b22746
1 /* ShapeGraphicAttribute.java
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt.font;
41 import java.awt.Graphics2D;
42 import java.awt.Shape;
43 import java.awt.geom.Rectangle2D;
45 /**
46 * This is an implementation of GraphicAttribute that draws shapes in a TextLayout.
48 * @author Lillian Angel (langel at redhat dot com)
50 public final class ShapeGraphicAttribute extends GraphicAttribute
52 /** True if the shape should be filled. */
53 public static final boolean FILL = false;
55 /** True if the shape should be stroked with a 1-pixel wide stroke. */
56 public static final boolean STROKE = true;
58 private Shape shape;
59 private boolean stroke;
60 private Rectangle2D bounds;
62 /**
63 * Constructor.
65 * @param shape - the Shape to render. The Shape is rendered with its origin.
66 * @param alignment - the alignment
67 * @param stroke - true if the Shape should be stroked; false if the Shape
68 * should be filled.
70 public ShapeGraphicAttribute(Shape shape, int alignment, boolean stroke)
72 super(alignment);
73 this.shape = shape;
74 this.stroke = stroke;
75 this.bounds = shape.getBounds2D();
78 /**
79 * Draws the graphic at the given location.
81 * @param graphics - the graphics to use.
82 * @param x - the x location to draw at.
83 * @param y - the y location to draw at.
85 public void draw(Graphics2D graphics, float x, float y)
87 graphics.translate(x, y);
88 if (stroke == STROKE)
89 graphics.draw(shape);
90 else
91 graphics.fill(shape);
92 graphics.translate(- x, - y);
95 /**
96 * Compares this ShapeGraphicAttribute to obj.
98 * @param obj - the object to compare.
100 public boolean equals(Object obj)
102 if (! (obj instanceof ShapeGraphicAttribute))
103 return false;
105 return equals((ShapeGraphicAttribute) obj);
109 * Compares this ShapeGraphicAttribute to rhs.
111 * @param rhs - the ShapeGraphicAttribute to compare.
113 public boolean equals(ShapeGraphicAttribute rhs)
115 return (this == rhs || (this.shape.equals(rhs.shape)
116 && getAlignment() == rhs.getAlignment()
117 && stroke == rhs.stroke
118 && getAdvance() == rhs.getAdvance()
119 && getAscent() == rhs.getAscent()
120 && getBounds().equals(rhs.getBounds())
121 && getDescent() == rhs.getDescent()
122 && hashCode() == rhs.hashCode()));
126 * Gets the distance from the origin of its Shape to the right side of the
127 * bounds of its Shape.
129 * @return the advance
131 public float getAdvance()
133 return Math.max(0, (float) bounds.getMaxX());
137 * Gets the positive distance from the origin of its Shape to the top of
138 * bounds.
140 * @return the ascent
142 public float getAscent()
144 return Math.max(0, -(float) bounds.getMinY());
148 * Gets the distance from the origin of its Shape to the bottom of the bounds.
150 * @return the descent
152 public float getDescent()
154 return Math.max(0, (float) bounds.getMaxY());
158 * Returns a Rectangle2D that encloses all of the bits drawn by this shape.
160 * @return the bounds of the shape.
162 public Rectangle2D getBounds()
164 Rectangle2D.Float bounds = new Rectangle2D.Float();
165 bounds.setRect(this.bounds);
167 if (stroke == STROKE)
169 bounds.width++;
170 bounds.height++;
173 return bounds;
177 * Gets the hash code.
179 * @return the hash code.
181 public int hashCode()
183 return shape.hashCode();