1 7 8 22 23 package java.awt.font; 24 25 import java.awt.Shape ; 26 import java.awt.Graphics ; 27 import java.awt.Rectangle ; 28 import java.awt.Graphics2D ; 29 import java.awt.geom.Rectangle2D ; 30 31 36 public final class ShapeGraphicAttribute extends GraphicAttribute { 37 38 private Shape fShape; 39 private boolean fStroke; 40 41 44 public static final boolean STROKE = true; 45 46 49 public static final boolean FILL = false; 50 51 private Rectangle2D fShapeBounds; 53 54 68 public ShapeGraphicAttribute(Shape 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 86 public float getAscent() { 87 88 return (float) Math.max(0, -fShapeBounds.getMinY()); 89 } 90 91 98 public float getDescent() { 99 100 return (float) Math.max(0, fShapeBounds.getMaxY()); 101 } 102 103 110 public float getAdvance() { 111 112 return (float) Math.max(0, fShapeBounds.getMaxX()); 113 } 114 115 123 public void draw(Graphics2D graphics, float x, float y) { 124 125 graphics.translate((int)x, (int)y); 127 128 try { 129 if (fStroke == STROKE) { 130 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 151 public Rectangle2D getBounds() { 152 153 Rectangle2D.Float bounds = new Rectangle2D.Float (); 154 bounds.setRect(fShapeBounds); 155 156 if (fStroke == STROKE) { 157 ++bounds.width; 158 ++bounds.height; 159 } 160 161 return bounds; 162 } 163 164 169 public int hashCode() { 170 171 return fShape.hashCode(); 172 } 173 174 182 public boolean equals(Object rhs) { 183 184 try { 185 return equals((ShapeGraphicAttribute ) rhs); 186 } 187 catch(ClassCastException e) { 188 return false; 189 } 190 } 191 192 201 public boolean equals(ShapeGraphicAttribute 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 |