home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / font / ShapeGraphicAttribute.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  4.0 KB  |  162 lines

  1. /*
  2.  * @(#)ShapeGraphicAttribute.java    1.5 98/03/18
  3.  *
  4.  * Copyright 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. /*
  16.  * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
  17.  * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
  18.  *
  19.  * The original version of this source code and documentation is
  20.  * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
  21.  * of IBM. These materials are provided under terms of a License
  22.  * Agreement between Taligent and Sun. This technology is protected
  23.  * by multiple US and International patents.
  24.  *
  25.  * This notice and attribution to Taligent may not be removed.
  26.  * Taligent is a registered trademark of Taligent, Inc.
  27.  *
  28.  */
  29.  
  30. package java.awt.font;
  31.  
  32. import java.awt.Shape;
  33. import java.awt.Graphics;
  34. import java.awt.Rectangle;
  35. import java.awt.Graphics2D;
  36. import java.awt.geom.Rectangle2D;
  37. import java.awt.geom.GeneralPath;
  38.  
  39. public final class ShapeGraphicAttribute extends GraphicAttribute {
  40.  
  41.     public static final boolean STROKE = true;
  42.     public static final boolean FILL = false;
  43.  
  44.     private GeneralPath fShape;
  45.     private boolean fStroke;
  46.  
  47.     // cache shape bounds, since GeneralPath doesn't
  48.     private Rectangle2D fShapeBounds;
  49.  
  50.     public ShapeGraphicAttribute(Shape shape,
  51.                                  int alignment,
  52.                                  boolean stroke) {
  53.  
  54.         super(alignment);
  55.  
  56.         // remind - allow other Shapes
  57.         try {
  58.             fShape = (GeneralPath) shape;
  59.         }
  60.         catch(ClassCastException e) {
  61.             throw new IllegalArgumentException("Only GeneralPath is supported now.");
  62.         }
  63.  
  64.         fStroke = stroke;
  65.         fShapeBounds = fShape.getBounds2D();
  66.     }
  67.  
  68.     public float getAscent() {
  69.  
  70.         return (float) Math.max(0, -fShapeBounds.getTop());
  71.     }
  72.  
  73.     public float getDescent() {
  74.  
  75.         return (float) Math.max(0, fShapeBounds.getBottom());
  76.     }
  77.  
  78.     public float getAdvance() {
  79.  
  80.         return (float) Math.max(0, fShapeBounds.getRight());
  81.     }
  82.  
  83.     public void draw(Graphics2D graphics, float x, float y) {
  84.  
  85.         // remind:  hack implementation - no Graphics2D
  86.         //Graphics g = graphics.fGraphics;
  87.         Graphics2D g = graphics;//.fGraphics;
  88.  
  89.         // translating graphics to draw Shape !!!
  90.         g.translate((int)x, (int)y);
  91.  
  92.         try {
  93.             if (fStroke == STROKE) {
  94.                 graphics.draw(fShape);
  95.             }
  96.             else {
  97.                 graphics.fill(fShape);
  98.             }
  99.         }
  100.         finally {
  101.             g.translate(-(int)x, -(int)y);
  102.         }
  103.     }
  104.  
  105.     public Rectangle2D getBounds() {
  106.  
  107.         // remind:  We will return the bounds, with one
  108.         // extra pixel on the right and bottom.  That
  109.         // will break if a different pen size is used.
  110.  
  111.         Rectangle2D.Float bounds = new Rectangle2D.Float();
  112.         bounds.setRect(fShapeBounds);
  113.  
  114.         if (fStroke == STROKE) {
  115.             ++bounds.width;
  116.             ++bounds.height;
  117.         }
  118.  
  119.         return bounds;
  120.     }
  121.  
  122.     public int hashCode() {
  123.  
  124.         return fShape.hashCode();
  125.     }
  126.  
  127.     public boolean equals(Object rhs) {
  128.  
  129.         try {
  130.             return equals((ShapeGraphicAttribute) rhs);
  131.         }
  132.         catch(ClassCastException e) {
  133.             return false;
  134.         }
  135.     }
  136.  
  137.     public boolean equals(ShapeGraphicAttribute rhs) {
  138.  
  139.         if (rhs == null) {
  140.             return false;
  141.         }
  142.  
  143.         if (this == rhs) {
  144.             return true;
  145.         }
  146.  
  147.         if (fStroke != rhs.fStroke) {
  148.             return false;
  149.         }
  150.  
  151.         if (getAlignment() != rhs.getAlignment()) {
  152.             return false;
  153.         }
  154.  
  155.         if (!fShape.equals(rhs.fShape)) {
  156.             return false;
  157.         }
  158.  
  159.         return true;
  160.     }
  161. }
  162.