001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Ilya S. Okomin
019: * @version $Revision$
020: */package java.awt.font;
021:
022: import java.awt.Graphics2D;
023: import java.awt.Image;
024: import java.awt.geom.Rectangle2D;
025:
026: import org.apache.harmony.misc.HashCode;
027:
028: public final class ImageGraphicAttribute extends GraphicAttribute {
029:
030: // Image object rendered by this ImageGraphicAttribute
031: private Image fImage;
032:
033: // X coordinate of the origin point
034: private float fOriginX;
035:
036: // Y coordinate of the origin point
037: private float fOriginY;
038:
039: // the width of the image object
040: private float fImgWidth;
041:
042: // the height of the image object
043: private float fImgHeight;
044:
045: public ImageGraphicAttribute(Image image, int alignment,
046: float originX, float originY) {
047: super (alignment);
048:
049: this .fImage = image;
050: this .fOriginX = originX;
051: this .fOriginY = originY;
052:
053: this .fImgWidth = fImage.getWidth(null);
054: this .fImgHeight = fImage.getHeight(null);
055:
056: }
057:
058: public ImageGraphicAttribute(Image image, int alignment) {
059: this (image, alignment, 0, 0);
060: }
061:
062: @Override
063: public int hashCode() {
064: HashCode hash = new HashCode();
065:
066: hash.append(fImage.hashCode());
067: hash.append(getAlignment());
068: return hash.hashCode();
069: }
070:
071: public boolean equals(ImageGraphicAttribute iga) {
072: if (iga == null) {
073: return false;
074: }
075:
076: if (iga == this ) {
077: return true;
078: }
079:
080: return (fOriginX == iga.fOriginX && fOriginY == iga.fOriginY
081: && getAlignment() == iga.getAlignment() && fImage
082: .equals(iga.fImage));
083: }
084:
085: @Override
086: public boolean equals(Object obj) {
087: try {
088: return equals((ImageGraphicAttribute) obj);
089: } catch (ClassCastException e) {
090: return false;
091: }
092:
093: }
094:
095: @Override
096: public void draw(Graphics2D g2, float x, float y) {
097: g2.drawImage(fImage, (int) (x - fOriginX),
098: (int) (y - fOriginY), null);
099: }
100:
101: @Override
102: public float getAdvance() {
103: return Math.max(0, fImgWidth - fOriginX);
104: }
105:
106: @Override
107: public float getAscent() {
108: return Math.max(0, fOriginY);
109: }
110:
111: @Override
112: public Rectangle2D getBounds() {
113: return new Rectangle2D.Float(-fOriginX, -fOriginY, fImgWidth,
114: fImgHeight);
115: }
116:
117: @Override
118: public float getDescent() {
119: return Math.max(0, fImgHeight - fOriginY);
120: }
121:
122: }
|