home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 4.0 KB | 162 lines |
- /*
- * @(#)ShapeGraphicAttribute.java 1.5 98/03/18
- *
- * Copyright 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- /*
- * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
- * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
- *
- * The original version of this source code and documentation is
- * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
- * of IBM. These materials are provided under terms of a License
- * Agreement between Taligent and Sun. This technology is protected
- * by multiple US and International patents.
- *
- * This notice and attribution to Taligent may not be removed.
- * Taligent is a registered trademark of Taligent, Inc.
- *
- */
-
- package java.awt.font;
-
- import java.awt.Shape;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.awt.Graphics2D;
- import java.awt.geom.Rectangle2D;
- import java.awt.geom.GeneralPath;
-
- public final class ShapeGraphicAttribute extends GraphicAttribute {
-
- public static final boolean STROKE = true;
- public static final boolean FILL = false;
-
- private GeneralPath fShape;
- private boolean fStroke;
-
- // cache shape bounds, since GeneralPath doesn't
- private Rectangle2D fShapeBounds;
-
- public ShapeGraphicAttribute(Shape shape,
- int alignment,
- boolean stroke) {
-
- super(alignment);
-
- // remind - allow other Shapes
- try {
- fShape = (GeneralPath) shape;
- }
- catch(ClassCastException e) {
- throw new IllegalArgumentException("Only GeneralPath is supported now.");
- }
-
- fStroke = stroke;
- fShapeBounds = fShape.getBounds2D();
- }
-
- public float getAscent() {
-
- return (float) Math.max(0, -fShapeBounds.getTop());
- }
-
- public float getDescent() {
-
- return (float) Math.max(0, fShapeBounds.getBottom());
- }
-
- public float getAdvance() {
-
- return (float) Math.max(0, fShapeBounds.getRight());
- }
-
- public void draw(Graphics2D graphics, float x, float y) {
-
- // remind: hack implementation - no Graphics2D
- //Graphics g = graphics.fGraphics;
- Graphics2D g = graphics;//.fGraphics;
-
- // translating graphics to draw Shape !!!
- g.translate((int)x, (int)y);
-
- try {
- if (fStroke == STROKE) {
- graphics.draw(fShape);
- }
- else {
- graphics.fill(fShape);
- }
- }
- finally {
- g.translate(-(int)x, -(int)y);
- }
- }
-
- public Rectangle2D getBounds() {
-
- // remind: We will return the bounds, with one
- // extra pixel on the right and bottom. That
- // will break if a different pen size is used.
-
- Rectangle2D.Float bounds = new Rectangle2D.Float();
- bounds.setRect(fShapeBounds);
-
- if (fStroke == STROKE) {
- ++bounds.width;
- ++bounds.height;
- }
-
- return bounds;
- }
-
- public int hashCode() {
-
- return fShape.hashCode();
- }
-
- public boolean equals(Object rhs) {
-
- try {
- return equals((ShapeGraphicAttribute) rhs);
- }
- catch(ClassCastException e) {
- return false;
- }
- }
-
- public boolean equals(ShapeGraphicAttribute rhs) {
-
- if (rhs == null) {
- return false;
- }
-
- if (this == rhs) {
- return true;
- }
-
- if (fStroke != rhs.fStroke) {
- return false;
- }
-
- if (getAlignment() != rhs.getAlignment()) {
- return false;
- }
-
- if (!fShape.equals(rhs.fShape)) {
- return false;
- }
-
- return true;
- }
- }
-