001: package net.xoetrope.swing;
002:
003: import java.awt.Graphics;
004: import java.awt.Image;
005: import java.awt.image.ImageObserver;
006: import javax.swing.JComponent;
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 (c) Xoetrope Ltd., 1998-2003</p>
015: * License: see license.txt
016: * $Revision: 1.10 $
017: */
018: public class XImage extends JComponent 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 the image being displayed.
049: * @return the image name
050: */
051: public String getImageName() {
052: return imageName;
053: }
054:
055: /**
056: * Renders the component
057: * @param g the graphics context
058: */
059: public void paintComponent(Graphics g) {
060: if (image != null)
061: g.drawImage(image, 0, 0, (int) getSize().width,
062: (int) getSize().height, null);
063: }
064:
065: /**
066: * Update the image as it is loaded.
067: * @param img the image
068: * @param infoflags the flags
069: * @param x
070: * @param y
071: * @param width
072: * @param height
073: * @return super.imageUpdate(...)
074: */
075: public boolean imageUpdate(Image img, int infoflags, int x, int y,
076: int width, int height) {
077: repaint(100, x, y, width, height);
078: if (infoflags == ImageObserver.ALLBITS)
079: return false;
080:
081: return true;
082: }
083:
084: /**
085: * Set one or more attributes of the component.
086: * <OL>
087: * <LI>content, value=the image file name</LI>
088: * <LI>imagename, value=the image file name</LI>
089: * </OL>
090: * @param attribName the attribute name
091: * @param attribValue the attribute value
092: */
093: public void setAttribute(String attribName, String attribValue) {
094: if (attribName.equals("content")
095: || attribName.equals("imagename")) {
096: imageName = attribValue;
097: setImage(XResourceManager.getImage(imageName));
098: }
099: }
100: }
|