001: package net.xoetrope.awt;
002:
003: import java.awt.Canvas;
004: import java.awt.Graphics;
005: import java.awt.Image;
006: import java.awt.image.ImageObserver;
007:
008: import net.xoetrope.xui.XImageHolder;
009: import net.xoetrope.xui.XAttributedComponent;
010: import net.xoetrope.xui.XResourceManager;
011:
012: /**
013: * <p>Draws an image</p>
014: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
015: * License: see license.txt
016: * $Revision: 1.11 $
017: */
018: public class XImage extends Canvas implements XImageHolder,
019: XAttributedComponent {
020: private Image image = null;
021: private String imageName;
022:
023: /**
024: * Constructs a blank image control.
025: */
026: public XImage() {
027: }
028:
029: /**
030: * Requests a repaint of the control once it has been created
031: */
032: public void addNotify() {
033: super .addNotify();
034: repaint(0);
035: }
036:
037: /**
038: * Sets the image to display.
039: * @param img the image
040: */
041: public void setImage(Image img) {
042: image = img;
043: repaint();
044: prepareImage(img, this );
045: }
046:
047: /**
048: * Gets the name of teh image being displayed.
049: * @return the image name
050: */
051: public String getImageName() {
052: return imageName;
053: }
054:
055: /**
056: * Update the component
057: * @param g the graphics context
058: */
059: public void update(Graphics g) {
060: paint(g);
061: }
062:
063: /**
064: * Renders the component
065: * @param g the graphics context
066: */
067: public void paint(Graphics g) {
068: if (image != null)
069: g.drawImage(image, 0, 0, (int) getSize().width,
070: (int) getSize().height, null, this );
071: }
072:
073: /**
074: * Update the image as it is loaded.
075: * @param img the image
076: * @param infoflags the flags
077: * @param x
078: * @param y
079: * @param width
080: * @param height
081: * @return super.imageUpdate(...)
082: */
083: public boolean imageUpdate(Image img, int infoflags, int x, int y,
084: int width, int height) {
085: if ((infoflags & ImageObserver.FRAMEBITS) == ImageObserver.FRAMEBITS)
086: repaint(0);
087:
088: if (infoflags == ImageObserver.ALLBITS) {
089: repaint(0); // 100, x, y, width, height );
090: return false;
091: } else if (infoflags > ImageObserver.ALLBITS)
092: return false;
093:
094: return true;
095: }
096:
097: /**
098: * Set one or more attributes of the component.
099: * <OL>
100: * <LI>content, value=the image file name</LI>
101: * <LI>imagename, value=the image file name</LI>
102: * </OL>
103: * @param attribName the attribute name
104: * @param attribValue the attribute value
105: */
106: public void setAttribute(String attribName, String attribValue) {
107: if (attribName.equals("content")
108: || attribName.equals("imagename")) {
109: imageName = attribValue;
110: setImage(XResourceManager.getImage(imageName));
111: }
112: }
113: }
|