1 7 8 22 23 package java.awt.font; 24 25 import java.awt.Image ; 26 import java.awt.Graphics2D ; 27 import java.awt.geom.Rectangle2D ; 28 29 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 51 public ImageGraphicAttribute(Image image, int alignment) { 52 53 this(image, alignment, 0, 0); 54 } 55 56 71 public ImageGraphicAttribute(Image image, 72 int alignment, 73 float originX, 74 float originY) { 75 76 super(alignment); 77 78 fImage = image; 81 82 fImageWidth = image.getWidth(null); 83 fImageHeight = image.getHeight(null); 84 85 fOriginX = originX; 87 fOriginY = originY; 88 } 89 90 96 public float getAscent() { 97 98 return Math.max(0, fOriginY); 99 } 100 101 107 public float getDescent() { 108 109 return Math.max(0, fImageHeight-fOriginY); 110 } 111 112 118 public float getAdvance() { 119 120 return Math.max(0, fImageWidth-fOriginX); 121 } 122 123 132 public Rectangle2D getBounds() { 133 134 return new Rectangle2D.Float ( 135 -fOriginX, -fOriginY, fImageWidth, fImageHeight); 136 } 137 138 145 public void draw(Graphics2D graphics, float x, float y) { 146 147 graphics.drawImage(fImage, (int) (x-fOriginX), (int) (y-fOriginY), null); 148 } 149 150 154 public int hashCode() { 155 156 return fImage.hashCode(); 157 } 158 159 167 public boolean equals(Object rhs) { 168 169 try { 170 return equals((ImageGraphicAttribute ) rhs); 171 } 172 catch(ClassCastException e) { 173 return false; 174 } 175 } 176 177 186 public boolean equals(ImageGraphicAttribute rhs) { 187 188 if (rhs == null) { 189 return false; 190 } 191 192 if (this == rhs) { 193 return true; 194 } 195 196 if (fOriginX != rhs.fOriginX || fOriginY != rhs.fOriginY) { 197 return false; 198 } 199 200 if (getAlignment() != rhs.getAlignment()) { 201 return false; 202 } 203 204 if (!fImage.equals(rhs.fImage)) { 205 return false; 206 } 207 208 return true; 209 } 210 } 211 212 213 | Popular Tags |