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 / ImageGraphicAttribute.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  3.4 KB  |  138 lines

  1. /*
  2.  * @(#)ImageGraphicAttribute.java    1.6 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.Image;
  33. import java.awt.Graphics2D;
  34. import java.awt.geom.Rectangle2D;
  35.  
  36. public final class ImageGraphicAttribute extends GraphicAttribute {
  37.  
  38.     private Image fImage;
  39.     private float fImageWidth, fImageHeight;
  40.     private float fOriginX, fOriginY;
  41.  
  42.     // hmmm, should make it convenient to use html-style center and
  43.     // bottom alignments
  44.     public ImageGraphicAttribute(Image image, int alignment) {
  45.  
  46.         this(image, alignment, 0, 0);
  47.     }
  48.  
  49.     public ImageGraphicAttribute(Image image,
  50.                                  int alignment,
  51.                                  float originX,
  52.                                  float originY) {
  53.  
  54.         super(alignment);
  55.  
  56.         // Can't clone image
  57.         // fImage = (Image) image.clone();
  58.         fImage = image;
  59.  
  60.         fImageWidth = image.getWidth(null);
  61.         fImageHeight = image.getHeight(null);
  62.  
  63.         // ensure origin is in Image?
  64.         fOriginX = originX;
  65.         fOriginY = originY;
  66.     }
  67.  
  68.     public float getAscent() {
  69.  
  70.         return Math.max(0, fOriginY);
  71.     }
  72.  
  73.     public float getDescent() {
  74.  
  75.         return Math.max(0, fImageHeight-fOriginY);
  76.     }
  77.  
  78.     public float getAdvance() {
  79.  
  80.         return Math.max(0, fImageWidth-fOriginX);
  81.     }
  82.  
  83.     public Rectangle2D getBounds() {
  84.  
  85.         return new Rectangle2D.Float(
  86.                         -fOriginX, -fOriginY, fImageWidth, fImageHeight);
  87.     }
  88.  
  89.     public void draw(Graphics2D graphics, float x, float y) {
  90.  
  91.         // remind:  hack implementation - no Graphics2D
  92.         //Graphics g = graphics.fGraphics;
  93.         graphics.drawImage(fImage, (int) (x-fOriginX), (int) (y-fOriginY), null);
  94.     }
  95.  
  96.     public int hashCode() {
  97.  
  98.         return fImage.hashCode();
  99.     }
  100.  
  101.     public boolean equals(Object rhs) {
  102.  
  103.         try {
  104.             return equals((ImageGraphicAttribute) rhs);
  105.         }
  106.         catch(ClassCastException e) {
  107.             return false;
  108.         }
  109.     }
  110.  
  111.     public boolean equals(ImageGraphicAttribute rhs) {
  112.  
  113.         if (rhs == null) {
  114.             return false;
  115.         }
  116.  
  117.         if (this == rhs) {
  118.             return true;
  119.         }
  120.  
  121.         if (fOriginX != rhs.fOriginX || fOriginY != rhs.fOriginY) {
  122.             return false;
  123.         }
  124.  
  125.         if (getAlignment() != rhs.getAlignment()) {
  126.             return false;
  127.         }
  128.  
  129.         if (!fImage.equals(rhs.fImage)) {
  130.             return false;
  131.         }
  132.  
  133.         return true;
  134.     }
  135. }
  136.  
  137.  
  138.