/*
 * Copyright (c) 2006-2008 MiniMe. Code released under The MIT/X Window System
 * License. Full license text can be found in license.txt
 */
package minime.ui;

import javax.microedition.lcdui.Graphics;

import minime.Drawable;
import minime.Image;

/**
 * The ImagePanel class is used to hold graphical image.
 * 
 * @author yishu
 * 
 */
public class ImagePanel extends Drawable
{
    protected Image image;

    /**
     * Constructs an instance of ImagePanel by given image.
     * 
     * @param image
     *            a image instance.
     */
    public ImagePanel(Image image)
    {
        this.image = image;
        setNeedLayout();
    }

    /**
     * Create an instance of ImagePanel, image instance will accesses
     * ResourceManager to get the image data from resource file(.bin) based on
     * image resource id.if want to set image to null, imRscId should be
     * ResourceManager.NONE_RESOURCE.
     * 
     * @param imRscId
     *            the image resource id, if want to set image to null, imRscId
     *            should be ResourceManager.NONE_RESOURCE.
     */
    public ImagePanel(int imRscId)
    {
        this(new Image(imRscId));
    }

    protected void layoutImp()
    {
        if (image != null)
        {
            setWidth(image.getWidth());
            setHeight(image.getHeight());
        }
    }

    /**
     * Sets the image for ImagePanel.
     * 
     * @param i
     *            a image instance.
     */
    public void setImage(Image i)
    {
        image = i;
        setNeedLayout();
    }

    /**
     * Gets the image holden in ImagePanel.
     * 
     * @return the image holden in ImagePanel.
     */
    public Image getImage()
    {
        return image;
    }

    /**
     * Sets the image for ImagePanel, the image will be gotten from image
     * resource file(.bin) based on imRscId by ResrouceManager.
     * 
     * @param imRscId
     *            the image resource id.
     */
    public void setImage(int imRscId)
    {
        setImage(new Image(imRscId));
    }

    /**
     * Render the image.
     */
    public void renderImpl(Graphics gc)
    {
        if (image != null)
            image.draw(gc, 0, 0);
    }
}
