/*
 * imageBox.java
 * English version 0.0.5
 * time 16:05
 * date 28/may/2009
 *
 * i beleived that a lot of new comers found it hard to use images in JAVA.
 * specially who came from .Net world. so i decided to smplify things out by
 * providing this simple tool. I call it imageBox to make it sound like
 * VB Picture box.
 *
 * this imageBox is a hacked version of JPanel
 * all what I did, is just overriding paintComponent(Graphics g)
 * after that i force it to load the Image file then draw it on the
 * component itself.
 *
 * imageBox is a very usefull tool, you may add it to netbeans palette.
 * this will allow you to change images from design level from imageFile option
 * 
 * note that you need to use URL insted of normal path
 * you URL can be generated by this code:
 * getClass().getResource("/imgFolder/imgFile.jpg");
 *
 * Feel free to contact me for any help.
 * Ahmed AL-Yamani
 * a7med.yamani@gmail.com
/**
 *
imageBox is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any older/later version.

imageBox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with UPL.  If not, see <http://www.gnu.org/licenses/>.*/

import java.awt.Graphics;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class imagebox extends JPanel {

    @Override
    public void paint(Graphics g) {

        super.paint(g);
        if (imageFile != null) {
            //imageIcon to load the image
            ImageIcon imageF = new ImageIcon(imageFile);
            //draw the image in stretch mode
            g.drawImage(imageF.getImage(), 0, 0, getWidth(), getHeight(), this);
        }else{
            g.drawLine(0,0,getWidth(),getHeight());
            g.drawLine(0,getHeight(),getWidth(),0);
            
        }
    }

    //allow user to change image file
    public void setimageFile(String absoultePath) {
        setimageFile(getClass().getResource(absoultePath));
    }

    public void setimageFile(URL absoultePath) {
        imageFile = absoultePath;
    }

    public URL getImageUrl() {
        return imageFile;
    }
    private URL imageFile = null;
}
